Sharing Data Between App Instances

M

Mike

Forgive me; this is sort of a newbie question. Is it possible to share in-
memory data between instances of an app?

I realize I could just write the info to disk with one instance then read
it into the other instances from the disk, but I'd like to keep it in
memory for performance reasons.

This data may need to be accessed simultaneously by separate instances in a
read-only fashion. Only one instance will ever modify the data. It's
possible that attempts to read and update the data simultaneously may occur
so the code would need to support some type of locking.

Hopefully this question makes sense to someone besides me ;) Any code
needs to work on JVM version 1.4 (in case that matters.)

Thanks in advance! This group has been infinitely useful in my learning.
Hopefully soon I'll be able to contribute.

- Mike
 
O

Oliver Wong

Mike said:
Forgive me; this is sort of a newbie question. Is it possible to share
in-
memory data between instances of an app?

I realize I could just write the info to disk with one instance then read
it into the other instances from the disk, but I'd like to keep it in
memory for performance reasons.

This data may need to be accessed simultaneously by separate instances in
a
read-only fashion. Only one instance will ever modify the data. It's
possible that attempts to read and update the data simultaneously may
occur
so the code would need to support some type of locking.

Hopefully this question makes sense to someone besides me ;) Any code
needs to work on JVM version 1.4 (in case that matters.)

Thanks in advance! This group has been infinitely useful in my learning.
Hopefully soon I'll be able to contribute.

Writing to disk is probably the easiest way. Another might be for the
first instance (or the read/write instance) to act as a server and listen on
a port. Other instances could then try to listen on the same port, and upon
discovering that it's already in use, realize that they are "secondary
instances", and then act like clients, connecting to that port.

As a newbie, I'd probably go for the easiest solution (which may or may
not be the file based one), and not worry about performance unless it
actually turns out to be a problem under real world usage.

- Oliver
 
P

Patricia Shanahan

Mike said:
Forgive me; this is sort of a newbie question. Is it possible to share in-
memory data between instances of an app?

I realize I could just write the info to disk with one instance then read
it into the other instances from the disk, but I'd like to keep it in
memory for performance reasons.

This data may need to be accessed simultaneously by separate instances in a
read-only fashion. Only one instance will ever modify the data. It's
possible that attempts to read and update the data simultaneously may occur
so the code would need to support some type of locking.

Hopefully this question makes sense to someone besides me ;) Any code
needs to work on JVM version 1.4 (in case that matters.)

Thanks in advance! This group has been infinitely useful in my learning.
Hopefully soon I'll be able to contribute.

- Mike

Don't assume that every read of a shared file will cause a physical disk
read. Most operating systems do some disk caching, so frequently
accessed blocks may be in memory anyway.

Try whatever approach seems simplest first, and measure it. Make sure
you isolate the shared data access in your class design, so that the
implementation can be changed without rewriting the program.

Patricia
 
L

Lew

Patricia said:
Don't assume that every read of a shared file will cause a physical disk
read. Most operating systems do some disk caching, so frequently
accessed blocks may be in memory anyway.

Would NIO memory-mapped files work here? Or some other NIO magic with direct
buffers?

I am speculating, because I am (shamefacedly) only just getting around to
learning NIO.

- Lew
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top