码迷,mamicode.com
首页 > 编程语言 > 详细

5.9 JAVA

时间:2016-05-09 15:48:39      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:

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);
}

}

技术分享

5.9 JAVA

标签:

原文地址:http://www.cnblogs.com/coolye/p/5473984.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!