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

(fp.bugs 3060) System.in.read() blocks other threads

    XMLWordPrintable

Details

    • Bug
    • Resolution: Duplicate
    • P3
    • None
    • 1.0
    • core-libs
    • sparc
    • solaris_2.4

    Description


      From: ###@###.### (Delong Yu)



      System.in.read() blocks other threads. If it is suspended in jdb, the other
      threads are running fine, but once it is resumed, all the other threads stop.

      Here is an example (modified Clock.java):

      ance and flashes a lot. (YUCK).
      mport java.awt.Graphics;
      import java.util.Date;
      import java.io.IOException;

      public class Clock extends java.applet.Applet implements Runnable {

          Thread clockThread;
              AnotherThread anotherThread;
              OneMoreThread oneMoreThread;

          public void start() {
              if (clockThread == null) {
                  clockThread = new Thread(this, "Clock");
                  clockThread.start();
              }
              if (oneMoreThread == null) {
                  oneMoreThread = new OneMoreThread();
                  oneMoreThread.start();
              }
              if (anotherThread == null) {
                  anotherThread = new AnotherThread();
                  anotherThread.start();
              }
          }
          public void run() {
              while (clockThread != null) {
                  repaint();
                  try {
                      clockThread.sleep(1000);
                  } catch (InterruptedException e){
                  }
              }
          }
          public void paint(Graphics g) {
              Date now = new Date();
              g.drawString(now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds()
      , 5, 10);
          }
          public void stop() {
              clockThread.stop();
              clockThread = null;
          }
      }

      class AnotherThread extends Thread {


              public AnotherThread () {
              }

              public void run () {

                      try {
                              while(true) {
                                      System.in.read();
                                      System.out.println("read a byte");
                              }
                      } catch(IOException e) {
                      }

              }
      }

      class OneMoreThread extends Thread {


              public OneMoreThread () {
              }

              public void run () {

                      try {
                              while(true) {
                                      System.out.println("before sleep");
                                      this.sleep(1000);
                              }
                      }
                      catch(InterruptedException e) {
                              System.err.println("InterruptedException " +e );
                      }

              }
      }

      Attachments

        Issue Links

          Activity

            People

              duke J. Duke
              hwilliamsunw Headley Williamson (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: