image
Main Components of the Concurrency Utilities - 2 - CountDownLatch
Start course
Difficulty
Intermediate
Duration
2h
Students
32
Description

In this course, we will learn the concepts of Java EE 7 with a focus on Concurrency Utilities with Threads, Semaphore, Phaser and other methods, and Transactions.

Learning Objectives

  • Concurrency Utilities 

Intended Audience

  • Anyone looking to get Oracle Java Certification
  • Those who want to improve Java 7 EE knowledge
  • Java developers

Prerequisites

  • Have at least 2 years of Java development experience 
Transcript

Hello there. In this lesson, we will continue to talk about concurrency in Java, so let's start. CountDownLatch. This synchronizer allows one or more threads to wait for a countdown to complete. This countdown could be for a set of events to happen or until a set of operations being performed in other threads completes. CountDownLatch. Creates an instance of CountDownLatch with the number of times the countdown method must be called before the threads waiting with a wait can continue execution.

Void await. If the current count in CountDownLatch object is zero, it immediately returns, otherwise, the thread blocks until the countdown reaches zero can throw an interrupted exception. Boolean await. Same as the previous method, await, but takes an additional timeout argument. If the thread returns successfully after the count reaches zero, this method returns true. If the thread returns because of timeout, it returns false. Void countdown. Reduces the number of counts by one in this CountDownLatch object. If the count reaches zero, all the awaiting threads are released. If the current count is already zero, nothing happens. Long getCount. Returns the pending counts in this CountDownLatch object. When you create a CountDownLatch, you initialize it with an integer, which represents a count value. Threads would wait by calling the wait method for this count to reach zero. Once zero is reached, all threads are released, any other calls to await would return immediately since the count is already zero.

The counter value can be decremented by one. by calling the CountDown method. You can get the current value of the counter using the getCount method. This class simulates the start of a running race by counting down from five. It holds three runner threads to be ready to start in the start line of the race. And once the countdown reaches zero, all the three runners start running. Count from 5-0 and, then start the race. Instantiate three runner threads. Once counter.countDown in the next statement is called, countdown will reach zero, so shout 'Start.' This runner class simulates a track runner in a 100m dash race. The runner waits until the CountDown timer gets to zero and then starts running. Wait for the timer countdown to reach zero. The output is like this. Let's consider how the program works.

The class runner simulates a runner in a running race waiting to start running. It waits for the race to start by calling the await method on the CountDownLatch object passed through the constructor. The running race starter class creates a CountDownLatch object. This counter object is initialized with the count value five, which means the countdown is from 5-0. In the main method, you create 'Runner Objects.' These three threads wait on the counter object. For each second, you call the countdown method, which decriments count by one. Once the count reaches zero, all three waiting threads are released and they automatically continue execution. An important note here. In this program, the sequence in which Joe, Carl, or Jack is printed, cannot be predicted since it depends on thread scheduling. So, if you run this program, you may get these three names printed in some other order. So, that's it. Hope to see you in our next lesson. Have a nice day.

 

About the Author
Students
3948
Courses
64
Learning Paths
5

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics