标签:
public class Test9_1 implements Runnable{
int count;
static Object obj = new Object();
public void run() {
synchronized(obj){
if(Thread.currentThread().getName().equals("a") || Thread.currentThread().getName().equals("b")){
count++;
System.out.println(Thread.currentThread().getName() + "_" + count);
}else{
count--;
System.out.println(Thread.currentThread().getName() + "_" + count);
}
}
}
public static void main(String[] args) throws InterruptedException {
Test9_1 t = new Test9_1();
new Thread(t, "a").start();
new Thread(t, "b").start();
new Thread(t, "c").start();
new Thread(t, "d").start();
Thread.sleep(1000);
System.out.println(t.count);
}
}
标签:
原文地址:http://www.cnblogs.com/Godxi/p/5473982.html