Good afternoon.
There is a question for an interview like this.
How many ways to create threads do you know? The number 2 appears in the answers: either create
Thread
, passing it
Runnable
, or create an object of a descendant class
Thread
in which
run()
. But I wondered: is it two? If you use
concurrent
, for example,
Executors.newFixedThreadPool(5)
then when thread objects are created, immediately when this method is executed, or later, when tasks appear for execution (after all, they may not appear). All these factory methods return some kind of configuration
ThreadPoolExecutor
, which has one of the configuration parameters.
corePoolSize
About this field it is written:
corePoolSize
– the number of threads to keep in the pool,
even if they are idle , unless
allowCoreThreadTimeOut
is set On the other hand, if the tasks didn’t arrive, then we created 5 workers idle, maybe they start being created when tasks start arriving?
In general, 2 or 3 options can still be kept in mind as an answer to the question?
GO TO FULL VERSION