• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

How to share a varible between two JVM

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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".
 
I once met a man from Nantucket. He had a tiny ad
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic