Work using Thread

Asked

Viewed 89 times

1

I’m doing a college paper who has to be used thread. But I’ve never used it and I’m still learning it. My intention is, when you press the up key on the keyboard, startar to thread to make an event. But there is a mistake that I am not able to figure out why this mistake.

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() {



        }
    }
  • Please provide a [mcve] so that it is possible to test the code and check the problem.

  • @DoutorStephenStrange https://pt.meta.stackoverflow.com/q/5359/132

  • @Victorstafusa Thanks for the teaching! show!!!

1 answer

4

A thread can only be started once. Try calling the method start() in an already started thread gives you this exception that you have: java.lang.IllegalThreadStateException.

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 his method keyPressed(KeyEvent), you do that:

            thread.start();

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

Your code isn’t enough to know what you really wanted to do, but some of the ideas would be:

  • Create thread within the keyPressed(KeyEvent).

  • Put tasks to be performed 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 that flag.

I also recommend you take a look in that question.

  • All right, I’ll take a look. Thanks for the help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.