Created
April 18, 2011 12:41
-
-
Save danboykis/925249 to your computer and use it in GitHub Desktop.
FutureTask.cancel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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