Main Components of the Concurrency Utilities - 3 - Exchanger
Start course

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


  • Have at least 2 years of Java development experience 

Hello there, in this lesson, we will continue to talk about concurrency in Java. So, let's start. The Exchanger class is meant for exchanging data between two threads. What Exchanger does is something very simple. It waits until both the threads have called the exchange method. When both threads have called the exchange method, the Exchanger object actually exchanges data shared by the threads with each other. This class is useful when two threads need to synchronize between them, and continuously exchange data. This class is a tiny class with only one method, exchange(). Note that this exchange method has an overloaded form, where it takes a time out period as an argument. 

If you're ready, let's move on to the Eclipse and get some practice. We'll create a Java class in this project. Right click to show the context menu and choose the menu item 'New and Class'. We must specify the class name. And lastly, I will check the box 'Public static void main' to let Eclipse create the main method. First, I will create a new class. An exchange object provides a means for them to talk to each other. Add constructor. Start the conversation with CoffeeShopThread. As you can see, the compiler gives a compilation error because we have to put it in the try and catch block. Here, I choose the 'Surround with try catch' option. As you can see, the compilation error has disappeared. Now, print the response received from CoffeeShopThread. Exchange another set of messages. Now, print the response received from CoffeeShopThread. An exchange could happen only when both send and receive happens. 

Since this is the last sentence to speak, we close the chat by ignoring the dummy reply. Done, and now I'll create the other class. I'll copy these codes and paste them here. Exchange the first messages. Exchange second message. Print what Duke said. There is no message to send, but to get a message from DukeThread, both ends should send a message. So, send a dummy string, done. Let's call this 'In-Maine'. Coordinate the sillyTalk between Duke and coffee shop, by instantiating the exchanger object, and the coffee shop and Duke threads. Done, now let's run the application and test it. The output is like this. Here, this code shows an example simulating sillyTalk between the Java Duke mascot and the coffee shop. 

The two threads, DukeThread and CoffeeShopThreads run independently. However, for a chat to happen, they need to listen when the other is talking. The DukeThread class runs as an independent thread. It talks to the CoffeeShopThread that also runs independently. The chat is achieved by exchanging messages through a common exchange string object, that synchronizes the chat between them. Note that the message printed are the responses received from CoffeeShopThread. The main concept to understand with this example, is that Exchanger helps coordinate exchanging messages between two threads. Both the threads wait for each other, and use the exchange method to exchange messages. So, that's it. Hope to see you in our next lesson. Have a nice day.


About the Author
Learning Paths

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