2009-11-27 120 views
2

我有2个嵌套的线程。在一些子线程完成之前,如何让一个Java线程返回?

第一个线程启动第二个线程的多个实例。每个第二线程必须睡眠一段时间(5秒)。

我想开始第一个线程并立即向用户返回一条消息,但似乎我的第一个线程一直等到第二个线程的所有子节点完成。

我该如何做到这一点?任何帮助?

+2

您正在使用的代码会可能有帮助。 – 2009-11-27 00:41:11

+2

你在调用'Thread.run'而不是'Thread.start'吗?这是一个非常常见的错误(并且是'Thread'的糟糕设计的结果)。 – 2009-11-27 00:47:40

+0

嗨汤姆, 感谢您的答复。 这是我的代码的外观。 FirstThread.run(); 在第一个线程的运行方法中,我运行了多个第二个线程,如下所示: SecondThread.run(); SecondThread.sleep(5000); 我希望我的第一个线程立即返回,而不是等待所有第二个线程子节点完成。 你建议在第一个线程或第二个线程上使用'start'? 再次感谢。 – Jani 2009-11-27 01:01:28

回答

0

查看代码可能会有帮助。这取决于你在哪里放Thread.sleep();

7

处理java.lang.Thread时有一些常见错误。

  • 在线程上调用run而不是start。这对于run方法来说并不神奇。
  • 在线程实例上调用静态方法。不幸的是,这编译。一个常见的例子是Thread.sleepsleep是一种静态方法,即使代码似乎在不同的线程上调用它,它也会始终睡眠当前线程。

不是直接处理线程,而是使用java.util.concurrent的线程池。

0

像其他人一样,用线程指出你调用start()这是一个非阻塞的调用,并且实际上获得了线程的滚动。调用run()会阻塞,直到run()方法结束。请参阅下面的示例代码;

public class Application { 

    public static void main(String[] args) { 
     FirstThread firstThread = new FirstThread(); 
     firstThread.start(); 
     System.out.println("Main Method ending"); 
    } 
} 


public class FirstThread extends Thread { 

    public void run() { 
     for(int i = 0; i < 3; i++) { 
      SecondThread secondThread = new SecondThread(i); 
      secondThread.start(); 
     } 
     System.out.println("FirstThread is finishing"); 
    } 
} 

public class SecondThread extends Thread { 

    private int i; 

    public SecondThread(int i) { 
     this.i = i; 
    } 

    public void run() { 

     while(true) { 
      System.out.println("Second thread number " + i + " doing stuff here..."); 
      // Do stuff here... 

      try { 
       Thread.sleep(5000); 
      } 
      catch(InterruptedException ex){ 
       //ignore for sleeping} 
      } 
     } 
    } 
} 

它产生的输出:

Main Method ending 
Second thread number 0 doing stuff here... 
Second thread number 1 doing stuff here... 
FirstThread is finishing 
Second thread number 2 doing stuff here... 
Second thread number 0 doing stuff here... 
Second thread number 2 doing stuff here... 
Second thread number 1 doing stuff here... 
+0

顺便说一句 - 我怀疑你实际上并不需要一个名为“FirstThread”的类 - 你可能只需从主线程启动SecondThread实例即可。 – 2009-11-27 01:49:24

1

你应该做的,是通过Executors.newCachedThreadPool()创建一个单独的线程池。从您的主线程submit可运行(任务)到池中。返回主线程Future s的列表。

在Java中,有足够的框架代码,很少需要直接处理线程。

0

我在第一个线程和第二个线程中都用'start'替换了'run'。 现在工作正常。

感谢所有回答有价值的建议。

-1

可以使用Synchronized关键字将用于运行一个线程完全

例如

public synchronized void run() //which thread to run completely 
{ 
} 
0
public class FirstThread extends Thread { 

    public synchronized void run() { 
     for(int i = 0; i < 3; i++) { 
       System.out.println("i="+i); 
     } 
     System.out.println("FirstThread is finishing"); 
    } 
} 

public class SecondThread extends Thread { 

    public synchronized void run() { 
     for(int j = 0; j < 3; i++) { 
       System.out.println("j="+j); 
     } 
     System.out.println("Second Thread is finishing"); 
    } 
} 

public class Application { 

    public static void main(String[] args) { 
     FirstThread firstThread = new FirstThread(); 
     SecondThread a=new SecondThread() 
     firstThread.start(); 
     a.start(); 

    } 
} 

输出将是:

i=0 
i=1 
i=2 
i=3 
FirstThread is finishing 

j=0 
j=1 
j=2 
j=3 
Second Thread is finishing 
相关问题