United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-4514257 : Thread.interrupt() on InputStream.read() fails

Details
Type:
Bug
Submit Date:
2001-10-12
Status:
Open
Updated Date:
2009-07-17
Project Name:
JDK
Resolved Date:
Component:
core-libs
OS:
linux
Sub-Component:
java.io
CPU:
x86
Priority:
P4
Resolution:
Unresolved
Affected Versions:
1.3.1
Targeted Versions:

Related Reports
Relates:

Sub Tasks

Description
Name: gm110360			Date: 10/12/2001


java version "1.3.1"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1-b24)
Java HotSpot(TM) Client VM (build 1.3.1-b24, mixed mode)


uname -a
Linux <HOSTNAME> 2.4.3-20mdk #1 Sun Apr 15 23:03:10 CEST 2001 i686 unknown


Thread.interrupt doesn't work for InputStream.read(). This is related to bug
#4344135, but doesn't involve closing of an underlying socket.
The program below shows that the thread cannot be interrupted.

The program works fine under Solaris 8 Update 5 with JDK 1.3.1.



import java.net.*;

/** Tests Thread.interrupt() against InputStream.read() */
public class test2 {

    static class SleeperThread extends Thread {
	public void run() {
	    int c;
	    try {
		c=System.in.read();
	    }
	    catch(Exception ex) {
		System.err.println("test2.SleeperThread.run(): " + ex);
	    }
	}
    }

    
    public static void main(String[] args) {
	Thread     thread;
	final long TIMEOUT=3000;

	thread=new SleeperThread();
	thread.start();
	System.out.println("test2.main(): sleeping for " + TIMEOUT + " msecs");
	try {thread.sleep(TIMEOUT);} catch(Exception e) {}
	System.out.println("test2.main(): sleeping -- done");
	
	System.out.println("test2.main(): interrupting thread");
	thread.interrupt();
	System.out.println("test2.main(): interrupting thread -- done");
	
	System.out.println("test2.main(): joining thread (timeout=" + TIMEOUT + "
msecs)");
	try {thread.join(TIMEOUT);} catch(Exception e) {}
	System.out.println("test2.main(): joining thread -- done");

	System.out.println("test2.main(): thread.isAlive()=" + thread.isAlive());
	if(thread.isAlive()) {
	    System.out.println("test2.main(): thread was interrupted but is still
running !");
	}
    }
}
(Review ID: 133623) 
======================================================================

                                    

Comments
EVALUATION

An approximately equal number of people are annoyed by thread interruption having a side effect on I/O as are annoyed by lack of thread interrupts interrupting I/O. Please see the 4385444 evaluation for additional information about the disposition of this and related problems stemming from nonuniform support for thread interruption causing I/O interruption.
                                     
2006-10-27
EVALUATION

This is the opposite of 4329256 "interrupted thread no longer produces output (sol)" and demonstrates more problems raised with behavior that isn't uniformly available. See the notes, especially the "workarounds" for 4329256 for more details.
                                     
2006-02-28
WORK AROUND



Name: gm110360			Date: 10/12/2001


NONE: we cannot close System.in
======================================================================
                                     
2004-06-11



Hardware and Software, Engineered to Work Together