[20160804]synchronized

 class Timer{
private static int num; void add(String name){
//synchronized (this){
num++;
try{ Thread.sleep(1); }
catch(InterruptedException e){};
System.out.println(name+" you are the "+num+" user to use timer!");
//}
}
} public class TestSync implements Runnable{
Timer timer = new Timer(); public static void main(String[] args) {
TestSync t = new TestSync(); Thread t1 = new Thread(t,"t1");
Thread t2 = new Thread(t,"t2"); t1.start();
t2.start();
} public void run(){
timer.add(Thread.currentThread().getName());
}
}
上一篇:JAVA基础中的注意点(二)


下一篇:PS CC 破解安装教程(亲测可用)