Codelytics

Visualise your geek speak

  • Blog
  • Terms
  • Privacy
  • Contact

How to Synchronize Threads – Use CountDownLatch

March 18, 2015 by Adrian Bartlett Leave a Comment

Concurrency in non-trivial systems is difficult. java.util.concurrent. CountDownLatch is one of my favourite concurrent core Java classes, because it simplifies the synchronization of threads. This class is useful when you want to wait until some task in another thread completes, before continuing in the current thread.

Synchronized swimmers - Greg Neate - https://www.flickr.com/photos/neate_photos/3522905573/in/photostream/

CountDownLatch causes one or more dependent threads to wait until tasks in one or more task threads completes.

  • CountDownLatch is constructed with a count (x)
  • Dependent Threads wait by calling CountDownLatch::await
  • CountDownLatch::countDown must be called (x) times by Task Threads, causing CountDownLatch::await calls in Dependent Threads to be released

In the following example, the Application main thread needs to wait for the DatabaseService thread to complete initialisation, before the Application can complete its own initialisation.

Click on the flashing arrow, to take 10 steps through Codelytics’ visualization of CountDownLatch.

Step through CountDownLatch:

Http iframes are not shown in https pages in many major browsers. Please read this post for details.

In the old days, before Java 1.5, we used to used Object::wait and Object::notify, to achieve this form of synchronization. Nowadays, using CountDownLatch is easier.

For more information see:

  • Oracle’s website

Do you like this Codelytics’ visualization?

If you like our visualizations, be sure to subscribe to our mailing list below. With your free subscription, we will periodically send you alerts about new tutorials and visualizations posted here.

What do you think about CountDownLatch? What do you think about this Codelytics’ visualization? We’d love to see your feedback in the comments below.

Image source: Greg Neate, image modified


Share this:

  • Click to email this to a friend (Opens in new window)
  • Click to print (Opens in new window)
  • Click to share on Facebook (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on Google+ (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)

Filed Under: Concurrency, Java Tagged With: Concurrency, CountDownLatch, Java

Connect with us

  • Facebook
  • Google+
  • Twitter
  • YouTube

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Facebook
  • Google+
  • Twitter
  • YouTube

Subscribe to the blog

Recent posts

  • LinkedHashMap Tutorial
  • java.util.TreeMap visualized: interactive view from a new perspective
  • Constructor & Setter Dependency Injection
  • 4 Reasons to use a Java Semaphore
  • Java 8: How To Retrieve Absent Values From HashMap

Search

  • Blog
  • Terms
  • Privacy
  • Contact

All website content, including software, (excluding that owned by Wordpress and Centric Theme on Genesis Framework) is owned by Codelytics Pty Ltd and/or its affiliates, all rights reserved and protected by Australian copyright laws and relevant international treaty provisions. Any reproduction, redistribution and/or reverse engineering is expressly prohibited. For further details see the terms and conditions page on this site.
© Codelytics Pty Ltd, Adrian Bartlett, 2013

Copyright © 2022 · Centric Theme on Genesis Framework · WordPress · Log in

loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.