4

I'm trying to understand the advantage of using thread pools, i wrote this code to see the time improvement of fixed thread pool.

First, i set the number of threads in the pool to 1 and it took approximately 920 ms, then i changed the number of threads in the pool to 2(and 3,4,5,6,7...) and it took 1200 ms, shouldn't it be faster when the threads are running concurrently?

When i changed it to cached thread pool it also took 1200 ms

package threadpool; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class Example3 { public static void main(String[] args) { new Example3(); } public Example3() { try { testFixedPool(1); //testFixedPool(2); } catch (InterruptedException e) { e.printStackTrace(); } } public void testFixedPool(int numberOfThreads) throws InterruptedException { ExecutorService executor = Executors.newFixedThreadPool(numberOfThreads); long start = System.currentTimeMillis(); executor.execute(new TaskPrintInteger(0, 10000)); executor.execute(new TaskPrintInteger(1, 10000)); executor.execute(new TaskPrintInteger(2, 10000)); executor.execute(new TaskPrintInteger(3, 10000)); executor.execute(new TaskPrintInteger(4, 10000)); executor.execute(new TaskPrintInteger(5, 10000)); executor.execute(new TaskPrintInteger(6, 10000)); executor.execute(new TaskPrintInteger(7, 10000)); executor.execute(new TaskPrintInteger(8, 10000)); executor.execute(new TaskPrintInteger(9, 10000)); executor.shutdown(); while(!executor.isTerminated()){ } System.out.println(); System.out.println((System.currentTimeMillis()) - start); } private class TaskPrintInteger implements Runnable { private int number, times; public TaskPrintInteger(int number, int times) { this.number = number; this.times = times; } @Override public void run() { for (int i = 0; i < times; i++) { System.out.println(number); } } } } 
2
  • 7
    Instead of 10 tasks, try submitting 100000 tasks. And don't make them print things to System.out, since that synchronizes (so only one thread is printing at a time, negating any advantages of threading). Commented Sep 27, 2014 at 11:55
  • 2
    @immibis The key here is indeed the synchronisation of the System.out.println() call. That and the fact that parallel processing isn't going to boost any I/O bound process. Commented Sep 27, 2014 at 12:06

2 Answers 2

7

You are asking many threads to all carry out one activity, that is most likely (although not guaranteed to be) synchronized:

System.out.println(number); 

Lets imagine that you have one person, and you ask her to write "one" 10 times on a piece of paper, one word on each line.

Now, you need to write "one" and "two" each 10 times on a piece of paper with one word on each line; which will be faster?

  1. use the same one person you used above
  2. ask that the person to bring a friend to write the "two"s. Except that the rules are that they start at one end of a hall, and both run to the paper. The one who gets there first, writes their word. Now they run back to the other end of the hall and repeat the process until all the words are written.

I would hazard a guess that the first alternative would be faster.

Imagine the same scenario with 5 people all trying to write their word 10,000 times? Chaos? Yes!

If you want to see real improvements from threading, tasks should be completely isolated with no synchronization. And especially no IO!

Sign up to request clarification or add additional context in comments.

Comments

1

When you have a contented resources, the optimal number of threads can be 1. Writing to the console is expensive and single threaded, so when you use multiple threads you are increasing the overhead, rather than making your program faster. Threads work best when they run independently.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.