Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-1234747

Blocking threads while reading

XMLWordPrintable

    • sparc
    • solaris_2.4


      Try the program below. It should print out a message every second while
      waiting for input. This is not what happens though. Instead it wakes up
      the printer thread each time you enter a line of input.


      import java.io.*;

      class ReadTest extends Thread {
          String str;

          ReadTest(String str) {
      this.str = str;
      start();
          }

          public void run() {
      System.out.println("ready: " + str);
      while (true) {
      try {sleep(1000);} catch (InterruptedException e) {}
      System.out.println(str);
      }
          }

          public static void main(String argv[]) {
      new ReadTest("printer");

      try {
      System.out.println("reader");

      DataInputStream in = new DataInputStream(System.in);
      for (int i = 0 ; i < 100 ; i++) {
      System.out.println("INPUT " + i + " = " +in.readLine());
      try {sleep(1000);} catch (InterruptedException e) {}
      }
      } catch (IOException e) {
      e.printStackTrace();
      }
          }
      }

            tlindholsunw Timothy Lindholm (Inactive)
            ahoffsunw Arthur Hoff (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: