tmn
tmn

Reputation: 11559

Can blocking still cause thread starvation?

This may be a remedial question, but I just had a double-take and I need to clarify something. If I have a fixed thread pool that manages 5 threads, and I have 150 instances of Task A and 150 instances of Task B... and task B is dependent on its Task A counterpart's completion, and thus it can block waiting for Task A. And tasks of both A and B are submitted to the same fixed threadpool, that can cause thread starvation correct? When one task blocks, it does not free that thread to execute another task. Right?

Upvotes: 0

Views: 272

Answers (1)

LeffeBrune
LeffeBrune

Reputation: 3477

I would assume so, although I'm having a hard time finding any information about it. It would make little sense to implement preemptive multitasking in Java. For me as a developer it is safer to assume that a single thread runs only one task until its completion, than that several tasks can share a thread at the same time.

Create two separate executors: one for tasks A, another for B.

Upvotes: 1

Related Questions