Holder threadHolderA = new Holder<>();
Holder threadHolderB = new Holder<>();
threadHolderA.value = new Thread(() -> {
while (true){
System.out.println("A");
LockSupport.unpark(threadHolderB.value);
LockSupport.park();
}
});
threadHolderB.value = new Thread(()->{
try {
Thread.sleep(10);
} catch (Exception ex) {
}
while (true){
System.out.println("B");
LockSupport.unpark(threadHolderA.value);
LockSupport.park();
}
});
threadHolderA.value.start();
threadHolderB.value.start();
打印结果:
A
B
A
B
A
B
A
B
A
B
A
B