Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why threadpool implementation is slower than normal threads

I have a code block

for (i=0;i<size;i++)
{

      do job;

}

initially this job task was executed sequentially (as shown above) but later I made multithreaded with normal threads (inner runnable class implementation) like,

for (i=0;i<size;i++)

{
   new threadingclass(some args) 
}

runnable threadingclass  {

  pub void run () {

      do job;

 }
} 

this worked fine with some thread limit (till system resources were enough) so to avoid resource overloading I implemented same code with standard theadpool implementation (threadpool,executor service and worker thread implementation)

threadexecutor t=new threadexecutor(size)

for (i=0 ; i<size ; i++)

{
   t.execute(new threadingclass(some args))
}

runnable threadingclass  {

  pub void run () {

      do job;

  }
} 

now scenario was like,

I wanted to run loop for 25 times (no. of threads), I tried with all 3 implementations

  1. sequential : takes 7 min approx
  2. normal multithreading : 40 sec
  3. multithreading with threadpool (size : 100) : 2 min approx

I am bit confused why normal threading and thredpool implementation timings differ so much and internally also threadpool does not involve much complex logic. any help is appreciated.

Thanks in advance

like image 826
Nachiket Kate Avatar asked Feb 04 '14 09:02

Nachiket Kate


1 Answers

It mostly depends on which ExecutorService you chose. Here it seems you chose a FixedThreadPool, which is basically equivalent to launching your threads in parallel if its size is big enough to hold all the threads. You might even get some performance improvement since threads are not creating on the fly.

ExecutorService is usually the way to go since it is readable, maintainable and has almost no overhead. It has also been heavily tested over the past years.

Your results clearly reveal an implementation problem: you probably ran your tests with size = 100 for the ExecutorService example and with size = 25 for the other ones.

like image 129
Jean Logeart Avatar answered Oct 06 '22 00:10

Jean Logeart