Reputation: 2357
I have a very basic question about wait-notify mechanism in Java. I know that we can synchronize two different threads by using this, however, do these threads need to be running in the same process? What if there are two threads running in two different processes? Would wait-notify approach still work?
Upvotes: 3
Views: 495
Reputation:
No, different processes start different JVM, so threads aren't communicating there. The mechanism works with threads are running on the same JVM.
wait()
release the object monitor and go to a infinite sleep while some other threads continue to execute.
notify()
wakes up the first thread that called wait()
on the same object monitor.
Upvotes: 1
Reputation: 6969
Wait-notify won't work for threads running in different processes.
Suppose process A allocated memory of 0x1000
-0x2000
, and is synchronizing on a lock 0x1200
Process B allocates memory of 0x3000
-0x4000
. It can't possibly get access to 0x1200
, can it...
Your best synchronization approaches at that point will be
Upvotes: 2
Reputation: 31744
When you start the JVM process, every thread you create and the memory you use belong to that JVM instance and none other.
When you launch another JVM instance, these two instances do not share memory resources. Hence wait/notify (or any other object for that matter) will only be accessible for threads belonging to that instance of JVM and not other process.
Upvotes: 1