Java synchronization between different JVMs
Solution 1
That's right. You can not use any standard synchronization mechanisms because they are working into one JVM.
Solutions
- You can use file locks introduced in java 7.
- You can use synchronization via database entities.
- One of already implemented solutions like Terracota may be helpful
- 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.
Solution 2
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:
terracotta provides distributed locking
hazelcast as well
you can use manual synchronization on file system or database
Solution 3
I'm using distributed lock provided by Redisson to synchronize work of different JVMs
Related videos on Youtube
tytchong
Updated on June 04, 2022Comments
-
tytchong over 1 year
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?
-
tytchong over 11 yearsThanks! 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.
-
user207421 over 11 years5. You can use file locks introduced in Java 1.4.