Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java wait-notify mechanism among different processes

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?

like image 890
pree Avatar asked Aug 13 '26 17:08

pree


2 Answers

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

  • have the processes talk to each other over local TCP/IP sockets
  • have the processes read & write lock file(s)
like image 54
iluxa Avatar answered Aug 16 '26 13:08

iluxa


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.

like image 30
Jatin Avatar answered Aug 16 '26 13:08

Jatin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!