起因
在使用java编写一个按顺序打印字符的多线程程序时,是正常运行的。同样的代码换成golang来编写却会死锁。下面我们来看代码:
java:
public class TestABC10 {
public static int seq = 1;
public static void main(String[] args) {
Lock lock = new ReentrantLock();
Condition conditionA = lock.newCondition();
Condition conditionB = lock.newCondition();
Condition conditionC = lock.newCondition();
new Thread(new Runnable() {
@Override
public void run() {
try{
for(int j =0;j < 10;j++){
lock.lock();
if(seq != 1){
conditionA.await();
}
for(int i =0;i < 10;i++){
System.out.print("A");
}
System.out.println();
seq = 2;
conditionB.signal();
}
}catch(Exception e){
}finally