A
Ake L
Someone knows what's wrong with this code? When I run it, sometimes all
threads finishes, sometimes not. Why?
When all is OK I get the following output:
Main thread enters sleep...
pool-1-thread-1 enters await...
pool-1-thread-4 enters await...
pool-1-thread-5 enters await...
pool-1-thread-3 enters await...
pool-1-thread-2 enters await...
Main thread woke up from sleep
Signalling all...
pool-1-thread-1 woke up from await
pool-1-thread-1 releases the lock
pool-1-thread-4 woke up from await
pool-1-thread-4 releases the lock
pool-1-thread-5 woke up from await
pool-1-thread-5 releases the lock
pool-1-thread-3 woke up from await
pool-1-thread-3 releases the lock
pool-1-thread-2 woke up from await
pool-1-thread-2 releases the lock
Terminated.
When it goes wrong, the code could hang after for instance
"pool-1-thread-4 releases the lock".
Here's the code:
------------------------
public class TestMain {
public static void main(String[] args) throws InterruptedException
{
ExecutorService executor = Executors.newCachedThreadPool();
CompletionService<Object> completionService = new
ExecutorCompletionService<Object>(executor);
for (int i = 0; i < 5; i++) {
completionService.submit(new TestThread());
}
System.out.println("Main thread enters sleep...");
Thread.sleep(1000);
System.out.println("Main thread woke up from sleep");
ALockingClass.getInstance().signal();
for (int i = 0; i < 5; i++) {
completionService.take();
}
executor.shutdown();
System.out.println("Terminated.");
}
}
------------------------
public class TestThread implements Callable<Object> {
public Object call() throws Exception {
ALockingClass.getInstance().waitForCondition();
return null;
}
}
------------------------
public class ALockingClass {
private static ALockingClass instance = null;
private final Lock lock = new ReentrantLock();
private final Condition aCondition = lock.newCondition();
public static ALockingClass getInstance() {
if (instance == null) {
instance = new ALockingClass();
}
return instance;
}
public void waitForCondition() throws InterruptedException {
lock.lock();
try {
System.out.println(Thread.currentThread().getName() + "
enters await...");
aCondition.await();
System.out.println(Thread.currentThread().getName() + "
woke up from await");
}
finally {
System.out.println(Thread.currentThread().getName() + "
releases the lock");
lock.unlock();
}
}
public void signal() {
lock.lock();
try {
System.out.println("Signalling all...");
aCondition.signalAll();
}
finally {
lock.unlock();
}
}
}
------------------------
threads finishes, sometimes not. Why?
When all is OK I get the following output:
Main thread enters sleep...
pool-1-thread-1 enters await...
pool-1-thread-4 enters await...
pool-1-thread-5 enters await...
pool-1-thread-3 enters await...
pool-1-thread-2 enters await...
Main thread woke up from sleep
Signalling all...
pool-1-thread-1 woke up from await
pool-1-thread-1 releases the lock
pool-1-thread-4 woke up from await
pool-1-thread-4 releases the lock
pool-1-thread-5 woke up from await
pool-1-thread-5 releases the lock
pool-1-thread-3 woke up from await
pool-1-thread-3 releases the lock
pool-1-thread-2 woke up from await
pool-1-thread-2 releases the lock
Terminated.
When it goes wrong, the code could hang after for instance
"pool-1-thread-4 releases the lock".
Here's the code:
------------------------
public class TestMain {
public static void main(String[] args) throws InterruptedException
{
ExecutorService executor = Executors.newCachedThreadPool();
CompletionService<Object> completionService = new
ExecutorCompletionService<Object>(executor);
for (int i = 0; i < 5; i++) {
completionService.submit(new TestThread());
}
System.out.println("Main thread enters sleep...");
Thread.sleep(1000);
System.out.println("Main thread woke up from sleep");
ALockingClass.getInstance().signal();
for (int i = 0; i < 5; i++) {
completionService.take();
}
executor.shutdown();
System.out.println("Terminated.");
}
}
------------------------
public class TestThread implements Callable<Object> {
public Object call() throws Exception {
ALockingClass.getInstance().waitForCondition();
return null;
}
}
------------------------
public class ALockingClass {
private static ALockingClass instance = null;
private final Lock lock = new ReentrantLock();
private final Condition aCondition = lock.newCondition();
public static ALockingClass getInstance() {
if (instance == null) {
instance = new ALockingClass();
}
return instance;
}
public void waitForCondition() throws InterruptedException {
lock.lock();
try {
System.out.println(Thread.currentThread().getName() + "
enters await...");
aCondition.await();
System.out.println(Thread.currentThread().getName() + "
woke up from await");
}
finally {
System.out.println(Thread.currentThread().getName() + "
releases the lock");
lock.unlock();
}
}
public void signal() {
lock.lock();
try {
System.out.println("Signalling all...");
aCondition.signalAll();
}
finally {
lock.unlock();
}
}
}
------------------------