Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.util.HashSet;
import java.util.concurrent.ForkJoinPool;
import java.util.stream.IntStream;

public class Main {
    public static void main(String[] arg) throws Exception {
        Runnable parallelCode = () -> {
            HashSet<String> allThreads = new HashSet<>();
            IntStream.range(0, 1_000_000).parallel().filter(i -> {
                allThreads.add(Thread.currentThread().getName());
                return false;
            }).min();
            System.out.println("executed by " + allThreads);
        };
        System.out.println("default behavior: ");
        parallelCode.run();
        System.out.println("specialized pool:");
        ForkJoinPool pool = new ForkJoinPool(2);
        pool.submit(parallelCode).get();
    }
}