Working using Thread

1

I'm doing a college job who has to be used thread . But I've never used it and I'm still learning. My intention is, when you press the up key on your keyboard, startar the thread to make an event. But you're making a mistake that I can not figure out why.

ERROR:

Exception in thread "AWT-EventQueue-1" java.lang.IllegalThreadStateException
at java.lang.Thread.start(Thread.java:708)
at trabalho.Jogo.keyPressed(Jogo.java:52)
at java.awt.Component.processKeyEvent(Component.java:6491)
at java.awt.Component.processEvent(Component.java:6310)
at java.awt.Container.processEvent(Container.java:2238)
at java.awt.Window.processEvent(Window.java:2025)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2296)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1954)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:835)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1103)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:974)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:800)
at java.awt.Component.dispatchEventImpl(Component.java:4760)
at java.awt.Container.dispatchEventImpl(Container.java:2296)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
at java.awt.EventQueue$4.run(EventQueue.java:733)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:730)
at org.GNOME.Accessibility.AtkWrapper$6.dispatchEvent(AtkWrapper.java:715)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
public class Jogo extends JFrame implements KeyListener {

    private CenarioInterno cenarioInterno;

    private Homer homer;
    private Cenario cenario;

    public static void main(String[] args) {

        Jogo jogo = new Jogo();

    }

    public Jogo() {

        this.addKeyListener(this);

        this.setSize(800, 700);
        this.setVisible(true);
        this.setResizable(true);

        cenarioInterno = new CenarioInterno(200000, 200000, "moesbar.jpg");
        cenario = new Cenario(0, 0, "cenario.jpg");
        homer = new Homer(80, 440, "homer.gif");

    }

    static ThreadImagemInterna r = new ThreadImagemInterna();
    static Thread thread = new Thread(r);

    @Override
    public void keyPressed(KeyEvent e) {

        if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            homer.movimentoFrente();
            repaint();
        } else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            homer.movimentoTras();
            repaint();
        } else if (e.getKeyCode() == KeyEvent.VK_UP) {

            thread.start();
            repaint();

        }

        System.out.println("Tecla Apertada: " + e.getKeyChar() + " Codigo da tecla: " + e.getKeyCode());

    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    public void paint(Graphics graficos) {

        graficos.drawImage(cenario.getImage(), cenario.getX(), cenario.getY(), null);
        graficos.drawImage(homer.getImage(), homer.getX(), homer.getY(), null);
        graficos.drawImage(cenarioInterno.getImage(), cenarioInterno.getX(), cenarioInterno.getY(), null);

    }

}
public class ThreadImagemInterna extends DesenhoMovel implements Runnable {

    CenarioInterno cenarioInterno = new CenarioInterno();

    @Override
    public void run() {



        }
    }
    
asked by anonymous 08.06.2018 / 16:58

1 answer

4

A thread can only be started once. Trying to call the start() method on a thread already started gives you this exception you have: java.lang.IllegalThreadStateException .

The thread is created but not started here:

    static ThreadImagemInterna r = new ThreadImagemInterna();
    static Thread thread = new Thread(r);

When the up arrow is pressed ( KeyEvent.VK_UP ), in your keyPressed(KeyEvent) method, you do this:

            thread.start();

If you press the up arrow again, start() will be called a second time for the same thread, which is not allowed. The result is this exception.

Your code is not enough to know what you really wanted to do, but some of the ideas would be:

  • Create the thread within keyPressed(KeyEvent) .

  • Put tasks to be executed by your thread in a queue and make it consume the tasks that are in that queue.

  • Use a ExecutorService .

  • Set some flag somewhere and make your thread observe this flag.

I also recommend you take a look at on this question .

    
08.06.2018 / 20:21