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();
}
}
}
- duplicates
-
JDK-1237893 Solaris: block on read of System.in blocks all threads
- Closed