By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,949 Members | 1,086 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,949 IT Pros & Developers. It's quick & easy.

Is PrintWriter threadsafe?

P: n/a
Is it OK for multiple threads to use the same PrintWriter concurrently?
Jul 17 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a

"Jeff Schwab" <je******@comcast.net> wrote in message
news:rY********************@comcast.com...
Is it OK for multiple threads to use the same PrintWriter concurrently?


What would you consider threadsafe behaviour in such a scenario?

Silvio Bierman
Jul 17 '05 #2

P: n/a
Silvio Bierman wrote:
"Jeff Schwab" <je******@comcast.net> wrote in message
news:rY********************@comcast.com...
Is it OK for multiple threads to use the same PrintWriter concurrently?

What would you consider threadsafe behaviour in such a scenario?


Good question. I just want to make sure the I/O system won't get
confused. It's OK if the output of the two threads is intertwined.
Jul 17 '05 #3

P: n/a
Do you mind if characters within words or words are mixed?

Thread 1 trys to print "Thread 1 ABCDEF"
Thread 2 trys to print "Thread 2 GHIJKLM"

Is the following OK?

"TThhr 1 eadAe 2a GHd ABCIJKLMDEF"

I think you should synchronize the actual IO call. What we do is utilize a
message queue;
the different threads drop message objects in the queue while a low-priority
thread waits
on the queue, pops off a message, does the IO, then waits for the next
message. So only
one, low-priority, thread is actually doing the IO.
"Jeff Schwab" <je******@comcast.net> wrote in message
news:eu********************@comcast.com...
Silvio Bierman wrote:
"Jeff Schwab" <je******@comcast.net> wrote in message
news:rY********************@comcast.com...
Is it OK for multiple threads to use the same PrintWriter concurrently?

What would you consider threadsafe behaviour in such a scenario?


Good question. I just want to make sure the I/O system won't get
confused. It's OK if the output of the two threads is intertwined.

~ Let us linux ~
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 17 '05 #4

P: n/a
Bob Sullivan wrote:
Do you mind if characters within words or words are mixed?

Thread 1 trys to print "Thread 1 ABCDEF"
Thread 2 trys to print "Thread 2 GHIJKLM"

Is the following OK?

"TThhr 1 eadAe 2a GHd ABCIJKLMDEF"
Yes.
I think you should synchronize the actual IO call. What we do is utilize a
message queue;
the different threads drop message objects in the queue while a low-priority
thread waits
on the queue, pops off a message, does the IO, then waits for the next
message. So only
one, low-priority, thread is actually doing the IO.


That's a neat idea, and it does help, but I need to see some output
ASAP. It can't wait for a low-priority thread.
Jul 17 '05 #5

P: n/a
Jeff Schwab wrote:
Bob Sullivan wrote:
Do you mind if characters within words or words are mixed?

Thread 1 trys to print "Thread 1 ABCDEF"
Thread 2 trys to print "Thread 2 GHIJKLM"

Is the following OK?

"TThhr 1 eadAe 2a GHd ABCIJKLMDEF"

Yes.
I think you should synchronize the actual IO call. What we do is utilize a message queue;the different threads drop message objects in the queue while a low-priority thread waits on the queue, pops off a message, does the IO, then waits for the next message. So only one, low-priority, thread is actually doing the IO.


That's a neat idea, and it does help, but I need to see some output ASAP. It can't wait for a low-priority thread.

To clarify: I don't need truly unbuffered output, but buffered only to
the level of the individual println. Since I don't println a character
at a time, is the above situation ("TThhr 1 eadAe 2a GHd ABCIJKLMDEF")
really possible?
Jul 17 '05 #6

P: n/a
Hello Jeff,

You could do all println calls inside synchronized(someSingleton) blocks.
This will result in almost-prompt printing and decent separation.

Silvio Bierman
"Jeff Schwab" <je******@comcast.net> wrote in message
news:3N********************@comcast.com...
Jeff Schwab wrote:
Bob Sullivan wrote:
Do you mind if characters within words or words are mixed?

Thread 1 trys to print "Thread 1 ABCDEF"
Thread 2 trys to print "Thread 2 GHIJKLM"

