Name: bsC130419 Date: 06/08/2001
java version "1.4.0-beta"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta-b65)
Java HotSpot(TM) Client VM (build 1.4.0-beta-b65, mixed mode)
On Windows NT 4.0
java.nio.channels.Selector.wakeup() does not make the next Selector.select()
call return immediately. The call behaves as wakeup() was never called before,
i.e. it blocks.
This does not hapen on Linux 2.4.
Test case:
/* SelectorWakeupTest.java
*/
package com.mn.tools.test.bugs.java;
import java.io.IOException;
import java.nio.channels.Selector;
/**
Tests wether wakeup properly makes the next select() call return immediately.
*/
public class SelectorWakeupTest {
public static void main(String argv[]) {
try {
int waitTime = 2000;
Selector selector = Selector.open();
selector.wakeup();
long t1 = System.currentTimeMillis();
selector.select(waitTime);
long t2 = System.currentTimeMillis();
System.out.println("A waked select("+waitTime+") needed "+(t2-t1)+" milliseconds to
get awaken.");
} catch (IOException e) {
System.err.print("caught: ");
e.printStackTrace();
}
}
}
Running this test case on Windows NT, I get:
A waked select() of 2000 milliseconds needed 2013 milliseconds to get awaken.
on linux, I get:
A waked select() of 2000 milliseconds needed 180 milliseconds to get awaken.
java -version on the linux box:
java version "1.4.0-beta"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta-b65)
Java HotSpot(TM) Client VM (build 1.4.0-beta-b65, interpreted mode)
Impact: with this bug, java.nio is useless.
(Review ID: 126050)
======================================================================
Name: bsC130419 Date: 06/14/2001
java version "1.4.0-beta"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta-b65)
Java HotSpot(TM) Client VM (build 1.4.0-beta-b65, mixed mode)
/*
Either the wakeup() method or the close() method can not interrupt the
blocking thread named "T"
*/
import java.nio.channels.*;
public class SelectorTest {
Selector sel = null;
boolean flag = false;
public SelectorTest() throws Exception {
sel = Selector.open();
}
public void test() throws Exception {
new T().start();
Thread.currentThread().sleep(1000);
sel.wakeup();
Thread.currentThread().sleep(1000);
System.out.println("flag: " + flag);
sel.close();
Thread.currentThread().sleep(1000);
System.out.println("flag: " + flag);
System.exit(0);
}
class T extends Thread {
public T() {
setName("T");
}
public void run() {
try{
int n = sel.select();
}catch (java.io.IOException e) {
e.printStackTrace();
}
flag = true;
}
}
public static void main(String[] args) throws Exception {
new SelectorTest().test();
}
}
(Review ID: 126588)
======================================================================
2001-08-28
Another customer experiencing this bug with build 77:
The wakeup() call never unblocks the Selector.select() call. Without
this, it is impossible for me to add new entries into my poll set. I
looked at Sun's implementation and it appears that the first entry in
the poll set is the read side of a pipe. The wakeup() code writes a
bogus byte to the pipe to get the poll (WSAWaitForMultipleEvents) call
to wakeup. Based on this, it looks like the attached code sample should
work, but it does not.
D:\bill\nio>java -version
java version "1.4.0-beta2"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta2-b77)
Java HotSpot(TM) Client VM (build 1.4.0-beta2-b77, mixed mode)
The output:
D:\bill\nio>java wakeup
BEGIN SELECT
waking...
waking...
waking...
Test case provided by customer:
import java.nio.channels.*;
public class wakeup {
public static void main(String[] args) throws Exception {
final Selector selector = Selector.open();
Thread t = new Thread() {
public void run() {
try {
while (true) {
System.err.println("BEGIN SELECT");
selector.select();
System.err.println("END SELECT");
}
} catch (Throwable thr) {
System.err.println("THR: " + thr);
thr.printStackTrace();
System.err.println("selector thread stopping");
}
}
};
t.start();
while (true) {
Thread.sleep(2000);
System.err.println("waking...");
selector.wakeup();
}
}
}