首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

java多线程的记要

2012-12-23 
java多线程的记录public class TestSync implements Runnable {Timer timer new Timer()public static

java多线程的记录
public class TestSync implements Runnable {
  Timer timer = new Timer();
  public static void main(String[] args) {
    TestSync test = new TestSync();
    Thread t1 = new Thread(test);
    Thread t2 = new Thread(test);
    t1.setName("t1");
    t2.setName("t2");
    t1.start();
    t2.start();
  }
  public void run(){
    timer.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的线程");
  //}
  }
}

热点排行