简体   繁体   中英

Atomic get and increment functionality on a value in shared memory, in Java

I have several processes (on the same computer) which together need to sequentially access the elements of an array in shared memory. To do this I want to store the current index into the array as an int in shared memory, and for each of the processes to be able to get the current value of the index and increment it by a fixed value in one single atomic operation. How can I do this in Java?

Note: I only want this atomic operation coded in Java. The rest of the code happens to be in MATLAB.

You can use the file system: Getting exclusive system-wide lock in Java

Or if you can use C instead, then you can create a POSIX mutex in shared memory: Linux Shared Memory Synchronization

Knowing what you're trying to do is kinda necessary. However, just taking a swing at it - you'll probably want some kind of in-memory database.

The oracle led me here: http://java.dzone.com/articles/using-shared-memory-java , but there's also things like h2 ( http://www.h2database.com/html/main.html ), which I've used before to good effect.

Let us know what you're trying to accomplish and we can probably help more.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM