Re: 1+ program writing simulatenously into a file, possible?

From: ByteCoder (ByteCoder_at_127.0.0.1)
Date: 12/28/04


Date: Tue, 28 Dec 2004 22:50:08 +0100

ByteCoder wrote:
> TonY wrote:
>
>> Hi,
>> i need to solve this problem using Java
>> Is it possible for two or more applications to write *simulataneously*
>> into a shared file?
>> do i have to use RandomAccessFile? is there any system settingst to do
>
>
> You could make a Thread class which sleeps that accepts requests (to
> write to a file) from other classes. The method in the thread should be
> a public synchronized method. Also make sure you this object to other
> classes you might start (from this class).
>
> Just to be sure you could make it something like this:
>
> Variable on class level:
> private boolean running = true;
>
> methods:
>
> public void run() {
> while (running) {
> try {
> this.sleep();
> } catch (InterruptedException ex) {
> }
> }
> }
>
> public synchronized void writeToFile(String message) throws IOException{
> // Code to write to file here.
> // throws IOException which makes it easy for the other thread/class
> // to check if the write was succesful.
> }
>
> public void stopMe() {
> this.running = false;
> this.interrupt();
> }

The stopMe method should be synchronized too!

>
> Above methods might not be completely correct, because I didn't care to
> write it in my IDE.
>

-- 
-------------
- ByteCoder -           ...I see stupid people
-------------
                    Curiosity *Skilled* the cat