2012-08-09 87 views
2
public class Thread1 extends Thread { 
    public void run() { 
     testFun1(); 
    } 

    public void testFun1(){ 
      for(int i=1;i<10;i++){ 
       try { 
        Thread.sleep(1000); 
         System.out.println("From testFun1() = "+i); 
       } catch (Exception e) { 
      } 

     } 

     } 

    } 

class Thread2 extends Thread { 
    public void run(){ 
     testFun2(); 
    } 

    public synchronized void testFun2() { 
     try { 
       for(int i=20;i<=25;i++){ 
       Thread.sleep(1000); 
       System.out.println("From testFun2() = "+i);   
     } 
     } 
    } 

MainClass.java如何等待当前线程执行,并执行其他线程completedly

public class MainClass{ 
    public static void main(String[] args) { 
     try{ 
     Thread1 thread1 = new Thread1(); 
     Thread2 thread1 = new Thread2(); 
     thread1.start(); 
     Thread.sleep(3000); 
     thread1.join(); 
     thread2.start(); 
    }catch(Exception e){ 
    } 

    } 
} 

需要的输出:

From testFun1() 1 
From testFun1() 2 
From testFun1() 3 
From testFun2() 20 
From testFun2() 21 
From testFun2() 22 
From testFun2() 23 
From testFun2() 24 
From testFun2() 25 
From testFun1() 4 
From testFun1() 5 
From testFun1() 6 
From testFun1() 7 
From testFun1() 8 
From testFun1() 9 
From testFun1() 10 

,但没有得到上述输出。

+1

你可以调整格式以删除空行吗? – 2012-08-09 15:43:17

+1

你的代码不会编译(缺少catch块,thread1声明两次)。请发布一个工作示例。 – assylias 2012-08-09 15:46:13

+1

那么你会得到什么? – 2012-08-09 15:48:30

回答

0

你的输出显示你真正拥有的是

thread1.start(); 

Thread.sleep(3000); 

thread2.start(); 

thread1.join(); 

如果最后两行分别围绕线程1将运行至完成线程2开始前的其他方式。

+0

我想他是说他没有得到那个输出,但愿意。 – assylias 2012-08-09 15:47:34

1

您致电thread1.join()将等到thread1完成它正在做的事情。所以你的2个线程将一个接一个地运行。

0

这是我想出的,please comment任何建议/改进/不良做法。

public class App { 

    public static void main(String[] args) { 
     Thread1 thread1 = new Thread1(); 
     Thread2 thread2 = new Thread2(); 

     try { 
      thread1.start(); 
      Thread.sleep(500); 
      synchronized (thread1) { 
       thread1.waiting = true; 
       thread2.start(); 
       thread2.join(); 
       thread1.waiting = false; 
       thread1.notify(); 
      } 
     } catch (Exception e) { 
      //TODO actually handle exception 
     } 
    } 
} 

类Thread1.java

public class Thread1 extends Thread { 
    boolean waiting = false; 

    public void run() { 
     testFun1(); 
    } 

    public void testFun1() { 
     for (int i = 1; i < 10; i++) { 
      synchronized (this) { 
       while (waiting) { 
        try { 
         wait(); 
        } catch (Exception e) { 
         //TODO Handle exception 
        } 
       } 
      } 
      try { 
       Thread.sleep(100); 
       System.out.println("From testFun1() = " + i); 
      } catch (Exception e) { 
       //TODO Handle exception 

      } 
     } 
    } 
} 

类Thread2.java

public class Thread2 extends Thread { 

    public void run() { 
     testFun2(); 
    } 

    public void testFun2() { 
     try { 
      for (int i = 20; i <= 25; i++) { 
       try { 
        Thread.sleep(100); 
       } catch (InterruptedException e) { 
        //catch 
       } 
       System.out.println("From testFun2() = " + i); 
      } 

     } catch (Exception e) { 
      //TODO do something 
     } 

    } 
} 

输出:

C:\Java\jdk1.6.0_25\bin\java -Didea.launcher.port=7543 "-Didea.launcher.bin.path=C:\Program Files\JetBrains\IntelliJ IDEA 10.5.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Java\jdk1.6.0_25\jre\lib\alt-rt.jar;C:\Java\jdk1.6.0_25\jre\lib\alt-string.jar;C:\Java\jdk1.6.0_25\jre\lib\charsets.jar;C:\Java\jdk1.6.0_25\jre\lib\deploy.jar;C:\Java\jdk1.6.0_25\jre\lib\javaws.jar;C:\Java\jdk1.6.0_25\jre\lib\jce.jar;C:\Java\jdk1.6.0_25\jre\lib\jsse.jar;C:\Java\jdk1.6.0_25\jre\lib\management-agent.jar;C:\Java\jdk1.6.0_25\jre\lib\plugin.jar;C:\Java\jdk1.6.0_25\jre\lib\resources.jar;C:\Java\jdk1.6.0_25\jre\lib\rt.jar;C:\Java\jdk1.6.0_25\jre\lib\ext\dnsns.jar;C:\Java\jdk1.6.0_25\jre\lib\ext\localedata.jar;C:\Java\jdk1.6.0_25\jre\lib\ext\sunjce_provider.jar;C:\Java\jdk1.6.0_25\jre\lib\ext\sunmscapi.jar;C:\Java\jdk1.6.0_25\jre\lib\ext\sunpkcs11.jar;C:\IdeaProjects\PracticeModule\target\classes;C:\Program Files\JetBrains\IntelliJ IDEA 10.5.1\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain jmhp.core.App 
From testFun1() = 1 
From testFun1() = 2 
From testFun1() = 3 
From testFun1() = 4 
From testFun1() = 5 
From testFun2() = 20 
From testFun2() = 21 
From testFun2() = 22 
From testFun2() = 23 
From testFun2() = 24 
From testFun2() = 25 
From testFun1() = 6 
From testFun1() = 7 
From testFun1() = 8 
From testFun1() = 9 

商祺!

+0

而不是发布答案,编辑您的原始问题。这会使评论变得更简单。 – 2012-08-10 08:19:53

+0

@Aaron我不是OP。 :) – jhurtado 2012-08-10 14:48:56

相关问题