1

Is it true that it is undesirable to create more than 10 additional threads? Example:

for(int i=0; i<100; i++) {
    new Thread() {
        public void run() {
            // something
        }
    }.start();
}

that will create and start 100 threads. That is not good, right?

UPDATE > Every thread are downloading something and put it into the bundle

ruslanys
  • 1,183
  • 2
  • 13
  • 25

4 Answers4

6

It is undesirable to create more thread than you need.

Of course if you need 100 threads, then that is a good number to create.

No idea where you get it is undesirable to create more than 10 additional threads from. Java processes can handle 10,000 threads.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • i think java process can handle more then 10,000 threads, and i don't think it depends upon java process http://stackoverflow.com/questions/763579/how-many-threads-can-a-java-vm-support – Harmeet Singh Aug 06 '12 at 14:01
  • I have found I get a limit around 32K for a Java process even if the process limit is 300K. The number of 10,000 is where its often thought to be worth trying to reducing the number of threads you have (or using more machines) – Peter Lawrey Aug 06 '12 at 14:03
  • 1
    @PeterLawrey Haha - Last time I tried to see where I would get an OutOfMemoryException from creating too many threads, I got a black screen on Windows 7 + hard reboot ;-) – assylias Aug 06 '12 at 20:53
  • 1
    Windows doesn't take kindly to being overloaded. Unix (esp linux) comes from a background where servers support multiple students all trying to use the machine as much as possible so it tends to be a bit more robust when overloaded. ;) – Peter Lawrey Aug 07 '12 at 07:49
2

It will be more easier and faster if you use a thread pool of 10 threads and pass them the correct Runnable.

Dimitri
  • 8,122
  • 19
  • 71
  • 128
  • There is no generic answer to that question. It really depends on what the runnables are doing - if they spend most of their time blocking, having only 10 threads will be highly inefficient. If they keep running calculations, 10 threads is probably a reasonable bet, but even in that case, the answer would obviously depend on the number of processors the machine has... – assylias Aug 06 '12 at 20:55
  • I totally agree but I did not add more details for my answers – Dimitri Aug 07 '12 at 12:35
1

Depends entirely on the context. If most the work you are doing is cpu bound then probably wont make much difference or actually make things worse (context switching etc) unless you actually have 100 cores. If a lot of time is spent on I/O tasks, then threading may be beneficial. You really need to do some benchmarking.

Science_Fiction
  • 3,403
  • 23
  • 27
1

What you probably want is a ThreadPool instead of creating so many Threads.

For example:

 ExecutorService executor = Executors.newFixedThreadPool(4);
 executor.submit(YourRunnable);

Look more into Thread Pools - they will make your life easier.

Eugene
  • 117,005
  • 15
  • 201
  • 306