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?
- 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?
- Where would that stack come from?
Thank you for your help!
Object.wait()andObject.notifyAll(), or use some higher level synchronization object fromjava.util.concurrentto make the first thread wait for the arrival of the second one.Exchangercontains 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.