首页 > 其他 > 详细

[20160804]synchronized

时间:2016-08-04 14:52:33      阅读:134      评论:0      收藏:0      [点我收藏+]
 1 class Timer{
 2    private static int num;
 3 
 4    void add(String name){
 5      //synchronized (this){
 6        num++;
 7        try{  Thread.sleep(1);  }
 8        catch(InterruptedException e){};
 9        System.out.println(name+"  you are the  "+num+"  user to use timer!");
10      //}
11    }
12 }
13 
14 
15 public class TestSync implements Runnable{
16     Timer timer = new Timer();
17 
18     public static void main(String[] args) {
19        TestSync t = new TestSync();
20 
21        Thread t1 = new Thread(t,"t1");
22        Thread t2 = new Thread(t,"t2");
23 
24 
25        t1.start();
26        t2.start();
27     }
28 
29     public void run(){
30        timer.add(Thread.currentThread().getName());
31     }
32 }

 

[20160804]synchronized

原文:http://www.cnblogs.com/jasonzeng888/p/5736583.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!