Java中线程出现Exception in thread "Thread-0" 异常 解决方法...

时间:2025-03-02 09:19:32

代码

package thread;

public class TestChongNeng {
    public static void main(String[] args) {
        Thread t1 = new Thread() {
            public void run() {
                while (true) {
                    for(int i=1;i<=3;i++) {
                        ("波动拳第%d发 %n",i);
                    }
                    try {
                        //synchronized (this) {
                            wait(2000);
                        //}    
                    } catch (InterruptedException e1) {
                        ();
                    }
                    
                    ("开始为时2s的充能");
                }
            }
        };
        ();
    }
}

 

报错信息 

Exception in thread "Thread-0"
at (Native Method)
at $(:13)

解决方法

在线线程中调用wait方法的时候 要用synchronized锁住对象,确保代码段不会被多个线程调用

 

参考文章:

/zhouxiaoyun0228/article/details/7757313/