private static volatile AtomicInteger blockClientCount = new AtomicInteger(0);
private static Object lock = new Object();
private static Lock reentrantLock = new ReentrantLock();
private static Condition condition = reentrantLock.newCondition();
@Override
public void run() {
Random random = new Random(this.hashCode());
while (true) {
String request = random.nextInt(10) + "" + IOUtil.operators[random.nextInt(4)] + (random.nextInt(10) + 1);
System.out.println(this.getName() + " send request:" + request);
if (socketConnect()) {
BufferedReader in = null;
PrintWriter out = null;
try {
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
blockClientCount.incrementAndGet();
reentrantLock.lock();
try {
while (blockClientCount.get() % IOUtil.CLIENNT_NUM != 0) {
/**
* TODO java.lang.IllegalMonitorStateException
*/
condition.wait();
}
condition.signalAll();
} finally {
reentrantLock.unlock();
}
// synchronized (lock) {
// while (blockClientCount.get() % IOUtil.CLIENNT_NUM != 0)
// {
// lock.wait();
// }
// lock.notifyAll();
// }
out.println(request);
String response = in.readLine();
System.out.println(this.getName() + " accept response:" + response);
} catch (final Exception e) {
e.printStackTrace();
} finally {
IOUtil.close(in);
IOUtil.close(out);
}
}
try {
sleep(1000);
} catch (InterruptedException e) {
System.out.println(this.getName() + " was interrupted and stop");
IOUtil.close(socket);
break;
}
}
}
die java.lang.IllegalMonitorStateException wird passieren, wenn ich ReentrantLock verwenden zu warten, aber es funktioniert gut, wenn ich synchronisiert Gebrauch, ich can'not es erklären. Der Grund ist, dass reentrantLock vor condition.wait() entsperrt wird, aber ich weiß nicht warum. Wenn mir jemand helfen kann, muss ich den Code an https://github.com/shanhm1991/demo_io.git sendenjava.lang.IllegalMonitorStateException wenn condition.wait
Mögliches Duplikat von [IllegalMonitorStateException on wait() Anruf] (https://stackoverflow.com/questions/1537116/illegalmonitorstatexception-on-wait-call) –
@VinceEmigh Das ist kein Duplikat dieses speziellen Problems, siehe die akzeptierte Antwort. –