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?

Recommended reading; post again if you get stuck with implementation. Also do have a look at the links mentioned in the comments there.

Can you get to the connection and close it?

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;
   }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.