This is the mail archive of the java@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Unblocking blocked I/O


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() ); 
        }
    }
}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]