473,569 Members | 2,735 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is PrintWriter threadsafe?

Is it OK for multiple threads to use the same PrintWriter concurrently?
Jul 17 '05 #1
11 8418

"Jeff Schwab" <je******@comca st.net> wrote in message
news:rY******** ************@co mcast.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
Silvio Bierman wrote:
"Jeff Schwab" <je******@comca st.net> wrote in message
news:rY******** ************@co mcast.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
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******@comca st.net> wrote in message
news:eu******** ************@co mcast.com...
Silvio Bierman wrote:
"Jeff Schwab" <je******@comca st.net> wrote in message
news:rY******** ************@co mcast.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
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
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
Hello Jeff,

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

Silvio Bierman
"Jeff Schwab" <je******@comca st.net> wrote in message
news:3N******** ************@co mcast.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
Silvio Bierman wrote:
Hello Jeff,

You could do all println calls inside synchronized(so meSingleton) 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******@comca st.net> wrote in message
news:3N******** ************@co mcast.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

"Jeff Schwab" <je******@comca st.net> wrote in message
news:Qa******** ************@co mcast.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 SynchronizedQue uePrintStream
and use System#setErr to prevent code modifications.

Regards,

Silvio
Jul 17 '05 #9
Silvio Bierman wrote:
"Jeff Schwab" <je******@comca st.net> wrote in message
news:Qa******** ************@co mcast.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 SynchronizedQue uePrintStream
and use System#setErr to prevent code modifications.


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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2761
by: Jason Morehouse | last post by:
Hello, Just wondering if anyone is using the apache worker module with php? I've complied php5 with zend threadsafe support, and apache2 with the MPM worker module on a Linux box. Everything seems sweet. mysqli_thread_safe() reports true... anyone know if this configuration may include modules that aren't threadsafe? ...
4
15608
by: Dr.Kadzija | last post by:
i have a client-server application. client and server should communicate via tcp sockets. ok, so i use Sockets, PrintWriter and BufferedReader. the problem is that: both client and server will send each other multiple lines (using PrintWriter.println()) at a time, and i don't know how many lines each of them will send. this wouldn't be a...
0
1341
by: Stephanie Stowe | last post by:
Hi. I have been posting like a looney on an issue I am working on. I will reiterate the background since you all do not want to remember my issues! I have an ASP app (biggish). We are creating the first of many JSP apps. Until such time as we re-engineer our ASP apps for our new JSP / Java / Websphere environment, we need a session bridge. ...
9
3174
by: John | last post by:
If a value type is immutable, I guess it's threadsafe to read it? But not threadsafe to assign a new value to it (can any value type be truely immutable? Isn't assigning a totally new value to it, like doing an modification, when no references are involved? I don't know enough about CLR) At the moment the whole: lock(anobject) {
2
2910
by: David | last post by:
I have a static method in the dll that looks like this public static int myStaticFunction(int input){ } My question is, should I use a mutex inside the function to ensure that the function is threadsafe? This is used for ASP.NET. I am asking this question without knowing how IIS works as a multi-threaded application. Does each...
3
2167
by: Diffident | last post by:
Hello All, Following is a static method. Can you please tell me if this is threadsafe...why? If it is not threadsafe...why? Thank you. public static string GetCachedApplicationWideObject(string HashtableKey) { //Cache holds the hashtable
2
3444
by: Macca | last post by:
Hi, I need a data structure such as an Array/ArrayList/Generic List to hold multiple instances of a user derfined class. This array will be accessed across multiple threads. However I dont want to lock the array itself as this will cause poor performance due to the array being accessed frequently. If I make the User defined class...
0
1155
by: sajithamol | last post by:
how does the PrintWriter class takes and prints the HTML code as response if i simply give PrintWriter pw = response.getWriter(); pw.println(" herehtml codes"); in doPost method
1
1752
by: Anand Murali | last post by:
My coding is as follows: import java.io.*; class Sample { public static void main(String args) { try { int a = new int;
0
7921
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8118
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7666
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5504
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2107
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1208
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.