I have a multithreaded Java program. The main thread executes the following code in a second thread, after which the second thread ends.
try{
System.out.println(1); //prints
doSomething();
System.out.println(2); //doesn't print
} catch(Throwable t) {
System.out.println(3); //doesn't print
}
I run this code and, on the rare occasion, I see 1
go to console, and 2
or 3
are absent. So it seems that doSomething
is throwing some kind of error to cause the thread to stop. This happens about 0.5% of the time. Since it's not a Throwable
that's causing the thread to terminate, what could possibly be going on?
The main thread keeps happily continuing after the second thread has stopped.
This is the code I'm using the create and run the second thread.
Runnable secondThread = new Runnable() { ... }
ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) Executors.newFixedThreadPool(1);
threadPoolExecutor.execute(secondThread);
EDIT: doSomething
is a call to a REST
API of an external website.