Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion System.out and System.err
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
John B. Matthews  
View profile  
 More options Oct 2 2008, 1:30 pm
Newsgroups: comp.lang.java.programmer
From: "John B. Matthews" <nos...@nospam.invalid>
Date: Thu, 02 Oct 2008 13:30:41 -0400
Local: Thurs, Oct 2 2008 1:30 pm
Subject: Re: System.out and System.err
In article <6kj7spF8625...@mid.individual.net>,
 Jan Thom <jantho...@janthomae.de> wrote:

> On 2008-10-02 00:09:52 +0200, "Mike Schilling"
> <mscottschill...@hotmail.com> said:

> > I think we all assume that access to System.out and System.err is
> > thread-safe; if we write to them from more than one threads, the
> > worst that will happen is output getting interleaved but not lost,
> > nor will exceptions be caused by unsynchronized access (e.g., we
> > don't expect internal bufferr-copying routines to get confused.)  
> > Looking, though, I can't find this guarnteed anywhere.  Can anyone
> > point to an offical statement about it?

> Well the only "official" statement I have found looking at the
> implementation of PrintStream which System.out and System.err are
> internally. The write( .... ) methods are all internally
> synchronized, so they will not produce weird results when multiple
> threads access them:

[...]

Interesting. IIUC, not all implementations are so well behaved, though.
For some years, Michael B. Feldman has taught students of Ada and Java
about threads using code similar to that below, which he kindly shares
with us.

<code>
import  java.util.Random;

/**---------------------------------------------------------------
 * Demo of simple thread class
 * Last Modified: March 2008
 * @author Michael B. Feldman, mfeldman at gwu.edu
 *--------------------------------------------------------------*/

public class ShowThreads {

  public static void main(String args[]) {
    SimpleThread ThreadA = new SimpleThread("A", 5, 1);
    SimpleThread ThreadB = new SimpleThread("B", 7, 21);
    SimpleThread ThreadC = new SimpleThread("C", 4, 41);

    SafeScreen.clearScreen();
    ThreadA.start();
    ThreadB.start();
    ThreadC.start();
  }

} // end ShowThreads

/**---------------------------------------------------------------
 *  Simple Java thread example
 *  Last Modified: March 2008
 *  @author Michael B. Feldman, mfeldman at gwu.edu
 *--------------------------------------------------------------*/

class SimpleThread extends Thread {

  private static final Random random = new Random();
  private String name = "Default";
  private int count = 0;
  private int column = 0;

  // constructor
  public SimpleThread(String name, int count, int column) {
    this.name = name;
    this.count = count;
    this.column = column;
  }

  // run method invoked when 'start' called
  public void run() {
    for(int i = 1; i <= count; i++) {
      int nap = random.nextInt(7) + 1;
      SafeScreen.write(name + " naps " + nap + " secs", i, column);
      try {
        sleep(nap * 1000);
      }
      catch(InterruptedException e) {} // ignored
    }
  }

} // end class SimpleThread

/**---------------------------------------------------------------
 * Thread-safe Mini-terminal controller for vt100 (ANSI) terminals
 * Translated from C to Java February 2001
 * @author Michael B. Feldman, mfeldman at gwu.edu
 *--------------------------------------------------------------*/

class SafeScreen {

  public static synchronized void clearScreen() {
    System.out.print("\033" + "[2J");
  }

  public static synchronized void write(
      String item, int row, int col) {
    System.out.println("\033" + "[" + row + ";" + col + "f" + item);
  }

} // End of class SafeScreen

</code>

[The code is Professor Feldman's; any errors are mine.]

--
John B. Matthews
trashgod at gmail dot com
home dot woh dot rr dot com slash jbmatthews


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.