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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© adamjy 中级黑马   /  2014-4-21 18:59  /  812 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 adamjy 于 2014-4-21 20:30 编辑

代码如下
  1. public class ThreadTest extends Thread{
  2.         private String name ;
  3.     static int num = 0 ;
  4.      
  5.     public ThreadTest(String name)
  6.     {
  7.         this.name = name ;
  8.     }
  9.      
  10.     public String getThreadName()
  11.     {
  12.         return this.name;
  13.     }
  14.     public void run ()
  15.     {
  16.         for ( ;num < 10; num++ )
  17.         {
  18.             System.out.println(num);
  19.             try{
  20.                 Thread.sleep(200);
  21.             }
  22.             catch(InterruptedException e){
  23.                 System.out.println("interrupt");
  24.             }
  25.                      
  26.         }
  27.          
  28.         System.out.println(this.getThreadName()+" over");
  29.         this.interrupt();
  30.             
  31.     }
  32.      
  33.     public static void main(String[] args) {
  34.          
  35.             ThreadTest a = new ThreadTest("Thread a");
  36.             ThreadTest b = new ThreadTest("Thread b");
  37.          
  38.         a.start();
  39.         // 待num的值增加到4的时候,启动线程b
  40.         // 为什么不行?
  41.         while (true)
  42.         {
  43.             if ( num > 4 && !b.isAlive())
  44.                 b.start();
  45.         }
  46.     }
  47. }
复制代码


请问,为什么main()方法中的Thread b没有启动起来?

评分

参与人数 1技术分 +1 收起 理由
ily521125 + 1

查看全部评分

3 个回复

倒序浏览
因为在你a线程还没执行到num大于4的时候,主线程就已经把下面的代码执行了。
回复 使用道具 举报
貌似是主线程无限死循环的原因吧
我只加了个1毫秒延迟 b就出来了

  1.                 while (true) {
  2.                         if (num > 4 && !b.isAlive()) {
  3.                                 b.start();
  4.                         }
  5.                         try {
  6.                                 Thread.sleep(1);
  7.                         }
  8.                         catch (InterruptedException e) {
  9.                                 // TODO Auto-generated catch block
  10.                                 e.printStackTrace();
  11.                         }
  12.                 }
复制代码
回复 使用道具 举报
  1. public class ThreadTest extends Thread{
  2.         private String name ;
  3.     static int num = 0 ;
  4.      
  5.     public ThreadTest(String name)
  6.     {
  7.         this.name = name ;
  8.     }
  9.      
  10.     public String getThreadName()
  11.     {
  12.         return this.name;
  13.     }
  14.     public void run ()
  15.     {
  16.         for ( ;num < 10; num++ )
  17.         {
  18.             System.out.println(Thread.currentThread().getName()+"--"+num);//这里我改了下方便看线程启动
  19.             try{
  20.                 Thread.sleep(200);
  21.             }
  22.             catch(InterruptedException e){
  23.                 System.out.println("interrupt");
  24.             }
  25.                      
  26.         }
  27.          
  28.         System.out.println(this.getThreadName()+" over");
  29.         this.interrupt();
  30.             
  31.     }
  32.      
  33.     public static void main(String[] args) {
  34.          
  35.             ThreadTest a = new ThreadTest("Thread a");
  36.             ThreadTest b = new ThreadTest("Thread b");
  37.          
  38.         a.start();
  39.         // 待num的值增加到4的时候,启动线程b
  40.         // 为什么不行?
  41.         while (true)
  42.         {
  43.             if ( num > 4 && !b.isAlive())
  44.                 b.start();
  45.                         if (num>=10)
  46.                         {
  47.                                 break;
  48.                         }
  49.         }
  50.     }
  51. }
复制代码
因为下面的while循环的问题,当num>=10的时候,while里面的值是true,当你run方法里interrupt方法调用之后,b线程就中断了,因为是true,还会启动b.start,一个线程被启动两次就会报非法线程声明异常,所以,线程b有启动,只是程序没有正常关闭而已,所以加一个判断条件就好了!!代码帮你改过了
回复 使用道具 举报 1 0
您需要登录后才可以回帖 登录 | 加入黑马