• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to share a varible between two JVM  RSS feed

 
Bk Jacky
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to share a variable between two/more instances of JVM running different programs on same computer. e.g., I am running read.java and write.java (my programs). Here read.java reads some value from console. Now write.java must know the value read by read.java and save it to some secondary storage, say file, or just print on its console.

The alternative is that read.java reads the value and save into a file, which write.java try to read at regular interval. Its working fine. But, I dont want to use the file system, as its very slow.

I am using J2SDK1.4.2_02.

Please help me. Thanx a lot in advance.
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
have a look at RMI, Jini, Sockets.

One program would act as a server (Write.java), waiting for the client (Read.java) to transfer the value to the server.

It does not matter if your JVMs are running on the same machine or on different machines. They do not share anything and can not communicate directly.

Why are these programs separated ?

pascal
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16045
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would indeed be easier if you would start both programs inside the same JVM. You could do that by writing a small wrapper that starts the programs in separate threads, like this for example (NOTE, untested code!):

(Note that if one of the applications calls System.exit(...), the whole JVM will get killed).
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd probably go for a database - it's persistent and transactional and handles concurrency. If it's really too slow, Google for "Java distributed cache". There are some good solutions for keeping in-memory stuff in sync. Failover and re-synchronizing if you start one VM late or stop and restart are all tricky enough that I would rather "buy" than "build".
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!