so, lets say I have a thread that looks like this:

public run() {
   boolean active = true;
   while (active) {
     //do some action that takes a really long time
   }
}

Is there any way I can directly stop and destroy this thread in the middle of the action which takes a really long time to complete? Basically, I want to instantly kill this thread. The other way that I see is to set active to false, but the thread wont actually stop until it completes its current action.

It may be hard to imagine where this is a problem, but the action I'm speaking of is a ftp transfer of a very large file, using this thread. I need a way to abort the transfer, instantly. Suggestions?

Edited 6 Years Ago by TheWhite: n/a

Call stop() method to kiill the thread.

import java.lang.Runnable;

public class T implements Runnable{

   Thread timer = null;

   public void start() {
   if(timer == null)
        {
        timer = new Thread(this);
        timer.start();      
        }
   }
   public void stop() {
        timer = null;
   }

   public void run( ) {

        while (timer != null) {
        try {Thread.sleep(100);} catch (InterruptedException e) {}
     		// do something
        }
        timer = null;
   }
}
This article has been dead for over six months. Start a new discussion instead.