I was trying to test guava concurrent package, as below.
I expect that the created thread pool will execute the "Runnable" class instance and then wait for termination.
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import java.util.concurrent.*;
public class MyTest {
public static void main(String [] args) {
final CountDownLatch latch = new CountDownLatch(2);
ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("UseCountDownLatch").build();
ThreadPoolExecutor executor = new ThreadPoolExecutor(
10,
10,
100,
TimeUnit.SECONDS,
new ArrayBlockingQueue<>(1),
namedThreadFactory);
executor.execute(new Runnable() {
@Override
public void run() {
latch.countDown();
}
});
try {
executor.wait();
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
But in fact, it prints:
Exception in thread "main" java.lang.IllegalMonitorStateException
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:502)
at UseCountDownLatch.main(MyTest.java:28)
If I remove the line of
executor.wait();
Then program hangs there, without stopping. So where did I get wrong to gracefully execute and finish the task in ThreadPool? How to fix it?
Thanks a lot.