ScheduledExecutorServiceソース分析
3880 ワード
詳細
テスト:
scheduled出力(遅延100+1000ミリ秒印刷)
1505538732156 : scheduled...
1505538733257 result : MyTask is done
scheduledAtFixedRateTest出力(3000ミリ秒間隔=max(2000,3000):
1505538684893 :scheduledAtFixedRate...
1505538687996 : MyCmd is done!
1505538690997 : MyCmd is done!
scheduledAtFixedDelayTest出力(5000ミリ秒間隔=2000+3000):
1505536678015 :scheduledAfFixedDelay...
1505536681117 : MyCmd is done!
1505536686118 : MyCmd is done!
public interface ScheduledExecutorService extends ExecutorService {
//
public ScheduledFuture> schedule(Runnable command,
long delay, TimeUnit unit);
// , callable
public ScheduledFuture schedule(Callable callable,
long delay, TimeUnit unit);
//
// , , 。
public ScheduledFuture> scheduleAtFixedRate(Runnable command,
long initialDelay,
long period,
TimeUnit unit);
// 。
// : delay , 。
public ScheduledFuture> scheduleWithFixedDelay(Runnable command,
long initialDelay,
long delay,
TimeUnit unit);
}
テスト:
public class ScheduledExecutorServiceTest {
private static final int POOL_SIZE = 4;
private static ScheduledExecutorService ses = null;
@org.junit.Before
public void setUp() {
ses = Executors.newScheduledThreadPool(POOL_SIZE);
}
@After
public void tearDown() {
ses.shutdown();
}
@Test
public void scheduled() throws ExecutionException, InterruptedException {
MyCall task = new MyCall();
System.out.println(System.currentTimeMillis() + " : scheduled...");
// 100+1000
ScheduledFuture future = ses.schedule(task, 100, TimeUnit.MILLISECONDS);
String result = future.get();
System.out.println(System.currentTimeMillis() + " result: " + result);
}
@Test
public void scheduledAtFixedRateTest() throws InterruptedException {
MyCmd myCmd = new MyCmd();
System.out.println(System.currentTimeMillis() + " : scheduledAtFixedRate...");
// 100 , max(2000,3000)
ses.scheduleAtFixedRate(myCmd, 100, 2000, TimeUnit.MILLISECONDS);
TimeUnit.MILLISECONDS.sleep(60000);
}
@Test
public void scheduledAfFixedDelayTest() throws InterruptedException {
MyCmd myCmd = new MyCmd();
System.out.println(System.currentTimeMillis() + " : scheduledAfFixedDelay...");
// 100 , 5000
ses.scheduleWithFixedDelay(myCmd, 100, 2000, TimeUnit.MILLISECONDS);
TimeUnit.MILLISECONDS.sleep(60000);
}
static class MyCall implements Callable {
@Override
public String call() throws Exception {
TimeUnit.MILLISECONDS.sleep(1000);
return "MyTask is done";
}
}
static class MyCmd implements Runnable {
@Override
public void run() {
try {
TimeUnit.MILLISECONDS.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(System.currentTimeMillis() + " : MyCmd is done!");
}
}
}
scheduled出力(遅延100+1000ミリ秒印刷)
1505538732156 : scheduled...
1505538733257 result : MyTask is done
scheduledAtFixedRateTest出力(3000ミリ秒間隔=max(2000,3000):
1505538684893 :scheduledAtFixedRate...
1505538687996 : MyCmd is done!
1505538690997 : MyCmd is done!
scheduledAtFixedDelayTest出力(5000ミリ秒間隔=2000+3000):
1505536678015 :scheduledAfFixedDelay...
1505536681117 : MyCmd is done!
1505536686118 : MyCmd is done!