Simple example of java thread pool ForkJoinPool

 

ForkJoinPool can be executed in parallel according to the number of CPU cores, which is suitable for time-consuming operations and can make full use of the CPU to perform tasks.

UML class diagram of ForkJoinPool:

 Simple example:

public class ForkJoinPoolTest {
    private static final int threads = 10;
    CountDownLatch countDownLatch = new CountDownLatch(threads);

    @Test
    public void test1() throws InterruptedException {
        System.out.println("-------- begin ----------");
        ForkJoinPool forkJoinPool = new ForkJoinPool();
        for (int i = 0; i < threads; i++) {
            forkJoinPool.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        System.out.println("getParallelism=" + forkJoinPool.getParallelism());
                        System.out.println("getStealCount=" + forkJoinPool.getStealCount());
                        System.out.println(Thread.currentThread().getName());
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        countDownLatch.countDown();
                    }
                }
            });
        }
        countDownLatch.await();
        System.out.println("-------- end ----------");
    }
}

Guess you like

Origin blog.csdn.net/qq_38428623/article/details/86690058