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.
SCJP1.4<br />SCWCD1.5<br />"Nothing is impossible"
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).
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".
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi