Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does 'Thread termination due to failure' refer to?

The javadoc for ExecutorService sometimes refers to the case when a Thread terminates 'due to failure'. However, it is not clear what kind of failure does this refer to.

For instance, the single thread executor documentation says that

if this single thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks

I would have thought that this situation might happen in case of an Exception, or maybe a RuntimeException, but it does not seem to be the case. Running the following code seems to be giving the same thread name and thread ID.

ExecutorService executor = Executors.newSingleThreadExecutor();
executor.submit(() -> {
  System.out.println("Hello from " + Thread.currentThread().getName()+ " " + Thread.currentThread().getId());
  throw new NullPointerException("Test");
});

executor.submit(() -> {
  System.out.println("Hello 2 from " + Thread.currentThread().getName() + " " + Thread.currentThread().getId());
});

The output of this code is:

Hello from pool-1-thread-1 12
Hello 2 from pool-1-thread-1 12

It seems that the same thread is being reused even in the case of NullPointerException.

So what kind of 'failure' is the Javadoc referring to?

like image 978
jbx Avatar asked Nov 15 '15 21:11

jbx


People also ask

How a thread is terminated?

A thread automatically terminates when it returns from its entry-point routine. A thread can also explicitly terminate itself or terminate any other thread in the process, using a mechanism called cancelation.

When a process is terminated what happens to its thread?

The Terminated property is set from a different thread to signal to the worker thread that it should terminate. It's then up to the worker thread to obey the signal by checking the Terminated flag in the Execute procedure. After the Execute procedure is finished, the Thread's Finished property is automatically set.

Can a thread object be used to launch multiple threads?

Because Java threads run in the same memory space, they can easily communicate among themselves because an object in one thread can call a method in another thread without any overhead from the operating system. In this Tutorial we will learn how to do multi-threaded programming in Java.

How do you run a concurrent method in Java?

If you want call method concurrently you need threads. Java documentation for concurrency: https://docs.oracle.com/javase/tutorial/essential/concurrency/index.html. You have many options how to create/use threads in java.


1 Answers

This is an interesting question. Following the code in ThreadPoolExecutor the thread is discarded when a Runnable is passed to the execute() method.

When you call submit() the executor creates a wrapper for the callable/runnable of type FutureTask. FutureTask.run() has some logic to catch exceptions and store them (so then, you can query this from the Future). In this case, the exception never reaches the ThreadPool, so the thread is not discarded.

like image 52
Augusto Avatar answered Oct 26 '22 01:10

Augusto