- class StopThread implements Runnable
- {
- private boolean flag = true;
- public synchronized void run()
- {
- while(flag)
- {
- try
- {
- wait();//<FONT color=red>我就是想知道这里wait了不就抛出异常了吗,下面直接在函数中解决异常,解决的时候是一条输出语句,但是为什么在编译运行后却看不到catch块中的打印语句呢?</FONT>
- }
- catch (InterruptedException e)
- {
- 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.start();
- t2.start();
- int num = 0;
- while(true)
- {
- if(num++ == 60)
- {
- st.changeFlag();
- break;
- }
- System.out.println(Thread.currentThread().getName()+"......."+num);
- }
- System.out.println("Over");
- }
- }
复制代码 求解释!
|