I'm trying to synchronize 3 threads. Each one of them handles a caterpillar that is moving along it's own path.
Unfortunately their paths are crossed like this:
To achieve this goal I'm using locks. There are two sections where blocks are shared: horizontal and vertical (I named it above and below). The problem is that a thread at one point wants to be in two shared sections at the same time.
My question is:
Can I unlock a lock inside try/finally clause using a flag?
I mean, when a thread unlocks a lock inside a try/finally clause, a flag is set and in finally clause it checks if the flag is set, then doesn't unlock a second time. I think it is a safe solution but maybe I'm wrong.
Is there any other way to synchronize the threads?
This is my code: (It almost works)
public void moveForward() throws InterruptedException {
redIsUpofAbove();
int choose= 0;
if (Route.critcalSectionAbove.contains(head))
choose= 1;
if (Route.critcalSectionBelow.contains(head))
choose= 2;
switch (choose) {
case 1: {
boolean flag = true;
System.err.println(name + " Lock above");
synchronization.aboveLock.lock();
try {
takeNextGrid();
Thread.sleep(sleepValue);
while (isInAboveCriticalSection()) {
takeNextGrid();
Thread.sleep(sleepValue);
if (isInBelowCriticalSection()) {// Caterpillar is on two
// shared sections
System.out.println(name + " Lock below");
if (!synchronization.belowLock.tryLock()) {
synchronization.aboveLock.unlock();
System.out.println(name + " Unlock above");
synchronization.belowLock.lock();
flag = false;
}
try {
while (isInBelowCriticalSection()) {
takeNextGrid();
if (!isInAboveCriticalSection() && flag) {
synchronization.aboveLock.unlock();
flag = false;
System.out.println(name + " Unlock above");
}
Thread.sleep(sleepValue);
}
} catch (Exception e) {
} finally {
synchronization.belowLock.unlock();
System.err.println(name + "Unlock belovelock");
}
}
}
} catch (Exception e) {
} finally {
if (flag) {
synchronization.aboveLock.unlock();
System.out.println(name + " unlock above");
}
}
break;
}
case 2: {
boolean flag = true;
System.err.println(name + " Lock below");
synchronization.belowLock.lock();
try {
takeNextGrid();
Thread.sleep(sleepValue);
while (isInBelowCriticalSection()) {
takeNextGrid();
Thread.sleep(sleepValue);
if (isInAboveCriticalSection()) {
if (!synchronization.aboveLock.tryLock() && flag) {
synchronization.belowLock.unlock();
System.out.println(name + " Unlock below");
synchronization.aboveLock.lock();
flag = false;
}
try {
System.out.println(name + " Lock above");
while (isInAboveCriticalSection()) {
takeNextGrid();
if (!isInBelowCriticalSection() && flag == true) {
synchronization.belowLock.unlock();
flag = false;
System.out.println(name + " Lock below");
}
Thread.sleep(sleepValue);
}
} catch (Exception e) {
} finally {
synchronization.aboveLock.unlock();
System.err.println(name + "Lock abovelock");
}
}
}
} catch (Exception e) {
} finally {
if (flag) {
synchronization.belowLock.unlock();
System.out.println("Opuszczam belowLock");
}
}
break;
}
default: {
takeNextGrid();
break;
}
}
}
And the last question:
Is there a way to set a priority to wake up, on threads which are waiting on a locked lock?