Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Some logistical questions re: threads

Reply
Thread Tools

Some logistical questions re: threads

 
 
Jeremy
Guest
Posts: n/a
 
      06-29-2003
Well, just one question, primarily.

Consider the following scenario: Two threads are running in parallel. Each
thread has called System.setOut(PrintStream myOut) to redirect output from
its own printstream ("myOut") to the output. So what happens when thread A
writes to its System.out, and what happens when thread B writes to
System.out? To which thread's "myOut" does the output go? I assume that
the last thread to call System.setOut will get all everything to its myOut,
but that hardly seems fair to the first thread?

This situation should really never matter, but in my situation it does
Also, it is an interesting question - System resources are not thread-safe
and could be hijacked by another thread.

And I guess the question is, is there a way to give different threads their
own System.out, such that they can write to System.out and be able to
predict where it is going?

Thanks,
-Jeremy


 
Reply With Quote
 
 
 
 
Keeger
Guest
Posts: n/a
 
      06-30-2003
"Jeremy" <(E-Mail Removed)> wrote in message news:<ZAuLa.271$(E-Mail Removed)> ...
> Well, just one question, primarily.
>
> Consider the following scenario: Two threads are running in parallel. Each
> thread has called System.setOut(PrintStream myOut) to redirect output from
> its own printstream ("myOut") to the output. So what happens when thread A
> writes to its System.out, and what happens when thread B writes to
> System.out? To which thread's "myOut" does the output go? I assume that
> the last thread to call System.setOut will get all everything to its myOut,
> but that hardly seems fair to the first thread?
>
> This situation should really never matter, but in my situation it does
> Also, it is an interesting question - System resources are not thread-safe
> and could be hijacked by another thread.
>
> And I guess the question is, is there a way to give different threads their
> own System.out, such that they can write to System.out and be able to
> predict where it is going?
>
> Thanks,
> -Jeremy



Well, I don't know anything about the System object in terms of
thread-safing, but if you have 2 threads and need to output to 2
different sources, why not just write to a file? Or a GUI component?
You could make 2 textPanes and just output there...
 
Reply With Quote
 
 
 
 
Matt Humphrey
Guest
Posts: n/a
 
      06-30-2003

"Jeremy" <(E-Mail Removed)> wrote in message
news:ZAuLa.271$(E-Mail Removed).. .
> Well, just one question, primarily.
>
> Consider the following scenario: Two threads are running in parallel.

Each
> thread has called System.setOut(PrintStream myOut) to redirect output from
> its own printstream ("myOut") to the output. So what happens when thread

A
> writes to its System.out, and what happens when thread B writes to
> System.out? To which thread's "myOut" does the output go? I assume that
> the last thread to call System.setOut will get all everything to its

myOut,
> but that hardly seems fair to the first thread?
>
> This situation should really never matter, but in my situation it does
> Also, it is an interesting question - System resources are not thread-safe
> and could be hijacked by another thread.
>
> And I guess the question is, is there a way to give different threads

their
> own System.out, such that they can write to System.out and be able to
> predict where it is going?
>
> Thanks,
> -Jeremy


This is an easy question to test--try it out! System.setOut is universal,
so it seems the last one wins, assuming the call itself is threadsafe and
the does not itself conflict.

Cheers,
Matt Humphrey http://www.velocityreviews.com/forums/(E-Mail Removed) http://www.iviz.com/


 
Reply With Quote
 
David Zimmerman
Guest
Posts: n/a
 
      06-30-2003


Jeremy wrote:

> And I guess the question is, is there a way to give different threads their
> own System.out, such that they can write to System.out and be able to
> predict where it is going?
>


Write a print stream that uses the current thread to look up print
streams in a hashtable. That way you can have a different print stream
for each thread.

 
Reply With Quote
 
Steve Horsley
Guest
Posts: n/a
 
      06-30-2003
On Sun, 29 Jun 2003 05:14:33 +0000, Jeremy wrote:

> Well, just one question, primarily.
>
> Consider the following scenario: Two threads are running in parallel.
> Each thread has called System.setOut(PrintStream myOut) to redirect
> output from its own printstream ("myOut") to the output. So what
> happens when thread A writes to its System.out, and what happens when
> thread B writes to System.out? To which thread's "myOut" does the
> output go? I assume that the last thread to call System.setOut will get
> all everything to its myOut, but that hardly seems fair to the first
> thread?
>
> This situation should really never matter, but in my situation it does
> Also, it is an interesting question - System resources are not
> thread-safe and could be hijacked by another thread.
>
> And I guess the question is, is there a way to give different threads
> their own System.out, such that they can write to System.out and be able
> to predict where it is going?
>
> Thanks,
> -Jeremy


There is only one System.out stream. It is synchronized so that individual
calls to print() and println() are atomic. I believe that in general, most
system resources are synchronized where necessary - I can't think of any
that are not. That doesn't prevent fights between threads at a higher
level though, such as where to redirect System.out.

Probably the best thing is for each thread to open and maintain its own
PrintWriter to its own output stream.

Steve
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Threads - Get running threads Pedro Pinto Java 2 04-08-2008 11:44 PM
TB View, Threads, Threads with unread The Invisible Man Firefox 1 03-20-2006 02:09 AM
Standard Threads vs Weightless Threads yoda Python 2 08-01-2005 09:12 PM
threads without threads sindica@gmail.com C Programming 4 08-27-2004 09:25 PM
Some questions regarding 070-305 and hopefully some right answers. Needs correction... wink, wink ;-) Daniel Walzenbach MCSD 1 11-10-2003 12:25 AM



Advertisments