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);
}
}
}
主类.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();
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
但是没有把上面的东西说出来。