1+ program writing simulatenously into a file, possible?

T

TonY

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


thanks
 
A

Ann

TonY said:
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


thanks

Since you say *simulataneously* the answer is no.
 
B

ByteCoder

TonY said:
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();
}

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

ByteCoder

ByteCoder said:
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!
 
M

Matt Humphrey

TonY said:
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

The behavior of two independent applications (e.g. JVM) writing to the same
file depends on the operating system and unless you have specific knowledge
of that access, it's best to simply establish a locking protocol based on
some shared resource. An easy one to use is based on allocating a server
socket. Your write routine would first try to allocate a server socket of a
particular unusual number. If it succeeds, you get to open the file, write
to it, close the file and release the socket. Because you're creating
exclusive access it's best to keep your file use as short as possible. If
your write routine can't open the socket, it should wait and try again. The
OS ensures only one application can allocate the socket at any time.

Cheers,
Matt Humphrey (e-mail address removed) http://www.iviz.com/
 
B

ByteCoder

Matt said:
The behavior of two independent applications (e.g. JVM) writing to the same
file depends on the operating system and unless you have specific knowledge
of that access, it's best to simply establish a locking protocol based on
some shared resource. An easy one to use is based on allocating a server
socket. Your write routine would first try to allocate a server socket of a
particular unusual number. If it succeeds, you get to open the file, write
to it, close the file and release the socket. Because you're creating
exclusive access it's best to keep your file use as short as possible. If
your write routine can't open the socket, it should wait and try again. The
OS ensures only one application can allocate the socket at any time.

Cheers,
Matt Humphrey (e-mail address removed) http://www.iviz.com/

I agree with that. That's a better solution than mine, but what do you
mean by "Your write routine would first try to allocate a server socket
of a particular unusual number"?
Shouldn't the application controlling the file and accepting incomming
connections be using a ServerSocket? The server should be 'blocking'
like it normally does and the applications connecting to it should have
a timeout long enough allow another application to complete it's write.
 
S

Steve Horsley

ByteCoder said:
I agree with that. That's a better solution than mine, but what do you
mean by "Your write routine would first try to allocate a server socket
of a particular unusual number"?
Shouldn't the application controlling the file and accepting incomming
connections be using a ServerSocket? The server should be 'blocking'
like it normally does and the applications connecting to it should have
a timeout long enough allow another application to complete it's write.

He's talking about using the ServerSocket as a locking mechanism. Only
one ServerSocket can be open at a particular port number. A second app
trying to listen on the same port gets an exception "address in use".
If everybody refrains from opening the file unless they also have the
open ServerSocket then that guarantees single-user access only.

The OP doesn't say why simultaneous (I guess he means concurrent) access
is wanted, but I advise against trying. Some OSs won't allow it, and I
know from experience that corruption can occur when it is possiblbe.
For instance two threads or processes opening the same file for appending
keep their own write-position indicator, don't know when the other thread
writes, and tend to overwrite each other's output. It's a problem with
log files.

Steve
 
F

frankgerlach22

It is perfectly legal and possible to write into one file with more
than one
thread. Use a RandomAccessFile and make sure the regions a thread
writes into
are somehow locked for other threads to access. Obviously, there needs
to be
some management data structure, which is synchronized, to administer
the file
regions.
 

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,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top