Some logistical questions re: threads

Discussion in 'Java' started by Jeremy, Jun 29, 2003.

  1. Jeremy

    Jeremy Guest

    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
    Jeremy, Jun 29, 2003
    #1
    1. Advertising

  2. Jeremy

    Keeger Guest

    "Jeremy" <> wrote in message news:<ZAuLa.271$>...
    > 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...
    Keeger, Jun 30, 2003
    #2
    1. Advertising

  3. "Jeremy" <> wrote in message
    news:ZAuLa.271$...
    > 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.iviz.com/
    Matt Humphrey, Jun 30, 2003
    #3
  4. 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.
    David Zimmerman, Jun 30, 2003
    #4
  5. 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
    Steve Horsley, Jun 30, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. yoda
    Replies:
    2
    Views:
    424
    =?utf-8?Q?Bj=C3=B6rn_Lindstr=C3=B6m?=
    Aug 1, 2005
  2. threads without threads

    , Aug 27, 2004, in forum: C Programming
    Replies:
    4
    Views:
    388
    William Ahern
    Aug 27, 2004
  3. Pedro Pinto

    Java Threads - Get running threads

    Pedro Pinto, Apr 8, 2008, in forum: Java
    Replies:
    2
    Views:
    1,415
    Arne Vajhøj
    Apr 9, 2008
  4. Une bévue
    Replies:
    0
    Views:
    137
    Une bévue
    Jun 14, 2006
  5. Junkone

    some threads are not starting

    Junkone, Jan 3, 2008, in forum: Ruby
    Replies:
    2
    Views:
    94
    Junkone
    Jan 3, 2008
Loading...

Share This Page