-
线程的实现方式
-
java 多线程之 extends Thread
-
java 多线程之 implements Runnable
-
java 多线程之 implements Callable
-
-
线程池的使用
-
线程池简介
-
ThreadPoolExecutor
-
java 线程池之 newScheduledThreadPool
-
java 线程池之 newCachedThreadPool
-
java 线程池之 newFixedThreadPool
-
java 线程池之 newSingleThreadExecutor
-
-
ForkJoinPool
-
java 线程池之 newWorkStealingPool
-
-
Completab leFuture
-
CompletableFuture 的使用示例
-
-
newScheduledThreadPool 和 其他线程池最大的区别是使用的阻塞队列是 DelayedWorkQueue,而且多了两个定时执行的方法scheduleAtFixedRate和scheduleWithFixedDelay
public ScheduledThreadPoolExecutor(int corePoolSize) {super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,new DelayedWorkQueue()); }
示例代码:
public class ScheduledThreadPoolTest {// 线程数private static final int threads = 10;// 用于计数线程是否执行完成CountDownLatch countDownLatch = new CountDownLatch(threads);/*** newScheduledThreadPool execute** @throws ExecutionException* @throws InterruptedException*/@Testpublic void test1() throws InterruptedException {System.out.println("---- start ----");ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(threads);for (int i = 0; i < threads; i++) {scheduledThreadPool.execute(new Runnable() {@Overridepublic void run() {try {System.out.println(Thread.currentThread().getName());} catch (Exception e) {System.out.println(e);} finally {countDownLatch.countDown();}}});} // countDownLatch.await();System.out.println("---- end ----");}/*** newScheduledThreadPool submit submit*/@Testpublic void test2() throws InterruptedException {System.out.println("---- start ----");ExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(threads);for (int i = 0; i < threads; i++) { // Callable 带返回值scheduledThreadPool.submit(new Thread(new Runnable() {@Overridepublic void run() {try {System.out.println(Thread.currentThread().getName());} catch (Exception e) {e.printStackTrace();} finally {countDownLatch.countDown();}}}));}countDownLatch.await();System.out.println("---- end ----");}/*** newScheduledThreadPool submit Callable** @throws ExecutionException* @throws InterruptedException*/@Testpublic void test3() throws ExecutionException, InterruptedException {System.out.println("---- start ----");ExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(threads);for (int i = 0; i < threads; i++) { // Runnable 带返回值FutureTask<?> futureTask = new FutureTask<>(new Callable<String>() {/*** call* @return currentThreadName*/@Overridepublic String call() {return Thread.currentThread().getName();}});scheduledThreadPool.submit(new Thread(futureTask));System.out.println(futureTask.get());}System.out.println("---- end ----");}/*** newScheduledThreadPool scheduleAtFixedRate Callable** @throws ExecutionException* @throws InterruptedException*/@Testpublic void test4() throws ExecutionException, InterruptedException {System.out.println("---- start ----");ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(threads);for (int i = 0; i < threads; i++) {scheduledThreadPool.scheduleAtFixedRate(new Runnable() {@Overridepublic void run() {try {System.out.println(Thread.currentThread().getName());} catch (Exception e) {e.printStackTrace();} finally { // countDownLatch.countDown();}}}, 0, 3, TimeUnit.SECONDS);}countDownLatch.await();System.out.println("---- end ----");}/*** newScheduledThreadPool submit Callable** @throws ExecutionException* @throws InterruptedException*/@Testpublic void test5() throws ExecutionException, InterruptedException {System.out.println("---- start ----");ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(threads);for (int i = 0; i < threads; i++) {scheduledThreadPool.scheduleWithFixedDelay(new Runnable() {@Overridepublic void run() {try {System.out.println(Thread.currentThread().getName());} catch (Exception e) {e.printStackTrace();} finally { // countDownLatch.countDown();}}}, 0, 3, TimeUnit.SECONDS);}countDownLatch.await();System.out.println("---- end ----");} }