I'm having a problem with using threads in Java (I have little experience with threads in Java, but much in C++, so i understand basic concept of threads). I've used example code for threads in Java, and the code is next:
ExecutorService executor = Executors.newFixedThreadPool(machines.size());
for (Machine m : machines) {
Runnable worker = new restartMachine(m.dataformachine());
executor.execute(worker);
}
executor.shutdown();
try {
executor.awaitTermination(15, TimeUnit.MINUTES);
} catch (InterruptedException e) {
e.printStackTrace();
}
restartMachine()
is restarting some remote machines, and machines are not connected in any way, data that are being passed to Runnable are IP address for given machine, and command that are executing then locally on that machine.
Error that I'm getting on execution of this piece of code is next:
java.lang.IllegalMonitorStateException
at java.util.concurrent.locks.ReentrantLock$Sync.tryRelease(ReentrantLock.java:155)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.release(AbstractQueuedSynchronizer.java:1260)
at java.util.concurrent.locks.ReentrantLock.unlock(ReentrantLock.java:460)
at java.util.concurrent.ThreadPoolExecutor.awaitTermination(ThreadPoolExecutor.java:1471)
Exception is thrown on calling of function awaitTermination() from code above. As I understand, and from various examples that I've seen there shouldn't be any problems with this code.
public boolean awaitTermination(long timeout, TimeUnit unit)
throws InterruptedException {
long nanos = unit.toNanos(timeout);
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
for (;;) {
if (runStateAtLeast(ctl.get(), TERMINATED))
return true;
if (nanos <= 0)
return false;
nanos = termination.awaitNanos(nanos);
}
} finally {
mainLock.unlock();
}
}
Trace indicate that error is in calling of function mainLock.unlock(); but as I understand it only main thread is going to execute that line, so I don't know why am I getting IllegalMonitorStateException, and there is no other code regarding threads in program (so I'm basically only using code from library)
I would appreciate any help, I know that there are many questions already answered regarding this problem (this exception), but I don't know what is the problem here.