Source Code : Another way to stop a thread
Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com.
Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com.
Click Here or search from google with Libraries Name you get jar file related it
Another way to stop a thread

public class AlternateStop extends Object implements Runnable {
private volatile boolean stopRequested;
private Thread runThread;
public void run() {
runThread = Thread.currentThread();
stopRequested = false;
int count = 0;
while (!stopRequested) {
System.out.println("Running ... count=" + count);
count++;
try {
Thread.sleep(300);
} catch (InterruptedException x) {
// re-assert interrupt
Thread.currentThread().interrupt();
}
}
}
public void stopRequest() {
stopRequested = true;
if (runThread != null) {
runThread.interrupt();
}
}
public static void main(String[] args) {
AlternateStop as = new AlternateStop();
Thread t = new Thread(as);
t.start();
try {
Thread.sleep(2000);
} catch (InterruptedException x) {
}
as.stopRequest();
}
}
Thank with us