码迷,mamicode.com
首页 > 其他好文 > 详细

[20160804]synchronized

时间:2016-08-04 14:52:41      阅读:89      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!