• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ThreadPool Question

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am trying to learn multi-threading, as part of that i am trying to implement thread pool. In below main method i am passing runnable object to pool.execute() method to run that in a separate thread from pool , now how do i wait till all the threads are completed to start processing the result.


 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The way to wait for Thread T to die is to call T.join(). However, that's probably not appropriate here for two reasons.

1) You normally don't have direct access to the Threads in a pool.

2) In a thread pool, the Threads usually continue to live beyond the execution of a single task. That's kind of the point of what thread pools are for and how they work.

Your threads might do something like this, in a loop:


One easy way to do the last step--the signalling--is to use a java.util.concurrent.CountDownLatch. Set it to 1, pass it to the thread pool with the task to execute, and have the thread pool call countDown(). The main thread calls await().
 
Surender Suri
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jeff.

After using the CountDownLatch, it is working as expected atleast with the initial test with no null exceptions. Is this the correct way to use CountDownLatch. Also, i see their is one more way to do this by using Semaphore, with the release() and acquire() , what is the difference between them ..

Secondly i don't understand how the Semaphore works at all , i mean where is the link which holds the threads to wait .. it say's it's just a counter adding the permits when release() is called



Thanks in advance.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Surender Suri wrote:Thanks Jeff.

After using the CountDownLatch, it is working as expected atleast with the initial test with no null exceptions. Is this the correct way to use CountDownLatch.



Why are you calling countDown() at the start of your task? You should call it at the end.

Also, i see their is one more way to do this by using Semaphore, with the release() and acquire() , what is the difference between them ..



You could do that I guess. I feel like a CDL is better suited to the task though. It's meant for waiting until some event happens. You can achieve the same results with a Semaphore here, but that's not really its purpose.

Secondly i don't understand how the Semaphore works at all , i mean where is the link which holds the threads to wait .. it say's it's just a counter adding the permits when release() is called



I don't really understand what you're asking here. If you're curious as to how it works, you can look at the source code. It's in src.zip, which came with your JDK download.

Finally, though, all this is pretty much irrelevant here. I didn't really look at your code before. I thought you were implementing your own thread pool. Since you're just using the existing ThreadPoolExecutor, you don't have to do the explicit CDL. You can use one of the submit or invoke methods, and then call get() on the returned Future.
reply
    Bookmark Topic Watch Topic
  • New Topic