Java synchronization between different JVMs

烈酒焚心 提交于 2019-12-03 05:47:15

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.

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

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.

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!