publicinterfaceScheduledExecutorServiceextendsExecutorService{ public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit); public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit); public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit); public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit); }
publicThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue);
1 2 3 4
publicThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory)// with thread factory
1 2 3 4
publicThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler)// with rejected execution handler
1 2 3 4 5
publicThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, // with thread factory RejectedExecutionHandler handler)// with rejected execution handler
public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit)
分别用于无返回值和有返回值的延迟任务。
1 2 3 4
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
用于固定频率周期的任务,以固定的周期开始下一次执行,不管上一次执行是否完毕。
1 2 3 4
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)
@Override protectedvoidcompute(){ if (end - start < THRESHOLD) { for (int i = start; i <= end; i++) { System.out.println(Thread.currentThread().getName() + " print value " + i); } } else { int mid = (end - start) / 2 + start; PrintTask leftTask = new PrintTask(start, mid); PrintTask rightTask = new PrintTask(mid + 1, end); leftTask.fork(); rightTask.fork(); } } }
执行结果:
1 2 3 4 5 6 7 8 9
ForkJoinPool-1-worker-1 print value 264 ForkJoinPool-1-worker-0 print value 39 ForkJoinPool-1-worker-0 print value 40 ForkJoinPool-1-worker-3 print value 189 ForkJoinPool-1-worker-2 print value 114 ForkJoinPool-1-worker-3 print value 190 ForkJoinPool-1-worker-0 print value 41 ForkJoinPool-1-worker-0 print value 42 ...
publicstaticvoidmain(String[] args)throws InterruptedException, ExecutionException { int[] nums = newint[100]; Random random = new Random(); int total = 0; for (int i = 0; i <100; i++) { nums[i] = random.nextInt(100); total += nums[i]; } System.out.println("Expect result: " + total);
SumTask sumTask = new SumTask(nums, 0, nums.length); ForkJoinPool forkJoinPool = new ForkJoinPool(); Future<Integer> future = forkJoinPool.submit(sumTask); Integer result = future.get(); // 或者可以用以下调用方法 // Integer result = forkJoinPool.invoke(sumTask); System.out.println("Fork and Join result: " + result);