Java synchronization between different JVMs
Asked Answered
P

5

14

The project I am working on would trigger various asynchronous jobs to do some work. As I look into it more these asynchronous jobs are actually being run as separate JVMs (separate java processes). Does it mean I would not be able to use any of the following if I need to synchronize between these processes:

  • synchronized methods/blocks
  • any lock that implements java.util.concurrent.locks

Because it seems to me they are all thread-level?

Does Java provide support for IPC like semaphores between processes?

Predictory answered 16/5, 2012 at 16:27 Comment(0)
V
14

That's right. You can not use any standard synchronization mechanisms because they are working into one JVM.

Solutions

  1. You can use file locks introduced in java 7.
  2. You can use synchronization via database entities.
  3. One of already implemented solutions like Terracota may be helpful
  4. Re-think your design. If you are beginner in java world try to talk in details with more experienced engineers. Your question shows that IMHO you are just on wrong way.
Verda answered 16/5, 2012 at 16:31 Comment(2)
Thanks! We are re-using the same architecture from a different project so some of the designs might not make sense in our world. Many thanks for the input, this is very helpful.Predictory
5. You can use file locks introduced in Java 1.4.Jugglery
E
7

You can use synchronized keyword, locks, atomic objects, etc. - but they are local to the JVM. So if you have two JVMs running the same program, they can still e.g. run the same synchronized method at the same time - one on each JVM, but not more.

Solutions:

  • provides distributed locking

  • as well

  • you can use manual synchronization on file system or database

Estellaestelle answered 16/5, 2012 at 16:32 Comment(0)
L
1

I have implemented a java IPC Lock implementation using files: FileBasedLock and a IPC Semaphore implementation using a shared DB (jdbc): JdbcSemaphore. Both implementations are part of spf4j.

If you have a zookeeper instance take a look at the Zookeeper based Lock recipes from Apache Curator

Longdistance answered 23/8, 2016 at 0:43 Comment(0)
C
1

I'm using distributed lock provided by Redisson to synchronize work of different JVMs

Cruel answered 27/8, 2016 at 8:29 Comment(0)
F
0

they are all thread-level?

That's correct, synchronized etc only work within the context of a single process.

Does Java provide support for IPC like semaphores between processes?

One way to implement communication between Java processes is using RMI.

Footless answered 16/5, 2012 at 16:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.