Writing to a file log from different threads

J

Jeff

Hi, I have an application that runs several threads. At various times those
threads will individually want to write to a shared file log.

So at program invocation the file log 'A' must be created (within its own
thread?). Later on, threads B, C & D will independantly want to write to log
A.

I can't quite seem to get my head round how I write this so that each thread
can see the same instance of the file log. (I guess they should write to it
using a synchronized write method).

Can anyone please shed some light on this?

Many thanks in anticipation.

Cheers

Jeff
 
J

Jorn W. Janneck

Jeff said:
Hi, I have an application that runs several threads. At various times those
threads will individually want to write to a shared file log.

So at program invocation the file log 'A' must be created (within its own
thread?). Later on, threads B, C & D will independantly want to write to log
A.

I can't quite seem to get my head round how I write this so that each thread
can see the same instance of the file log. (I guess they should write to it
using a synchronized write method).

yes, i think representing the log by an object that synchronizes writing
seems like a reasonable idea (in that case, you might also want to handle
file creation in that object, and synchronize it with the writes). you could
make this object globally available through, e.g., a static variable
somewhere, a singleton pattern, or some other suitable technique.

if performance is an issue, you might want to check whether the common write
locking tends to stall threads too often. in that case you could make the
critical section faster by buffering the output and then have a single
thread that reads from that buffer and does the actual writing to the output
medium.

also, introducing a synchronization between threads always runs the risk of
causing deadlocks. unless you do something rather fancy in the output
method, this should not be a problem in the case of a logging
infrastructure, but it might be something to keep in mind (and document) if
you make it available as an extensible API to someone else.

hth,

-- j
 
J

Jeff

Jorn W. Janneck said:
yes, i think representing the log by an object that synchronizes writing
seems like a reasonable idea (in that case, you might also want to handle
file creation in that object, and synchronize it with the writes). you could
make this object globally available through, e.g., a static variable
somewhere, a singleton pattern, or some other suitable technique.

if performance is an issue, you might want to check whether the common write
locking tends to stall threads too often. in that case you could make the
critical section faster by buffering the output and then have a single
thread that reads from that buffer and does the actual writing to the output
medium.

also, introducing a synchronization between threads always runs the risk of
causing deadlocks. unless you do something rather fancy in the output
method, this should not be a problem in the case of a logging
infrastructure, but it might be something to keep in mind (and document) if
you make it available as an extensible API to someone else.

hth,

-- j

OK, I'll give it some thought.

Cheers

Jeff :0)
 
N

nos

Jeff said:
Hi, I have an application that runs several threads. At various times those
threads will individually want to write to a shared file log.

So at program invocation the file log 'A' must be created (within its own
thread?). Later on, threads B, C & D will independantly want to write to log
A.

I can't quite seem to get my head round how I write this so that each thread
can see the same instance of the file log. (I guess they should write to it
using a synchronized write method).

Can anyone please shed some light on this?

Many thanks in anticipation.

Cheers

Jeff
The java standard logging capability uses a file lock.

import java.util.logging.*;
static Logger logger = Logger.getLogger("global");
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top