简体   繁体   English

如何等待线程完成其工作

[英]How to wait until a thread completes its work

I have a movement thread that listens for movement. 我有一个运动线程,听取运动。 I can let it run through the while loop, but when there is no movement taking place, the Thread goes to 100% CPU usage because its just going through the while loop over and over. 我可以让它在while循环中运行,但是当没有发生移动时,Thread会达到100%的CPU使用率,因为它只是一遍又一遍地通过while循环。

What I am trying to do is put that Thread to sleep AFTER it finishes it's animation, and wake it up when I press a key 我想要做的是让它在完成它的动画之后休眠,当我按下一个键时将其唤醒

When I press an arrow key, I will move in a direction until I let go. 当我按下箭头键时,我会向一个方向移动,直到我松开。 At which point the movementThread waits, but when I press another key, or the same key, the keyListener doesn't respond. moveThread在此时等待,但当我按下另一个键或相同的键时,keyListener不响应。 The Keylistener doesn't respond, because when I call Keylistener没有回应,因为我打电话的时候

waitMovementThread();

it never leaves the synchronized object. 它永远不会离开同步对象。 Idk whats wrong, or how I should fix this situation. Idk什么是错的,或者我应该如何解决这个问题。

private void keyListeners(){
    GlobalObjects.frame.addKeyListener(new KeyListener() {
        @Override
        public void keyPressed(KeyEvent e) {

            //Up    = 38
            //Down  = 40
            //Left  = 37
            //Right = 39
            System.out.println("Keypressed");
            if(e.getKeyCode() == 16){
                player.setPlayerMovementSpeed(3);
            }
            if(e.getKeyCode() == 38){
                notifyMovementThread();
                player.setPlayerMovement(1);
            }else if(e.getKeyCode() == 40){
                notifyMovementThread();
                player.setPlayerMovement(2);
            }else if(e.getKeyCode() == 37){
                notifyMovementThread();
                player.setPlayerMovement(3);
            }else if(e.getKeyCode() == 39){
                notifyMovementThread();
                player.setPlayerMovement(4);
            }
        }

        @Override
        public void keyReleased(KeyEvent e) {
            if(e.getKeyCode() == 16){
                player.setPlayerMovementSpeed(10);
            }
            if(e.getKeyCode() == 38){
                if(player.getPlayerMovement() != 1){

                }else{
                    player.setPlayerMovement(0);
                    waitMovementThread();
                }
            }else if(e.getKeyCode() == 40){
                if(player.getPlayerMovement() != 2){

                }else{
                    player.setPlayerMovement(0);
                    waitMovementThread();
                }
            }else if(e.getKeyCode() == 37){
                if(player.getPlayerMovement() != 3){

                }else{
                    player.setPlayerMovement(0);
                    waitMovementThread();
                }
            }else if(e.getKeyCode() == 39){
                if(player.getPlayerMovement() != 4){

                }else{
                    player.setPlayerMovement(0);
                    waitMovementThread();
                }
            }
        }

        @Override
        public void keyTyped(KeyEvent e) {

        }
    });
}
private void waitMovementThread(){
    synchronized(movementThread){
        try {
            movementThread.wait();
        } 
        catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
private void notifyMovementThread(){
    synchronized(movementThread){
        movementThread.notify();
    }
}
  1. Don't tie up the GUI event thread with a while (true) or a wait() . 不要将GUI事件线程与while (true)wait() Your GUI will freeze (not sure at this point if you're doing this or not). 您的GUI将冻结(此时不确定您是否正在执行此操作)。
  2. If a Swing application, don't use a KeyListener, but rather use Key Bindings. 如果是Swing应用程序,请不要使用KeyListener,而是使用Key Bindings。 The tut's will show you how. 啧啧将告诉你如何。
  3. Use a Swing Timer in place of your while or wait. 使用Swing Timer代替您的等待或等待。 Start the Timer on key press and stop it on key release. 按键启动计时器并在按键释放时停止计时器。 There are many examples of this to be found on this site and others, several written by me in fact. 在这个网站和其他网站上可以找到很多这样的例子,其中有几个是我写的。

For example, have a look at this question's answer code , including camickr's, Trashgod's and mine. 例如,看看这个问题的答案代码 ,包括camickr's,Trashgod和我的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM