package test.utils.concurrent;

import com.firefly.utils.concurrent.BlockingTask;

import java.util.stream.IntStream;

/**
 * @author Pengtao Qiu
 */
public class TestBlockingTask {
    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        System.out.println("start");
        IntStream.range(0, 10).parallel().forEach((x) -> sleep());
        long end = System.currentTimeMillis();
        System.out.println("end -> " + (end - start));

        start = System.currentTimeMillis();
        System.out.println("start2");
        IntStream.range(0, 10).parallel().forEach((x) -> BlockingTask.callInManagedBlock(() -> {
            sleep();
            return x;
        }));
        end = System.currentTimeMillis();
        System.out.println("end2 -> " + (end - start));
    }

    public static void sleep() {
        try {
            System.out.println(System.currentTimeMillis() + ": Sleeping " + Thread.currentThread().getName());
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            throw new Error(e);
        }
    }
}

@@ -79,135 +79,1045 @@
 sk;%0A
-%0Aimport java.util.stream.IntStream;%0A%0A/**%0A * @author Pengtao Qiu%0A */%0Apublic class TestBlockingTask %7B%0A    public static void main
+import com.firefly.utils.exception.CommonRuntimeException;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0Aimport java.util.concurrent.ExecutionException;%0Aimport java.util.concurrent.ForkJoinPool;%0Aimport java.util.concurrent.ForkJoinTask;%0Aimport java.util.stream.IntStream;%0A%0A/**%0A * @author Pengtao Qiu%0A */%0Apublic class TestBlockingTask %7B%0A    public static void main(String%5B%5D args) %7B%0A        List%3CForkJoinTask%3CInteger%3E%3E tasks = new ArrayList%3C%3E();%0A        ForkJoinPool pool = new ForkJoinPool(2);%0A        for (int i = 0; i %3C 10; i++) %7B%0A            final int x = i;%0A            ForkJoinTask%3CInteger%3E task = pool.submit(() -%3E BlockingTask.callInManagedBlock(() -%3E %7B%0A                sleep();%0A                return x;%0A            %7D));%0A            tasks.add(task);%0A        %7D%0A        tasks.forEach(task -%3E %7B%0A            try %7B%0A                System.out.println(task.get());%0A            %7D catch (InterruptedException %7C ExecutionException e) %7B%0A                e.printStackTrace();%0A            %7D%0A        %7D);%0A    %7D%0A%0A    public static void main2
 (Str
@@ -1985,13 +1985,30 @@
 new 
-Error
+CommonRuntimeException
 (e);

test blocking task