日期:2014-05-20 浏览次数:20902 次
public class TestSync implements Runnable {
Timer t = new Timer();
public static void main(String[] args) {
TestSync ts1 = new TestSync();
TestSync ts2 = new TestSync();
Thread t1 = new Thread(ts1);
Thread t2 = new Thread(ts2);
t1.setName("t1");
t2.setName("t2");
t1.start();
t2.start();
// TODO Auto-generated method stub
}
public void run(){
t.add(Thread.currentThread().getName());
}
}
class Timer{
private static int num = 0;
public synchronized void add(String name){
//synchronized (this){
num ++;
try{
Thread.sleep(1);
}catch(InterruptedException e){}
System.out.println(name + ",你是第" + num + "个使用timer线程");
}
//}
}