Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-1258776

Can't stop a thread blocked on a socket read.

    XMLWordPrintable

Details

    • sparc
    • solaris_2.5

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

      Attachments

        Issue Links

          Activity

            People

              tlindholsunw Timothy Lindholm (Inactive)
              tkincaidsunw Tom Kincaid (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: