如题,在某个线程中使用wait方法会抛出interrupt异常。在另一个线程使用interrupt强制清除冻结状态也会产生interrupt异常,这两个线程有什么不同呢?
- class StopThread implements Runnable
- {
- private boolean flag =true;
- public void run()
- {
- while(flag)
- {
- try
- {
- wait();
- }
- catch ()
- {
- System.out.println(Thread.currentThread().getName()+"....Exception");
- }
-
-
- System.out.println(Thread.currentThread().getName()+"....run");
- }
- }
- public void changeFlag()
- {
- flag = false;
- }
- }
- class StopThreadDemo
- {
- public static void main(String[] args)
- {
- StopThread st = new StopThread();
-
- Thread t1 = new Thread(st);
- Thread t2 = new Thread(st);
- t1.setDaemon(true);
- t2.setDaemon(true);
- t1.start();
- t2.start();
- int num = 0;
- while(true)
- {
- if(num++ == 60)
- {
- //st.changeFlag();
- t1.interrupt();
-
- break;
- }
- System.out.println(Thread.currentThread().getName()+"......."+num);
- }
- System.out.println("over");
- }
- }
复制代码
|
|