Skip to content

Instantly share code, notes, and snippets.

@danboykis
Created April 18, 2011 12:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danboykis/925249 to your computer and use it in GitHub Desktop.
Save danboykis/925249 to your computer and use it in GitHub Desktop.
FutureTask.cancel
import org.junit.Test;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
public class StopTest {
private final static ExecutorService es = Executors.newFixedThreadPool(10);
@Test
public void testStopTask() throws InterruptedException {
Runnable nonstop = new Runnable() {
public void run() {
int c = 0;
long start = System.currentTimeMillis();
System.out.println(start);
while( true ) { }
}
};
FutureTask nonStopTask = new FutureTask(nonstop,null);
es.execute(nonStopTask);
Thread.sleep(10000L);
nonStopTask.cancel(true);
long end = System.currentTimeMillis();
System.out.println(end);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment