无法调用包含Thread.sleep方法的方法

时间:2014-06-17 01:38:01

标签: java multithreading exception-handling

我在方法中实现Thread.sleep()并尝试从另一个类调用此方法;但是,每次执行此操作时代码都会冻结。我有一种感觉,它与Thread.sleep()导致的InterruptedException有关。

以下是一个示例:

班级名称为A

....
public static void start() throws InterruptedException
{
    //other code
    while (true) {
        Thread.sleep(10);
    }
}
...

单独的课程

    ...
    public static void call()
    {
        new A().start();
    }
    ...

start()在一个类中,call()在一个单独的类中,而call()试图从第一个类调用start方法。

3 个答案:

答案 0 :(得分:3)

 every time I do this the screen just freezes

这是因为Thread.sleep(10);中的while loop会将您当前的线程置于阻塞状态,从而冻结您的屏幕。

答案 1 :(得分:1)

一旦你调用了这个方法,while循环就开始了,因为它的条件而永远不会停止!

  while (true) {  <<<<<<<<<< change it to a logical condition or take it out
        Thread.sleep(10);
    }

答案 2 :(得分:0)

使用这种条件使得while循环永不停止,这不是智慧, 它是一个死循环,这就是冻结的原因。

while (true) {  
        Thread.sleep(10);
    }