Is the following OK?

"TThhr 1 eadAe 2a GHd ABCIJKLMDEF"

Yes.
I think you should synchronize the actual IO call. What we do is
utilize a message queue;the different threads drop message objects in the
queue while a low-priority thread waits on the queue, pops off a message,
does the IO, then waits for the next message. So only one, low-priority,
thread is actually doing the IO.
That's a neat idea, and it does help, but I need to see some output

ASAP. It can't wait for a low-priority thread.

To clarify: I don't need truly unbuffered output, but buffered only to
the level of the individual println. Since I don't println a character
at a time, is the above situation ("TThhr 1 eadAe 2a GHd ABCIJKLMDEF")
really possible?

Jul 17 '05 #7

P: n/a
Silvio Bierman wrote:
Hello Jeff,

You could do all println calls inside synchronized(someSingleton) blocks.
This will result in almost-prompt printing and decent separation.

Silvio Bierman
Yes, if necessary, I'll do that. I expect to have hundreds or thousands
of threads at a time vying for the same Writer, though, and I worry that
the performance detriment will be too great.

Is it safe to get the current time simultaneously in different threads,
without synchronization? I only need to see error messages immediately
when the app ( a server ) is first starting. Once things get going,
it's just the order of the messages that needs to be preserved. Maybe I
could use the message-queue idea, and add time-stamps to the messages.
What do you think?


"Jeff Schwab" <je******@comcast.net> wrote in message
news:3N********************@comcast.com...
Jeff Schwab wrote:
Bob Sullivan wrote:
Do you mind if characters within words or words are mixed?

Thread 1 trys to print "Thread 1 ABCDEF"
Thread 2 trys to print "Thread 2 GHIJKLM"

Is the following OK?

"TThhr 1 eadAe 2a GHd ABCIJKLMDEF"
Yes.
I think you should synchronize the actual IO call. What we do is
utilize a message queue;the different threads drop message objects in the
queue while a low-priority thread waits on the queue, pops off a message,
does the IO, then waits for the next message. So only one, low-priority,
thread is actually doing the IO.
That's a neat idea, and it does help, but I need to see some output


ASAP. It can't wait for a low-priority thread.

To clarify: I don't need truly unbuffered output, but buffered only to
the level of the individual println. Since I don't println a character
at a time, is the above situation ("TThhr 1 eadAe 2a GHd ABCIJKLMDEF")
really possible?


Jul 17 '05 #8

P: n/a

"Jeff Schwab" <je******@comcast.net> wrote in message
news:Qa********************@comcast.com...

Yes, if necessary, I'll do that. I expect to have hundreds or thousands
of threads at a time vying for the same Writer, though, and I worry that
the performance detriment will be too great.

Is it safe to get the current time simultaneously in different threads,
without synchronization? I only need to see error messages immediately
when the app ( a server ) is first starting. Once things get going,
it's just the order of the messages that needs to be preserved. Maybe I
could use the message-queue idea, and add time-stamps to the messages.
What do you think?


You have that many threads shortly after starting your server? Impressive!

Using a synchronized queue would work fine and it would probably mean less
time lost due to synchronization when compared to synchronzied println's.
Your print-thread would be wait-ing on the queue and the error producers
would have to notify it to start the printing. The simpler alternative would
be using a timed wait but that would delay error logging and activate the
printing thread even when no messages are present.

You could probably wrap such a thing inside a SynchronizedQueuePrintStream
and use System#setErr to prevent code modifications.

Regards,

Silvio
Jul 17 '05 #9

P: n/a
Silvio Bierman wrote:
"Jeff Schwab" <je******@comcast.net> wrote in message
news:Qa********************@comcast.com...
Yes, if necessary, I'll do that. I expect to have hundreds or thousands
of threads at a time vying for the same Writer, though, and I worry that
the performance detriment will be too great.

Is it safe to get the current time simultaneously in different threads,
without synchronization? I only need to see error messages immediately
when the app ( a server ) is first starting. Once things get going,
it's just the order of the messages that needs to be preserved. Maybe I
could use the message-queue idea, and add time-stamps to the messages.
What do you think?
You have that many threads shortly after starting your server? Impressive!


