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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 郑文博 中级黑马   /  2012-7-8 17:23  /  1829 人查看  /  2 人回复  /   1 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 郑文博 于 2012-7-8 18:49 编辑
  1. /*
  2.         join()方法:
  3. */

  4. class Demo implements Runnable
  5. {
  6.         public void run()
  7.         {
  8.                 for (int x = 0;x < 70 ;x++ )
  9.                 {
  10.                         System.out.println(Thread.currentThread().getName()+"-----"+x);
  11.                         //Thread.yield();
  12.                 }
  13.         }
  14. }

  15. class JoinDemo
  16. {
  17.         public static void main(String[] args) throws Exception
  18.         {
  19.                 Demo d = new Demo();

  20.                 Thread t1 = new Thread(d);
  21.                 Thread t2 = new Thread(d);

  22.                 t1.start();
  23.                 t2.start();

  24.                 t1.join();
  25.                 /*
  26.                 对以上这句join()方法的理解是:当一个线程A执行到另一个线程B的join()方法时,A会等待,B线程执行完,A再执行。
  27.                 而这个程序通过语句看 t2来到t1的join()方法了,为什么t2没有进入等待状态?
  28.                 根据join()方法,唯一解释是t2线程没有执行到t1的join()方法,那这段语句是t1跟t2两个线程是并列的吗?
  29.                 若是请问怎样能够让t2执行到t1呢?
  30.                 */

  31.                 for (int x = 0;x < 90 ;x++ )
  32.                 {
  33.                         System.out.println(Thread.currentThread().getName()+"---"+x);
  34.                 }
  35.                 System.out.println("over");
  36.         }
  37. }
复制代码
求解,多谢。

评分

参与人数 1技术分 +1 收起 理由
蒋映辉 + 1

查看全部评分

2 个回复

倒序浏览
看一下运行结果自己应该能想到嘛
t1和t2只有那个for循环的输出语句
而t1.join()是main线程调用的
所以t1和t2并发打印输出语句
等t1打印完毕后,主线程打印输出语句

如果要让t2执行t1.join(),方法也很多
提供一种:

  1. class Demo implements Runnable{
  2.         Thread other=null;

  3.         public Demo(Thread other){
  4.                 this.other=other;
  5.         }

  6.         public Demo(){

  7.         }

  8.         public void run(){
  9.                 if(other!=null)
  10.                         try{
  11.                                 other.join();
  12.                         }catch(InterruptedException e){
  13.                                 e.printStackTrace();
  14.                         }
  15.                 for(int x=0;x<70;x++){
  16.                         System.out.println(Thread.currentThread().getName()+"-----"+x);
  17.                         // Thread.yield();
  18.                 }
  19.         }
  20. }

  21. public class JoinDemo{
  22.         public static void main(String[] args) throws Exception{
  23.                 // Demo d = new Demo();

  24.                 Thread t1=new Thread(new Demo());
  25.                 Thread t2=new Thread(new Demo(t1));//t2 输出前会执行t1.join()

  26.                 t1.start();
  27.                 t2.start();

  28.                  t2.join();
  29.                 /*
  30.                  然后main线程等待t2线程.
  31.                  最后顺序就为t1---t2---main
  32.                  */

  33.                 for(int x=0;x<90;x++){
  34.                         System.out.println(Thread.currentThread().getName()+"---"+x);
  35.                 }
  36.                 System.out.println("over");
  37.         }
  38. }
复制代码
回复 使用道具 举报
呃。。其实这么说吧,这是cpu的执行权决定的,还没读到ti.join(),  t1 和t2都执行完了,建议这样改进
  t1.start();
t1.join();
  t2.start();
               
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马