Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java GUI使用线程冻结_Java_Multithreading_Frame - Fatal编程技术网

Java GUI使用线程冻结

Java GUI使用线程冻结,java,multithreading,frame,Java,Multithreading,Frame,我是线程编程的新手,我启动了一个线程,在一个框架内移动一个JPanel。但是当我调用该方法时,框架完全冻结,我必须强制它关闭 这是我实现的简化方法 public class Thread extends Thread{ public void movePanel(Frame f) { try { long t = f.getTime(); while (true) {

我是线程编程的新手,我启动了一个线程,在一个框架内移动一个JPanel。但是当我调用该方法时,框架完全冻结,我必须强制它关闭

这是我实现的简化方法

public class Thread extends Thread{
    
public void movePanel(Frame f) {
                try {
                    long t = f.getTime();
                    while (true) {
                        f.changeColor();// changes Colour of current JPanel
                        Thread.sleep(t);
                        while (true) {
                            f.setNextPosition(direction);// sets the Position, where the JPanel moves next
                            p = f.getNextPosition();
                            f.moveForward();// moves in given direction
                            Thread.sleep(dauer);
                            if (p == null)// checks if the next Position is accessible {
                                f.moveDown();
                                Thread.sleep(t);
                                while (true) {
                                    f.moveBackwards();
                                    Thread.sleep(t);
                                    if (p == null) {
                                        f.moveDown();
                                        Thread.sleep(t);
                                    }
                                }
                            }
                        }
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
}
什么会导致问题? 非常感谢您的帮助。

Thread.sleep()正在阻止当前线程至少达毫秒。重要的是“阻塞”!查找线程。等待(t);您有一个Thread.sleep(),并且有一段时间(true)从未完成执行,因此您一直处于循环中。对于动画,请使用