0

I have a http server which is gonna be really busy, there are few HttpHandlers inside it which all of them start their job with a new Thread() , since i still can not compeletely understand ThreadPoolExecutor's Usage (When you should use, when no need to), i really could use a little tip about it and do i need to use one?

Plus is there any roof for the threadPoolExecutor's max Threads ?

Same goes for the android, i dont understand why should i use ThreadPoolExecutor instead simply use newThread()?

Lazy Ninja
  • 22,342
  • 9
  • 83
  • 103
Reza
  • 151
  • 1
  • 9

3 Answers3

0

Basically ThreadPoolExecutor is just a high level API from java to do task in multiple thread without dealing with low level API (Creating thread manually)

For a little example a ExecutorService executor = Executors.newFixedThreadPool(5); will run the tasks you submit in 8 threads.

You can try to understand it more by reading this documentation.

http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadPoolExecutor.html
and this tutorial
http://tutorials.jenkov.com/java-util-concurrent/threadpoolexecutor.html

Niko Adrianus Yuwono
  • 11,012
  • 8
  • 42
  • 64
  • and as far as i know medium thread pool for androids is 5 right? and in those 8 threads you said rest will just be waiting or dropped? plus keepaliveTime means time that Executor waits before dropping an idle thread? so for network it must be about 30sec right? – Reza Oct 14 '14 at 11:13
  • Yes sorry about that, I thought you were asking in Java. For keepAliveTime there is very good explanation here -> http://stackoverflow.com/questions/10379314/how-does-keep-alive-work-with-threadpoolexecutor maybe it can help you :) – Niko Adrianus Yuwono Oct 15 '14 at 01:16
  • I don't understand why you changed the size to 5, but since you did, you should also change the continuation of the line "you submit in **5** threads" instead of 8. – Maverick Meerkat Dec 13 '19 at 22:32
0

you must use ThreadPoolExecutor when you do not know how many times your new Thread() will be called because you have limited resources if you let unlimited call to new Thread() you will get in trouble like out of memory exception. in restricted world like android it is necessary to know how many times you create thread and if it is not in control of you for example your user input determines the creation of thread you must use ThreadPoolExecutor.

mmlooloo
  • 18,937
  • 5
  • 45
  • 64
0

Creating and destroying threads has a high CPU usage, so when you need to perform lots of small, simple tasks concurrently the overhead of creating your own threads can take up a significant portion of the CPU cycles and severely affect the final response time. This is especially true in stress conditions where executing multiple threads can push CPU to 100% and most of the time would be wasted in context switching (swapping threads in and out of the processor along with their memory).

Nithin Raja
  • 1,144
  • 12
  • 11