A
A
aarifkhamdi2018-04-07 17:13:12
Java
aarifkhamdi, 2018-04-07 17:13:12

How to release a ThreadPool if a thread is waiting for a resource and another thread can be executing?

Means I create ThreadPool: . I send tasks to it: Many tasks... Let me send 10 tasks there. The first 8 are waiting for a resource (blocked). This is where my question actually begins. Why do they take up space in the executor? Why doesn't the executor take on the 2 remaining tasks? I figured out how it should be? At the same time, the code works well if replaced with How to implement what I want (to take tasks from the queue when it cannot perform the current ones)? executor = Executors.newFixedThreadPool(8);
executor.submit(work);
executor = Executors.newCachedThreadPool();

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gornostaev, 2018-04-07
@aarifkhamdi

Where do you think they should be? All threads created by the executor are born, work and die "inside" the pool. If you need to run more threads, either increase the limit or use CachedThreadPool. In order for the thread to do something else while waiting for the resource, you yourself will have to rewrite your Runnable\Callabe tasks so that they check the state of the resource and do something else if it is blocked.

A
Alexander Yudakov, 2018-04-07
@AlexanderYudakov

You yourself wrote:
newFixedThreadPool(8);
What are the questions after that?
In general, it is not necessary to specify anything in the constructor there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question