4

I would like to perform the following algorithm - this must be done in Java

for(int i = 0; i< 100; i++){
    create 8 threads which perform a task
    wait for all threads to finish
}

It is desirable that threads are not continuously created and destroyed due to overheads (and the fact that each thread will have <20milli seconds of work), which brought about the idea of Thread Pools1. I also know that using Executable2, one can call shutdown, and then awaitTermination. However it is not desirable in this case due to the loop. Thus how can thread synchronization occur?

I would like to synchronize threads in the thread pool as would be done using a traditional thread's join() method.

ET13
  • 427
  • 6
  • 23
  • 1
    The question is not clear. You create 100 times 8 threads or you process 100 tasks in 8 threads? What do you need to synchronize (you synchronize the data, not the threads)? – toto2 Nov 29 '11 at 17:31
  • Are you sure you have to wait for all 8 tasks to finish before starting a new task? If you want 8 threads because you have an 8 core machine then you simply need to use an 8 thread ThreadPool. – toto2 Nov 30 '11 at 16:40

3 Answers3

5

Have you tried looking at a Cyclic Barrier. It is optimized to allow a group of threads to stop and wait till everyone has reached a common barrier. I can't seen any reason why it can't be used with known number of pooled threads with references to a common barrier. There could be some additional complexity if you need to synchronize on the callback invoked with the barriers await() count is reached because it executes in a different thread.

nsfyn55
  • 14,875
  • 8
  • 50
  • 77
  • Thank you, this looks very helpful. I will try coupling the Cyclic Barrier with Thread Pools and post with my results. However I want to point out that you are incorrect in assuming that I am interested in the functionality, I simply have an application I would like to make faster through threading which needs synchronization. – ET13 Nov 30 '11 at 13:21
2

You need to stick all your tasks in a queue, then feed the queue to a ThreadPoolExecutor. You tell the thread pool executor how many threads to use and it takes care of executing the tasks.

TMN
  • 3,060
  • 21
  • 23
  • However this does not solve the problem of synchronization, if I am not mistaken? – ET13 Nov 30 '11 at 13:00
  • If you really need it (and if so, why is your task list not a multiple of 8?), you can put jobs in the task queue 8 at a time and override `ThreadPoolExecutor.afterExecute()` to check if all tasks have completed and, if so, add another 8 to the queue. – TMN Nov 30 '11 at 13:18
0

Have a look at the fork/ join framework of jdk 7.

Puce
  • 37,247
  • 13
  • 80
  • 152
  • Thank you, I had read about the Fork/Join Framework previously here (http://docs.oracle.com/javase/tutorial/essential/concurrency/forkjoin.html) but was dissuaded from using it as it is intended for recursive algorithms, while the algorithm I am trying to implement is much easier to implement iteratively. Also I have a fixed number of threads I would like this to run on, while this algorithm is better suited for dividing a task into small units of work and allowing Fork/Join to figure out how to best use the resources accordingly. – ET13 Nov 30 '11 at 12:59