notify不是唤起一个等待的线程吗??
为什么我的这个程序有一个线程被唤起之后就直接退掉了。
class ThreadRun implements Runnable{
private boolean flag=true;
public synchronized void run(){
while(flag){
try{
System.out.println(" i'll sleep!!....");
//放弃线程
this.wait();
}catch(InterruptedException e){
System.out.println("exception output");
//修改运行条件
this.flag=false;
}
System.out.println("so what??");
}
//唤醒一个线程
this.notify();
}
public void change(){
//this.notifyAll();
this.flag=false;
}
}
public class Main{
public static void main(String[] args){
ThreadRun example = new ThreadRun();
Thread t1 = new Thread(example);
Thread t2 = new Thread(example);
Thread t3 = new Thread(example);
t1.start();
t2.start();
t3.start();
int i=0;
while(true){
if(i++<60){
System.out.println(Thread.currentThread().getName());
}else{
System.out.println("end............");
//example.change();
//清除t1线程的中断状态,问题就在这里,只是清除了t1的中断状态,t1执行后清除掉运行的条件时对的,但是之后t1执行的notify只可能唤醒一个线程,也就是t2和t3中的一个,而程序应该还有一个线程在等待,但为什么程序直接就退出了??
请前辈们指教。小鸟感激不尽~~~~
t1.interrupt();
//t3.start();
break;
}
}
}
}
为什么我的这个程序有一个线程被唤起之后就直接退掉了。
class ThreadRun implements Runnable{
private boolean flag=true;
public synchronized void run(){
while(flag){
try{
System.out.println(" i'll sleep!!....");
//放弃线程
this.wait();
}catch(InterruptedException e){
System.out.println("exception output");
//修改运行条件
this.flag=false;
}
System.out.println("so what??");
}
//唤醒一个线程
this.notify();
}
public void change(){
//this.notifyAll();
this.flag=false;
}
}
public class Main{
public static void main(String[] args){
ThreadRun example = new ThreadRun();
Thread t1 = new Thread(example);
Thread t2 = new Thread(example);
Thread t3 = new Thread(example);
t1.start();
t2.start();
t3.start();
int i=0;
while(true){
if(i++<60){
System.out.println(Thread.currentThread().getName());
}else{
System.out.println("end............");
//example.change();
//清除t1线程的中断状态,问题就在这里,只是清除了t1的中断状态,t1执行后清除掉运行的条件时对的,但是之后t1执行的notify只可能唤醒一个线程,也就是t2和t3中的一个,而程序应该还有一个线程在等待,但为什么程序直接就退出了??
请前辈们指教。小鸟感激不尽~~~~
t1.interrupt();
//t3.start();
break;
}
}
}
}