0

I'm still very new to how Threads work in Java and I got an assignment to recreate the Exchanger class based on its functionality.

I read the documentation and I understand what it is supposed to do, but I'm still confused about how it actually works:

Whenever exchange gets called, how does it actually make the trade between threads?

  1. Does it put the object from its parameter in some sort of stack and returns another one that was already previously put in that stack?
  2. Where would that stack come from?

Thank you for your help!

3
  • 1
    The exchanger is storing 0 to 1 instances of V at all times. Not much in terms of data structures required. Commented Mar 20, 2024 at 11:27
  • 2
    No need for any special "stack." Ordinary member variables can hold the values provided by the two threads. Use Object.wait() and Object.notifyAll(), or use some higher level synchronization object from java.util.concurrent to make the first thread wait for the arrival of the second one. Commented Mar 20, 2024 at 11:37
  • 3
    The code of the JDK class Exchanger contains too much low level optimizations to be useful to a beginner. As teapot418 said, the actual logic can be implemented around one or two variables. Commented Mar 20, 2024 at 11:48

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.