Posted on 2008-12-06 11:31
鱼跃于渊 阅读(120)
评论(0) 编辑 收藏 所属分类:
j2se
1
2
3 public class ThreadTest3 implements Runnable{
4
5 Timer timer = new Timer();
6
7 public static void main(String[] args){
8 ThreadTest3 r1 = new ThreadTest3();
9 Thread t1 = new Thread(r1);
10 Thread t2 = new Thread(r1);
11
12 t1.setName("t1");
13 t2.setName("t2");
14 t1.start();
15 t2.start();
16 }
17
18 public void run(){
19 timer.add(Thread.currentThread().getName());
20 }
21 }
22
23 class Timer{
24 private static int num = 0;
25 public void add(String name){
26 synchronized(this){
27 num++ ;
28 try{
29 Thread.sleep(1000);
30 }catch(InterruptedException ex){
31
32 }
33 System.out.println("===" + name + " shi di " + num + " ge fang wen Timer de Thread !");
34 }
35 }
36 }