2

I want to clear my concept on unused threads effect.What is the overall effect of too many unused blocked threads in the thread pool. Suppose I have a thread pool having 100 threads in which 50 are unused and waiting for request to process but the arrival rate of request is 2 slow due to which 50 threads are in the block state.


What system resources would be effected?


Does CPU resource would be effected?


Does memory resource would be effected?


What is the effect of this scenario on thread management overhead? i.e. Thread management overhead only involves in 50 running thread or all of 100 threads?

faisal bahadur
  • 97
  • 1
  • 1
  • 7
  • 2
    Do some benchmarking and find out. – Aify Jun 03 '15 at 22:33
  • If you have 100 idle threads and using a Java thread pool, they will be be removed and GC'd, therefore creating no effect on CPU or memory (other than collection overhead). Due to semantics of the thread pool, you may need to tune it to GC the threads after less than 60 seconds. –  Jun 03 '15 at 23:26
  • 1
    @xTrollxDudex [ThreadPoolExecutor](https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadPoolExecutor.html) takes care of all that automatically out of the box. (Keep alive argument and maximum pool size) – Vojtěch Kaiser Jun 03 '15 at 23:40
  • You're begging the question here. You're assuming there is such a thing as 'too many'. 50 of just about anything isn't too many. – user207421 Jun 04 '15 at 00:44
  • @VojtěchKaiser I was referring to the one provided by `Executors`, whose executors default to 60 seconds idle time before removal. Direct instantiation of a `ThreadPoolExecutor` requires that particular argument –  Jun 04 '15 at 01:13
  • I suggest see this regarding to memory question: [impact-of-threads-in-wait-state](http://stackoverflow.com/questions/19522125/impact-of-threads-in-wait-state-on-memory-in-java-process) – hsnkhrmn Jun 04 '15 at 10:31

2 Answers2

1

What system resources would be effected?

Memory. Each native thread requires some for its stack. It's not much and with 50 threads I wouldn't care.

A thread blocked for other reason than "waiting for a job" may be a bigger problem as it may hold other resources, including e.g., file descriptors, which are limited.

In case of a server running many many requests which are waiting for an external resource (file or DB access), this may be a problem.

Additionally, there are ThreadLocals, which may hold data which may or may not be needed later. It can even lead to memory leaks like here, but this is completely unrelated to the number of (idle) threads.

Does CPU resource would be effected?

Why should they? In Java, it's just an object sitting somewhere in a collection of idle threads.

Does memory resource would be effected?

See above.

Community
  • 1
  • 1
maaartinus
  • 44,714
  • 32
  • 161
  • 320
-1

Take a look at article on CPU Scheduling.

Thread that is waiting for some interruption (done by OS, or java notify) just sits in collection and is not participating in resource management. Only threads that are in ready state can obtain CPU resources.

Resources affected may be only by locks, in case thread is sleeping (which means locks are not released).

Do not create too many threads above those physically available. Even though switching TCB is faster than PCB, try to avoid it as much as possible. Specific demands are up to used JIT, hardware and your own implementation.

Vojtěch Kaiser
  • 568
  • 3
  • 15
  • He's talking about blocked threads here, not running threads. You can have as many blocked threads as you like without any impact on the matters in your answer, which is therefore basically irrelevant. – user207421 Jun 04 '15 at 00:45
  • Does block threads still consumes memory? if not then where is TCB etc of those blocked threads? – faisal bahadur Jun 04 '15 at 04:38
  • @faisalbahadur What I meant it does not consume any additional memory, like the state is constant. – Vojtěch Kaiser Jun 04 '15 at 08:39
  • @EJP Blocked thread is waiting thread, which is exactly what I was talking about in my answer. His threads are blocked on thread pool handling waiting for work, therefore not participating in resource distribution. I really don't see the difference between what you have said and my answer, can you please clarify? – Vojtěch Kaiser Jun 04 '15 at 08:45