本帖最后由 adamjy 于 2014-4-21 20:30 编辑
代码如下
- public class ThreadTest extends Thread{
- private String name ;
- static int num = 0 ;
-
- public ThreadTest(String name)
- {
- this.name = name ;
- }
-
- public String getThreadName()
- {
- return this.name;
- }
- public void run ()
- {
- for ( ;num < 10; num++ )
- {
- System.out.println(num);
- try{
- Thread.sleep(200);
- }
- catch(InterruptedException e){
- System.out.println("interrupt");
- }
-
- }
-
- System.out.println(this.getThreadName()+" over");
- this.interrupt();
-
- }
-
- public static void main(String[] args) {
-
- ThreadTest a = new ThreadTest("Thread a");
- ThreadTest b = new ThreadTest("Thread b");
-
- a.start();
- // 待num的值增加到4的时候,启动线程b
- // 为什么不行?
- while (true)
- {
- if ( num > 4 && !b.isAlive())
- b.start();
- }
- }
- }
复制代码
请问,为什么main()方法中的Thread b没有启动起来? |