Hi I want to get rid of an infinite loop which holds a worker thread in a service.
I try to use the ReentrantLock() methods but I can't seem to get it to work.
My worker thread calls from JNI the nativeWaitForTask method and goes to sleep.
But my service main thread can't seem to wake him up in the submitTask method where he should signal the worker thread.
It's like the await() call blocked the service's main thread aswell.
But they are on different threads as far as I know...
What did I do wrong? Why doesn't it work?
private synchronized void nativeWaitForTask() {
threadLock.lock();
try {
while(opcode == SR_Manager.STATE_IDLE) {
newTask.await();
}
}catch (InterruptedException e) {
e.printStackTrace();
Log.e(SR_Manager.DEBUG_TAG,"Failed to wait for new task! " + e.toString());
}finally {
threadLock.unlock();
}
}
private synchronized void submitTask() {
if (nativeMain.isAlive()) {
dstImageName = sr.getDstImageName();
if (sr.getSrcImagePath() != null && !sr.getSrcImagePath().equals(srcImagePath)) {
srcImagePath = sr.getSrcImagePath();
width = sr.getWidth();
height = sr.getHeight();
format = sr.getFormat();
algorithm = sr.getAlgorithm();
value = sr.getValue();
saveOutput = sr.getSaveOutput();
opcode = SR_Manager.STATE_LOAD_IMAGE;
} else if (sr.getOpcode() != SR_Manager.STATE_LOAD_IMAGE) {
algorithm = sr.getAlgorithm();
value = sr.getValue();
saveOutput = sr.getSaveOutput();
opcode = sr.getOpcode();
}
t0 = System.currentTimeMillis();
} else {
// No native thread no service...
silentCrash(-100);
}
// Wake the worker thread which waits for the new task condition
threadLock.lock();
newTask.signal();
threadLock.unlock();
}