How can a thread kill another blocked thread?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: How can a thread kill another blocked thread?

  1. #1
    Join Date
    Sep 2005
    Posts
    4

    Angry How can a thread kill another blocked thread?

    Hello,

    As the title mentioned, lets say i a main class in which i created a new thread but for some reason, this thread is blocked so i want to find a way to kill this thread from the main class (the interrupt function is not working), is there a workarround to do this?

    Thank you in advance

    Flovet

  2. #2
    Join Date
    Jul 2005
    Posts
    8
    Generally, there are two ways to kill a thread:

    1. Set value of some shared var, so that the thread reads it and terminates voluntarily.

    2. Invoke Thread.stop() which is deperecated and unsafe.

    In your example, Thread.interrupt() does not help, so the thread is not locked, it is executing some code. So, you should add checks like (1) to that code.

    Hope this helps

    Denis.

    --------------------
    http://www.excelsior-usa.com/jet.html
    JVM with AOT compilation

  3. #3
    Join Date
    Sep 2005
    Posts
    4
    Hello,
    The thread is in a runnable state, and it is executing some piece of code which never returns , like for exmaple:

    SimpleThread task = new SimpleThread()
    task.start();

    on the other hand the SimpleThread's run method contain the following code:
    .......
    RemoteLogin rlogin = new RemoteLogin(); // call to a remote class
    /*the initialization above does not return due to some network problems so the thread keeps on waiting for infinit time, i want to kill this thread, is there a good way to do this?*/


    Thank in advance

  4. #4
    Join Date
    Sep 2005
    Location
    istanbul / Turkey
    Posts
    133
    i dont know about RemoteControl class,
    generally
    if a library doesn't offer sometihng better,
    this logic may help.

    Code:
    public class SyncTest {
    
      static boolean connected = false;
      static int goodluck = 0;
      static Random rand = new Random(System.currentTimeMillis());
      
      public static void main(String[] args)  {
    
        Thread t = new Thread() {
          public void run() {
            // if random number is 1 we will be connected
            while (goodluck != 1) {
              System.out.println(".");
              goodluck = rand.nextInt(1000);
            }; 
            connected = true;
          }
        };
        
        t.start();
    //wait either some seconds passed or thread finished
        t.join(1000); 
        
        if (!connected) {
          t.stop();//deprecated method stop()...
          System.out.println("couldnt connect and terminated");
          System.exit(1);
        }
        else {
          System.out.println("connected");
        };
      }
    }

  5. #5
    Join Date
    Jul 2005
    Location
    SW MO, USA
    Posts
    299
    If you know what resource the thread is hanging on, try closing it.

  6. #6
    Join Date
    Sep 2005
    Posts
    4
    Hello,
    I am working in a web environment, the code that i am executing is run within a jsp so the System.exit could stop the whole application and not just this particular thread

    i tried the stop() method but still does not work, it is not killing the thread :S

    any suggestions?

    Thks for the code mr1yh1

  7. #7
    Join Date
    Jul 2005
    Location
    SW MO, USA
    Posts
    299
    I think the successful killing of a blocked thread is related to how its being blocked. If waiting for I/O close the resource.
    What is your code blocking on?

Similar Threads

  1. Replies: 2
    Last Post: 05-30-2005, 07:10 AM
  2. Data access with thread
    By Edwin in forum .NET
    Replies: 4
    Last Post: 08-02-2002, 06:11 AM
  3. Infragistics/Sheridan new registration policy
    By Lowell Williams in forum vb.announcements
    Replies: 18
    Last Post: 02-10-2001, 06:22 AM
  4. Replies: 1
    Last Post: 10-29-2000, 05:51 PM
  5. Replies: 2
    Last Post: 10-27-2000, 04:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center