how to set a infinite loop and break it. (Java threads)
java, multithreading
Solution
Assuming you're running on JDK 1.5 or newer (where memory model was clarified and improved) you can use
public class MyRunnable extends Runnable
{
private volatile boolean cancelled;
public void run() {
while (!cancelled) {
doStuff();
}
}
public void cancel()
{
cancelled = true;
}
public boolean isCancelled() {
return cancelled;
}
}j
Alternatively, use java.util.concurrent.Future, and FutureTask, which supports cancellation out of the box.
Problem
i have set a thread and i want to run it using a loop. so this thread should run in the loop and break in a certain time and again run the loop. please i have no clue how to do this. can someone guide me.