WalzoneInterview Prep
πŸ“ž Interviewing soon? Practice with a realistic AI mock phone interview β€” it calls you, then scores you. First 15 min FREE β†’

Java Concurrency Β· Advanced Β· question 55 of 100

Can you explain what the Java Exchanger class is used for and how it works?

πŸ“• Buy this interview preparation book: 100 Java Concurrency questions & answers β€” PDF + EPUB for $5

The Java Exchanger class is a synchronization aid that allows two threads to exchange objects in a thread-safe manner. The Exchanger class is part of the java.util.concurrent package in Java and was introduced in Java 5.

The Exchanger works by providing a synchronization point where two threads can exchange objects. When one thread calls the exchange() method on an Exchanger object, it blocks until another thread calls the exchange() method on the same Exchanger object. When both threads have called exchange(), the Exchanger returns the objects that were passed by the other thread.

Here is an example of how the Exchanger can be used to exchange objects between two threads:

import java.util.concurrent.Exchanger;

public class ExchangerExample {
    public static void main(String[] args) {
        Exchanger<String> exchanger = new Exchanger<>();

        Thread t1 = new Thread(() -> {
            try {
                String message = "Hello from thread 1";
                System.out.println("Thread 1 sent: " + message);
                String received = exchanger.exchange(message);
                System.out.println("Thread 1 received: " + received);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        Thread t2 = new Thread(() -> {
            try {
                String message = "Hello from thread 2";
                System.out.println("Thread 2 sent: " + message);
                String received = exchanger.exchange(message);
                System.out.println("Thread 2 received: " + received);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        t1.start();
        t2.start();
    }
}

In this example, we create an Exchanger object that exchanges strings (Exchanger<String>). We then create two threads, t1 and t2. In t1, we call exchange() with the string "Hello from thread 1". This thread will block until t2 calls exchange() with its own string. When t2 calls exchange(), the Exchanger will return the string sent by t1, and t1 will print out the received string. The same process happens in t2.

The output of running this code would be something like:

Thread 1 sent: Hello from thread 1
Thread 2 sent: Hello from thread 2
Thread 1 received: Hello from thread 2
Thread 2 received: Hello from thread 1

As you can see, the Exchanger has allowed two threads to exchange objects in a thread-safe manner. If t1 or t2 were to call exchange() again, they would block until the other thread called exchange() again.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Java Concurrency interview β€” then scores it.
πŸ“ž Practice Java Concurrency β€” free 15 min
πŸ“• Buy this interview preparation book: 100 Java Concurrency questions & answers β€” PDF + EPUB for $5

All 100 Java Concurrency questions Β· All topics