A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 李征雪 于 2012-4-16 16:21 编辑
  1. class Tick implements Runnable
  2. {
  3.     public static int num = 0;
  4.     public int tic = 100;
  5.     public void run()
  6.     {
  7.         while (tic > 0)
  8.         {
  9.             System.out.println(Thread.currentThread().getName()+"--"+tic--);
  10.             num++;
  11.         }
  12.     }
  13. }
  14. class DemoThread2
  15. {
  16.     public static void main(String[] args)
  17.     {
  18.         Tick t = new Tick();
  19.         Thread t1 = new Thread(t);
  20.         Thread t2 = new Thread(t);
  21.         Thread t3 = new Thread(t);
  22.         Thread t4 = new Thread(t);
  23.         t1.start();
  24.         t2.start();
  25.         t3.start();
  26.         t4.start();

  27.         System.out.println("--------------------"+t.num);
  28.     }
  29. }
复制代码
我定义num是为了看看System.out.println(Thread.currentThread().getName()+"--"+tic--);这句代码到底执行了多少次,
我想让t1,t2,t3,t4都执行完毕后再打印下num,有没有什么办法?
呵呵,可能这个没啥意义,不过还是想看一下结果。

评分

参与人数 1技术分 +1 收起 理由
岳民喜 + 1

查看全部评分

2 个回复

倒序浏览
  1. class Tick implements Runnable
  2. {
  3.     public static int num = 0;
  4.     public int tic = 100;
  5.     public void run()
  6.     {
  7.         while (tic > 0)
  8.         {
  9.             System.out.println(Thread.currentThread().getName()+"--"+tic--);
  10.             num++;
  11.         }
  12.                 System.out.println("--------------------"+num);
  13.     }

  14. }
  15. class DemoThread2
  16. {
  17.     public static void main(String[] args)
  18.     {
  19.         Tick t = new Tick();
  20.         Thread t1 = new Thread(t);
  21.         Thread t2 = new Thread(t);
  22.         Thread t3 = new Thread(t);
  23.         Thread t4 = new Thread(t);
  24.         t1.start();
  25.         t2.start();
  26.         t3.start();
  27.         t4.start();

  28.         
  29.     }
  30. }
复制代码
这样改动后每个线程结束前都会打印当前的num

评分

参与人数 1技术分 +1 收起 理由
岳民喜 + 1

查看全部评分

回复 使用道具 举报
  1. class Tick implements Runnable
  2. {
  3.         public static int num = 0;
  4.         public int tic = 1000;
  5.         public void run()
  6.         {
  7.                 show();
  8.         }
  9.         public void show()
  10.         {
  11.                 while (tic > 0)
  12.                 {

  13.                                 while (tic > 0)
  14.                                 {
  15.                                         System.out.println(Thread.currentThread().getName()+"--"+tic--);
  16.                                         num++;
  17.                                 }

  18.                 }
  19.         }
  20. }
  21. class DemoThread2
  22. {
  23.         public static void main(String[] args) throws Exception
  24.         {
  25.                 Tick t = new Tick();
  26.                 Thread t1 = new Thread(t);
  27.                 Thread t2 = new Thread(t);
  28.                 Thread t3 = new Thread(t);
  29.                 Thread t4 = new Thread(t);
  30.                 t1.start();
  31.                 t2.start();
  32.                 t3.start();
  33.                 t4.start();
  34.                 t1.join();
  35.                 t2.join();
  36.                 t3.join();
  37.                 t4.join();

  38.                 System.out.println("--------------------"+t.num);
  39.         }
  40. }
复制代码
已经解决了,呵呵,谢谢!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马