Java 如何等待当前线程执行并完全执行另一个线程
所需输出:Java 如何等待当前线程执行并完全执行另一个线程,java,Java,所需输出: 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();
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
如果最后两行是thread1的另一种方式,则thread1将在thread2启动之前运行到完成状态。对
thread1.join()
的调用将一直等到thread1
完成它正在执行的操作。因此,您的两个线程将一个接一个地运行。查看该类。它的类javadoc有一个协调线程的示例。这里是我提出的,任何建议/改进/错误实践
thread1.start();
Thread.sleep(3000);
thread2.start();
thread1.join();
类Thread1.java
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
}
}
}
问候你能调整格式化来删除空白行吗?PLS?你的代码没有编译(缺少catch块,thule1声明两次……)。请发布一个工作示例。@NathanHughes也许他们试图理解事情。你真正想要什么?是否希望THEAD1和THEAD2的输出散布,或者希望THEAD2完全在线程1的中间运行?当你说“所需输出”时,我想你的意思并不完全是这样,因为如果没有更多的协调,几乎不可能保证这种输出。我想他是说他没有得到输出,但想得到。与其发布答案,不如编辑你原来的问题。这将使评论变得更加简单。
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
}
}
}
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
}
}
}
}
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