:) Not usually, but I have to be prepared for it.
Using a synchronized queue would work fine and it would probably mean less
time lost due to synchronization when compared to synchronzied println's.
Your print-thread would be wait-ing on the queue and the error producers
would have to notify it to start the printing. The simpler alternative would
be using a timed wait but that would delay error logging and activate the
printing thread even when no messages are present.
Thanks, Silvio, sounds good. Does this mean that PrintWriter isn't
synchronized on its own, or does this mean that we don't know, so I
should do possibly redundant synchronization just to make sure?
You could probably wrap such a thing inside a SynchronizedQueuePrintStream
and use System#setErr to prevent code modifications.


What is "System#setErr"?
Jul 17 '05 #10

P: n/a
nos

"Jeff Schwab" <je******@comcast.net> wrote in message
news:FK********************@comcast.com...
Silvio Bierman wrote:
"Jeff Schwab" <je******@comcast.net> wrote in message
news:Qa********************@comcast.com...
Yes, if necessary, I'll do that. I expect to have hundreds or thousands
of threads at a time vying for the same Writer, though, and I worry that
the performance detriment will be too great.

Is it safe to get the current time simultaneously in different threads,
without synchronization? I only need to see error messages immediately
when the app ( a server ) is first starting. Once things get going,
it's just the order of the messages that needs to be preserved. Maybe I
could use the message-queue idea, and add time-stamps to the messages.
What do you think?
You have that many threads shortly after starting your server? Impressive!
:) Not usually, but I have to be prepared for it.
Using a synchronized queue would work fine and it would probably mean

less time lost due to synchronization when compared to synchronzied println's. Your print-thread would be wait-ing on the queue and the error producers
would have to notify it to start the printing. The simpler alternative would be using a timed wait but that would delay error logging and activate the printing thread even when no messages are present.


Thanks, Silvio, sounds good. Does this mean that PrintWriter isn't
synchronized on its own, or does this mean that we don't know, so I
should do possibly redundant synchronization just to make sure?
You could probably wrap such a thing inside a SynchronizedQueuePrintStream and use System#setErr to prevent code modifications.


What is "System#setErr"?


Let me see, 8,000 threads. If each thread tries to print a single 50
character
message once per minute you will have 576,000,000 bytes of data in one day.

How bout using the logging capability?
Jul 17 '05 #11

P: n/a
nos wrote:
"Jeff Schwab" <je******@comcast.net> wrote in message
news:FK********************@comcast.com...
Silvio Bierman wrote:
"Jeff Schwab" <je******@comcast.net> wrote in message
news:Qa********************@comcast.com...
Yes, if necessary, I'll do that. I expect to have hundreds or thousands
of threads at a time vying for the same Writer, though, and I worry that
the performance detriment will be too great.

Is it safe to get the current time simultaneously in different threads,
without synchronization? I only need to see error messages immediately
when the app ( a server ) is first starting. Once things get going,
it's just the order of the messages that needs to be preserved. Maybe I
could use the message-queue idea, and add time-stamps to the messages.
What do you think?

You have that many threads shortly after starting your server?
Impressive!
:) Not usually, but I have to be prepared for it.

Using a synchronized queue would work fine and it would probably mean


less
time lost due to synchronization when compared to synchronzied
println's.
Your print-thread would be wait-ing on the queue and the error producers
would have to notify it to start the printing. The simpler alternative
would
be using a timed wait but that would delay error logging and activate
the
printing thread even when no messages are present.


Thanks, Silvio, sounds good. Does this mean that PrintWriter isn't
synchronized on its own, or does this mean that we don't know, so I
should do possibly redundant synchronization just to make sure?

You could probably wrap such a thing inside a
SynchronizedQueuePrintStream
and use System#setErr to prevent code modifications.


What is "System#setErr"?

Let me see, 8,000 threads. If each thread tries to print a single 50
character
message once per minute you will have 576,000,000 bytes of data in one day.

How bout using the logging capability?


Where did you come up with your data? I never said 8000 threads all the
time. More like 3000 at a time in short bursts, when a bunch of people
hit a web site. I need the messages to be ordered for debugging purposes.
Jul 17 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.