/**
* 并发测试
*/
@Test
public void test3() {
char a []= “abcdef”.toCharArray();
char b []= “123456”.toCharArray();
Object o = new Object();
CountDownLatch countDownLatch = new CountDownLatch(a.length);
new Thread(()->{
synchronized (o){
for(char x:a){
System.out.println(x);
o.notify();
try {
o.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
o.notify();
}
}).start();
new Thread(()->{
synchronized (o){
for(char x:b){
try {
countDownLatch.await();
System.out.println(x);
o.notify();
o.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
o.notify();
}
}).start();
}
最优解决方案:LockSupport