-
线程的实现方式
-
线程池的使用
newWorkStealingPool适合使用在很耗时的操作,但是newWorkStealingPool不是ThreadPoolExecutor的扩展,它是新的线程池类ForkJoinPool的扩展,但是都是在统一的一个Executors类中实现,由于能够合理的使用CPU进行对任务操作(并行操作),所以适合使用在很耗时的任务中:
示例代码:
public class WorkStealingPoolTest { // 线程数 private static final int threads = 10; // 用于计数线程是否执行完成 CountDownLatch countDownLatch = new CountDownLatch(threads); /** * newFixedThreadPool execute * * @throws ExecutionException * @throws InterruptedException */ @Test public void test1() throws ExecutionException, InterruptedException { System.out.println("---- start ----"); ExecutorService executorService = Executors.newWorkStealingPool(); for (int i = 0; i < threads; i++) { executorService.execute(() -> { try { System.out.println(Thread.currentThread().getName()); } catch (Exception e) { System.out.println(e); } finally { countDownLatch.countDown(); } }); } countDownLatch.await(); System.out.println("---- end ----"); } /** * newFixedThreadPool submit submit */ @Test public void test2() throws InterruptedException { System.out.println("---- start ----"); ExecutorService executorService = Executors.newWorkStealingPool(); for (int i = 0; i < threads; i++) { // Callable 带返回值 executorService.submit(new Thread(new Runnable() { @Override public void run() { try { System.out.println(Thread.currentThread().getName()); } catch (Exception e) { e.printStackTrace(); } finally { countDownLatch.countDown(); } } })); } countDownLatch.await(); System.out.println("---- end ----"); } /** * newFixedThreadPool submit Callable * * @throws ExecutionException * @throws InterruptedException */ @Test public void test3() throws ExecutionException, InterruptedException { System.out.println("---- start ----"); ExecutorService executorService = Executors.newWorkStealingPool(); for (int i = 0; i < threads; i++) { // Runnable 带返回值 FutureTask<?> futureTask = new FutureTask<>(new Callable<String>() { /** * call * @return currentThreadName */ @Override public String call() { return Thread.currentThread().getName(); } }); executorService.submit(new Thread(futureTask)); System.out.println(futureTask.get()); } System.out.println("---- end ----"); } }