Skip to content

Instantly share code, notes, and snippets.

@mizdra
Created November 21, 2014 16:02
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 mizdra/7d7f58d2f74e9c75b388 to your computer and use it in GitHub Desktop.
Save mizdra/7d7f58d2f74e9c75b388 to your computer and use it in GitHub Desktop.
package futurecanceltest1;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class FutureCancelTest1 {
private static final int task_size = 3; // 起動するタスク数
private static final boolean mayInterruptIfRunning = true; // cancelメソッドに渡す引数
public static void main(String[] args) throws InterruptedException {
ExecutorService executor = Executors.newFixedThreadPool(task_size);
Future[] futures = new Future[task_size];
// タスクを送信
for (int i = 0; i < task_size; i++)
futures[i] = executor.submit(new TestRunnable(i));
// executorが動きっぱなしになってしまうのを回避するためにshutdown
executor.shutdown();
// 1秒待機
Thread.sleep(1000);
// 現在のタスクの状態を出力
for (int i = 0; i < task_size; i++) {
Future f = futures[i];
System.out.printf("index: %d, isCancelled: %b, isDone: %b\n", i, f.isCancelled(), f.isDone());
}
// タスクをcancelして状態を出力
for (int i = 0; i < task_size; i++) {
Future f = futures[i];
System.out.printf("index: %d, cancel: %b, isCancelled: %b, isDone: %b\n", i, f.cancel(mayInterruptIfRunning), f.isCancelled(), f.isDone());
}
}
}
class TestRunnable implements Runnable {
private final int index;
public TestRunnable(int index) {
this.index = index;
}
@Override
public void run() {
try {
System.out.printf("index: %d, message: start\n", index);
Thread.sleep(2000); // 2秒間停止
System.out.printf("index: %d, message: end\n", index);
} catch (InterruptedException ex) {
System.out.printf("index: %d, message: InterruptedException\n", index);
}
}
}
package futurecanceltest2;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class FutureCancelTest2 {
private static final int task_size = 3; // 起動するタスク数
private static final boolean mayInterruptIfRunning = true; // cancelメソッドに渡す引数
public static void main(String[] args) throws InterruptedException {
ExecutorService executor = Executors.newFixedThreadPool(task_size);
Future[] futures = new Future[task_size];
// タスクを送信
for (int i = 0; i < task_size; i++)
futures[i] = executor.submit(new TestRunnable(i));
// executorが動きっぱなしになってしまうのを回避するためにshutdown
executor.shutdown();
// 1秒待機
Thread.sleep(1000);
// 現在のタスクの状態を出力
for (int i = 0; i < task_size; i++) {
Future f = futures[i];
System.out.printf("index: %d, isCancelled: %b, isDone: %b\n", i, f.isCancelled(), f.isDone());
}
// タスクをcancelして状態を出力
for (int i = 0; i < task_size; i++) {
Future f = futures[i];
try {
f.get();
} catch (ExecutionException ex) {
System.out.printf("index: %d, message: ExecutionException\n", i);
}
System.out.printf("index: %d, cancel: %b, isCancelled: %b, isDone: %b\n", i, f.cancel(mayInterruptIfRunning), f.isCancelled(), f.isDone());
}
}
}
class TestRunnable implements Runnable {
private final int index;
public TestRunnable(int index) {
this.index = index;
}
@Override
public void run() {
try {
System.out.printf("index: %d, message: start\n", index);
Thread.sleep(2000); // 2秒間停止
System.out.printf("index: %d, message: end\n", index);
} catch (InterruptedException ex) {
System.out.printf("index: %d, message: InterruptedException\n", index);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment