473,699 Members | 2,834 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
11 8440
nos

"Jeff Schwab" <je******@comca st.net> wrote in message
news:FK******** ************@co mcast.com...
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" ?


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
nos wrote:
"Jeff Schwab" <je******@comca st.net> wrote in message
news:FK******** ************@co mcast.com...
Silvio Bierman wrote:
"Jeff Schwab" <je******@comca st.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
synchronize d 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" ?

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2768
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? with-apxs2=/usr/local/apache2/bin/apxs with-gd enable-gd-native-ttf
4
15616
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 problem if a sender could end his turn with a pre-defined character or string (e.g. "\n", "bye" or...
0
1343
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. The services are always initiated from within ASP where they are authenticated and authorized....
9
3185
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
2914
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 websession get a thread? If so, if two users are calling the static function at the same time, do they
3
2173
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
3453
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 threadsafe will this be enough to make
0
1160
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
1761
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
8685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9171
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8905
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 most users, this new feature is actually very convenient. If you want to control the update process,...
1
6532
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4373
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4625
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3053
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
2
2342
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.