多线程顺序的控制(wait,notity,sleep)
public class abc extends Thread{
private Object
prev=null;
private Object self=null;
private String msg=null;
public abc(Object
prev,Object self,String msg){
this.prev=prev;
this.self=self;
this.msg=msg;
}
public void
run(){
int count=0;
while(count<1000){
synchronized(prev){
synchronized(self){
System.out.print(msg);
if(msg.contains("C"))
System.out.println();
count++;
self.notify();
}
if(count==1000)
break;
try{
prev.wait();}
catch(Exception e)
{e.getMessage();}
}
}
}
public
static void main(String[] args){
Object
a=new Object();
Object b=new
Object();
Object c=new
Object();
abc abc1=new
abc(c,a,"A");
abc abc2=new
abc(a,b,"B");
abc abc3=new
abc(b,c,"C");
try{
abc1.start();Thread.sleep(100);}catch(Exception
e){e.getMessage();}
try{
abc2.start();Thread.sleep(100);}catch(Exception
e){e.getMessage();}
abc3.start();
}
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。