public class TextDead implements Runnable{
public static void main(String[] args){
TextDead td1=new TextDead();
TextDead td2=new TextDead();
td1.flag=0;
td2.flag=1;
Thread t1=new Thread(td1);
Thread t2=new Thread(td2);
t1.start();
t2.start();
}
int flag = 0;
@Override
public void run() {
if(flag==0){
try {
TextDead.a();
TextDead.b();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(flag==1){
try {
TextDead.b();
TextDead.a();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static synchronized void a() throws InterruptedException{
Thread.sleep(10);
System.out.println("SDFDSA");
}
public static synchronized void b() throws InterruptedException{
Thread.sleep(10);
System.out.println(123);
}
}
wwpbjing
if_else_
相关分类