所以我正在尝试编写一个打印出以下输出的程序:
44
33
22
11
该程序应该是多线程的,并且必须使用锁来防止竞争条件。它还必须利用 Condition 以便当线程要打印的数字与变量 threadnum(必须打印的下一个数字)不对应时,它必须等待。我已经得到了大部分它,除了当我尝试运行它时我得到 IllegalMonitorStateExceptions 并且我不确定是什么导致它,也不知道如何修复它。我会很感激一些帮助。提前致谢。
public class Threadlocksrev implements Runnable {
Lock lock = new ReentrantLock();
Condition wrongNumber = lock.newCondition();
int i;
static int threadnum = 4;
public Threadlocksrev(int i){
this.i = i;
}
private int getI(){
return i;
}
@Override
public synchronized void run() {
lock.lock();
while(true){
if (threadnum == i){
try{
System.out.print(getI());
System.out.print(getI());
System.out.print("\n");
threadnum--;
wrongNumber.signalAll();
}
catch(Exception e){
e.printStackTrace();
}
finally{
lock.unlock();
}
}
else{
try {
wrongNumber.await();
}
catch (InterruptedException e) {
e.printStackTrace();
}
finally{
wrongNumber.signalAll();
lock.unlock();
}
}
}
}
}
主要类:
public class ThreadlocksrevInit {
private static final int max_threads = 4;
public static void main(String[] args) {
Threadlocksrev task1 = new Threadlocksrev(1);
Threadlocksrev task2 = new Threadlocksrev(2);
Threadlocksrev task3 = new Threadlocksrev(3);
Threadlocksrev task4 = new Threadlocksrev(4);
Thread thread1 = new Thread(task1);
Thread thread2 = new Thread(task2);
Thread thread3 = new Thread(task3);
Thread thread4 = new Thread(task4);
thread1.start();
thread2.start();
thread3.start();
thread4.start();
}
}