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
,但没有得到上述输出。
你可以调整格式以删除空行吗? – 2012-08-09 15:43:17
你的代码不会编译(缺少catch块,thread1声明两次)。请发布一个工作示例。 – assylias 2012-08-09 15:46:13
那么你会得到什么? – 2012-08-09 15:48:30