How does java.util.concurrent.Executor work?

How can java.util.concurrent.Executor create a "real" thread? Suppose that I am executing an Executor or using any executor service (for example, ThreadPoolExecutor). How does the JVM work?

+4
source share
2 answers

It calls ThreadFactory . Take a look at the Executors class. Note that they all have an overloaded argument, where you can provide an implementation of ThreadFactory . ThreadFactory interface basically

 public Thread newThread(Runnable runnable); 

and the default implementation, if not supplied, is basically just return new Thread(runnable);

Why redefine this - well, it is very useful for customizing the topic name and daemon status, among other things.

+3
source

Contractor - a ready-made flow control interface.

Depending on the type of artist, it creates one or more threads. After completing the flow, the performer of his task stops them or leaves the work. You may also have an executor performing scheduled tasks (for example, every minute). This is a good alternative for creating the many (often thousands of threads) that are needed for just five seconds or the many threads that have been used since time.

If you specify the number of threads for creating and sending more tasks than the number of threads, all other Runnable objects will be queued before they are rotated. No JVM magic here, only Java code.

0
source

All Articles