当前所在位置:珠峰网资料 >> 计算机 >> 计算机等级考试 >> 正文
java多线程的记录
发布时间:2010/11/8 15:35:13 来源:城市学习网 编辑:ziteng
  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的线程");
  //}
  }
  }
广告合作:400-664-0084 全国热线:400-664-0084
Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号
珠峰网 版权所有 All Rights Reserved