pree
pree

Reputation: 2357

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?

Upvotes: 3

Views: 495

Answers (3)

user2173738
user2173738

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

iluxa
iluxa

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

  • have the processes talk to each other over local TCP/IP sockets
  • have the processes read & write lock file(s)

Upvotes: 2

Jatin
Jatin

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

Related Questions