JDK-1258776 : Can't stop a thread blocked on a socket read.
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 1.0,1.0.2
  • Priority: P1
  • Status: Closed
  • Resolution: Duplicate
  • OS: solaris_2.5
  • CPU: sparc
  • Submitted: 1996-07-11
  • Updated: 2021-11-09
  • Resolved: 1997-02-26
Related Reports
Duplicate :  
Description
Platform Solaris:


Included is a test case that demonstrates the fact that  you can't  stop
a thread that is blocked on a socket read.



import java.net.*;

public class Timeout implements Runnable
{
	public static void main(String args[])
	{
		try
		{
			TestThread th = new TestThread();
			th.run();
			th.join();

			//	(We never get here -- this is the bug)
			System.out.println("TestThread has stopped!");
		}
		catch( Exception e ) { };
	}

	public void run()
	{
	}
}

class TestThread extends Thread
{
	public void run()
	{
		try
		{
			DatagramSocket s = new DatagramSocket();
			DatagramPacket packet = new DatagramPacket(new byte[100],100);

			//	Create a ThreadKiller object to kill this thread in 5 seconds.
			//	We are blocking on the DatagramSocket.receive() function, which
			//	we don't expect to ever receive a packet in this demonstration.
			//	When the ThreadKiller kicks in and calls stop on this thread,
			//	it has no effect.  It should stop the thread   This is the bug.
			//
			System.out.println("Waiting for packet (timeout in 5 seconds)...");

			ThreadKiller killer = new ThreadKiller(this,5000);
			killer.start();
			s.receive(packet);
			killer.Dismiss();

			System.out.println("Exiting TestThread.run()");
		}
		catch( Exception e ){ };
	}
}

/*
**	Thread class to kill other threads after a timeout occurs.
**	The thread sleeps for the specified amount of milliseconds;
**	when it wakes up it kills the target thread unless the
**	Dismiss function is called.
*/
class ThreadKiller extends Thread
{
	boolean 			fDismissed = false;
	long 				fDuration = 5000;
	Thread	 			fTarget = null;

	/***
	*	Constructor
	*	@param targetThread The thread to kill
	*	@param msDuration The amount of time that will elapse before the
	*		target thread is killed.
	*/
	ThreadKiller( Thread targetThread, long msDuration )
	{
		super("ThreadKiller");
		fDuration=msDuration;
		fTarget=targetThread;
	}

	/*
	**	Thread execution.
	*/
	public void run()
	{
		try
		{
			System.out.println("Waking up in "+fDuration+" ms");
			sleep(fDuration);
		}
		catch( InterruptedException e )
		{
			System.out.println("ThreadKiller.run: Exception: "+e);
		}

		if( !fDismissed && fTarget != null ) {
			System.out.println("Stopping thread: "+fTarget);
			fTarget.stop();
		}
	}

	/*
	**	Call to dismiss this thread; when it wakes up it will not kill the
	**	target thread.
	*/
	public void Dismiss()
	{
		System.out.println("Dismissed");
		fDismissed=true;
	}
}

Comments
PUBLIC COMMENTS Can't stop a thread blocked on a socket read.
10-06-2004

EVALUATION Should be just another application of interruptable I/O, which is in the pipe. timothy.lindholm@Eng 1996-12-03
03-12-1996