作者:Gemini强强Gemini | 来源:互联网 | 2024-09-28 15:26
publicclassJoinThreadextendsThread{staticThreadthreadAnull,threadBnull;publicstaticvoidmai
public class JoinThread extends Thread {
static Thread threadA = null,threadB = null;
public static void main(String[] args) throws InterruptedException {
char[] a = "1234567".toCharArray();
char[] b = "ABCDEFG".toCharArray();
threadA = new Thread(()->{
for (char c: a) {
System.out.println(c);
LockSupport.unpark(threadB); //叫醒threadB
LockSupport.park(); //threadA阻塞
}
}, "threadA");
threadB = new Thread(()->{
for (char c: b) {
LockSupport.park(); //threadB阻塞
System.out.println(c);
LockSupport.unpark(threadA); //叫醒threadB
}
}, "threadB");
threadA.start();
threadB.start();
}
}