- class Test extends Thread{
- private boolean flag = true;
- public void changeFlagFalse(){
- flag = false;
- }
- public void run() {
- int num = 0;
- while(flag){
- if(num==10){
- try {
- wait();
- } catch (InterruptedException e) {
- System.out.println(Thread.currentThread().getName()+"...Exception");
- flag = false;
-
- }
- }
- System.out.println(Thread.currentThread().getName()+" "+num++);
- }
- }
- }
- public class ThreadExceptionDemo {
- public static void main(String[] args) {
- Test t = new Test();
- t.start();
- for(int i =0;i<=60;i++){
- System.out.println(Thread.currentThread().getName()+" "+i);
- if(i==60){
- t.changeFlagFalse();
- t.interrupt();
- }
- }
- }
- }
复制代码
为什么会抛出IllegalMonitorStateException异常啊,看不出来了
|