Name: dbT83986 Date: 02/15/99
Problem description:
I try to unblock a call to ServerSocket.accept() by calling thread.interrupt from a different thread.
I create a thread that does a call to ServerSocket.accept(), and after 5 seconds the main thread try to interrupt the accept call by calling thread.interrupt.
This works on Solaris using the jdk, and it works on NT using Microsoft VJ++ 6.0.
It does NOT work using the jdk1.1.6 from javasoft on NT, and it does NOT work using Inprise JBuilder2 on NT.
I've added a call to setSoTimeout, but it makes no difference. In both cases the program hangs until the timeout occurs on the ServerSocket.
Exact steps to reproduce the problem:
-> Compile and run the included source code.
Java SOURCE CODE that demonstrates the problem:
BEGIN SOURCE:
import java.net.ServerSocket;
import java.net.Socket;
import java.io.IOException;
import java.io.InterruptedIOException;
class TestSocket implements Runnable
{
public TestSocket() throws IOException
{
srvsock = new ServerSocket(3000);
srvsock.setSoTimeout(20000); // I don't need this on Solaris
// and it does not help on NT ?
}
public void run()
{
try
{
thread = Thread.currentThread();
System.out.println("Waiting for connections...");
Socket s = srvsock.accept();
System.out.println("Connection accepted.");
}
catch (InterruptedIOException e)
{
System.out.println("Accept interrupted.");
}
catch (IOException e)
{
System.out.println("IOException!");
}
finally
{
System.out.println("Terminating.");
}
}
public void stop()
{
if (thread != null)
thread.interrupt();
}
private ServerSocket srvsock;
private Thread thread;
}
public class TestInterrupt
{
public TestInterrupt()
{
}
public static void main(String[] args)
{
try
{
TestSocket ts = new TestSocket();
Thread t = new Thread(ts);
t.start();
System.out.println("Going to sleep...");
Thread.currentThread().sleep(5000);
System.out.println("Waking up, trying to stop other thread gracefully");
ts.stop();
t.join();
}
catch (Exception e)
{
System.out.println(e.toString());
}
finally
{
System.out.println("That's it! Goodbye.");
}
}
}
END SOURCE.
(Review ID: 42996)
======================================================================
- duplicates
-
JDK-4154947 JDK 1.1.6, 1.2/Windows NT: Interrupting a thread blocked does not unblock IO
- Closed
- relates to
-
JDK-4403952 remove dependency on Thread.interrupt()
- Resolved