This is the mail archive of the
java@gcc.gnu.org
mailing list for the Java project.
Re: Unblocking blocked I/O
- To: Bryce McKinlay <bryce at waitaki dot otago dot ac dot nz>
- Subject: Re: Unblocking blocked I/O
- From: David Vrabel <dv207 at hermes dot cam dot ac dot uk>
- Date: Wed, 19 Sep 2001 09:18:29 +0100 (BST)
- cc: <java at gcc dot gnu dot org>
Hi,
On Wed, 19 Sep 2001, Bryce McKinlay wrote:
> David Vrabel wrote:
>
> > [...]
> >I also tried Thread.interrupt() but that seemed completely broken.
> >
> This discussion has come up before. Last time, I think we agreed that
> the semantics of Thread.interrupt() were pretty much undefined when it
> comes to blocking I/O, and since its implementation varies significantly
> between platforms we wern't going to bother implementing it at all
> unless Sun fix it or clarify the spec.
Yeah. I found that discussion. So much for the claimed cross-platform
compatibility...
> Unblocking I/O by closing a socket (resource revocation), however,
> should work, and its a bug if it doesn't. Do you have a test case?
Yes. It's perhaps a bit long but find it attached. Hmm. Attachments are
okay aren't they?
David Vrabel
/* Unblock.java - try to unblock a blocked thread.
Copyright 2001 Arcom Control Systems Ltd
*/
import java.io.*;
import java.lang.*;
import java.net.*;
public class Unblock {
public static void main( String[] args ) {
if( args.length < 2 ) {
System.err.println( "Usage: DontClose <host name> <port>" );
return;
}
Unblock u = new Unblock();
u.go( args );
}
public void go( String[] args )
{
Socket socket = null;
try {
socket = new Socket( args[0], Integer.parseInt( args[1] ) );
}
catch( IOException e ) {
System.out.println( "connection failed: " + e.getMessage() );
return;
}
Reader r = new Reader( socket );
r.start();
try {
Thread.currentThread().sleep( 5000 );
}
catch( InterruptedException e ) {}
r.terminate();
}
}
class Reader extends Thread {
private Socket socket;
private boolean killThread;
public Reader( Socket s )
{
socket = s;
killThread = false;
}
public void run()
{
while( !killThread ) {
byte[] buf = new byte[50];
try {
socket.getInputStream().read( buf );
System.out.println( "read" );
}
catch( IOException e ) {
System.out.println( "read: I/O exception: " + e.getMessage() );
}
catch( Exception e ) {
System.out.println( "exception: " + e.getMessage() );
}
}
System.out.println( "thread end" );
}
public void terminate()
{
killThread = true;
System.out.println( "closing socket" );
try {
socket.close();
System.out.println( "closed socket" );
}
catch( IOException e ) {
System.out.println( "close: I/O exception: " + e.getMessage() );
}
}
}