class FirstThread implements Runnable{
int i = 100;
public void run(){
while(i > 0){
synchronized(this){
System.out.println(Thread.currentThread().getName() + i);
i--;
if((i % 2) != 0){
try{
Thread.sleep(1 * 100);
}catch(Exception e){
e.printStackTrace();
}
}
}
}
}
}
public class TestDemo{
public static void main(String args[]){
FirstThread ft = new FirstThread();
Thread a = new Thread(ft);
Thread b = new Thread(ft);
a.setName("线程A");
b.setName("线程B");
a.start();
b.start();
}
}
synchronized关键字
最新推荐文章于 2020-10-11 16:32:17 发布