目的是想用ReentrantLock Condition 按数组 顺序输出;但好像一直锁等待,不明白为什么,麻烦大神解惑,感谢
private static ReentrantLock lock = new ReentrantLock();
private static Condition c = lock.newCondition();
private static Condition c2 = lock.newCondition();
volatile static int m = 0;
public static void main(String[] args) throws Exception {
int[] arr = {1,2,3,4,5,6,7,8,9,0};
for(int i = 0;i<arr.length;i++) {
final int n = arr[i];
new Callable<String>() {
@Override
public String call() throws Exception {
String rt = "";
try {
lock.lock();
if(n != m) {
System.out.println(m+" wait");
c.await();
rt+="--wait--";
}
System.out.println(Thread.currentThread().getName()+"-------run-----"+n);
c.signalAll();
rt+="--ok";
m++;
return rt;
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
lock.unlock();
}
return null;
}
}.call();
}
}
建议写一些调试的打印语句,跟踪代码运行状态。
你的代码和你想要完成的效果完全不同
package Condition;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
class RunnableThread implements Runnable {
Lock lock;
Condition condition1;
Condition condition2;
Condition condition3;
boolean isOnePrint = false;
boolean isTwoPrint = false;
public RunnableThread(Lock lock, Condition condition1, Condition condition2, Condition condition3) {
this.lock = lock;
this.condition1 = condition1;
this.condition2 = condition2;
this.condition3 = condition3;
}
@Override
public void run() {
if ("Thread-1".equals(Thread.currentThread().getName())) {
methodForT1();
} else if ("Thread-2".equals(Thread.currentThread().getName())) {
methodForT2();
} else {
methodForT3();
}
}
public void methodForT1() {
lock.lock();
try {
for (int i = 1; i <= 15; i += 3) {
if (!isOnePrint) {
condition2.signalAll();
System.out.println(Thread.currentThread().getName() + " Print = " + i);
Thread.sleep(1000);
isOnePrint = true;
} else {
i = i - 3;
condition1.await();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void methodForT2() {
lock.lock();
try {
for (int i = 2; i <= 15; i += 3) {
if (isOnePrint) {
condition3.signalAll();
System.out.println(Thread.currentThread().getName() + " Print = " + i);
Thread.sleep(1000);
isOnePrint = false;
isTwoPrint = true;
} else {
i = i - 3;
condition2.await();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void methodForT3() {
lock.lock();
try {
for (int i = 3; i <= 15; i += 3) {
if (isTwoPrint) {
condition1.signalAll();
System.out.println(Thread.currentThread().getName() + " Print = " + i);
Thread.sleep(1000);
isOnePrint = false;
isTwoPrint = false;
} else {
i = i - 3;
condition3.await();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
public class LockConditionTest {
public static void main(String[] args) {
Lock lock = new ReentrantLock();
Condition condition1 = lock.newCondition();
Condition condition2 = lock.newCondition();
Condition condition3 = lock.newCondition();
RunnableThread runnableObj = new RunnableThread(lock, condition1, condition2, condition3);
Thread t1 = new Thread(runnableObj, "Thread-1");
Thread t2 = new Thread(runnableObj, "Thread-2");
Thread t3 = new Thread(runnableObj, "Thread-3");
t2.start();
t1.start();
t3.start();
}
}
改了下代码,之后好了,多谢各位
new Thread(new Runnable() {
@Override
public void run() {
String rt = "";
try {
lock.lock();
while(n != m) {
System.out.println(n+" wait");
rt+="--wait--";
Thread.sleep(100);
c.await();
}
System.out.println(m+"--------"+n+" ---"+(m==n));
System.out.println(Thread.currentThread().getName()+"-------run-----"+n);
Thread.sleep(100);
c.signalAll();
rt+="--ok";
m++;
System.out.println(m);
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
lock.unlock();
}
}
}).start();
您好,我是有问必答小助手,你的问题已经有小伙伴为您解答了问题,您看下是否解决了您的问题,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632
非常感谢您使用有问必答服务,为了后续更快速的帮您解决问题,现诚邀您参与有问必答体验反馈。您的建议将会运用到我们的产品优化中,希望能得到您的支持与协助!
速戳参与调研>>>https://t.csdnimg.cn/Kf0y