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
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With