美文网首页
ExecutorService & GParsExecutors

ExecutorService & GParsExecutors

作者: 泠泉 | 来源:发表于2019-08-07 17:59 被阅读0次

    ExecutorService


    package groovy
    
    import org.springframework.util.StopWatch
    
    import java.util.concurrent.Callable
    import java.util.concurrent.ExecutorService
    import java.util.concurrent.Executors
    import java.util.concurrent.Future
    
    /**
     * 异步执行练习
     */
    class AsyncTask {
        static void main(String[] args) {
            StopWatch stopWatch = new StopWatch()
            stopWatch.start()
    //        consumeTime(1)
    //        consumeTime(2)
    //        consumeTime(3)
    //        consumeTime(4)
    //        [1,2,3,4].parallelStream().forEach({consumeTime(it)})
            ExecutorService executorService = Executors.newFixedThreadPool(5)
            Future<List> f1 = executorService.submit({consumeTime(1)} as Callable)
            Future<List> f2 = executorService.submit({consumeTime(2)} as Callable)
            Future<List> f3 = executorService.submit({consumeTime(3)} as Callable)
            Future<List> f4 = executorService.submit({consumeTime(4)} as Callable)
            List l1 = f1.get()
            List l2 = f2.get()
            List l3 = f3.get()
            List l4 = f4.get()
            println l1+=l2+=l3+=l4
            stopWatch.stop()
            println "all time take: " + stopWatch.getTotalTimeSeconds()
            executorService.shutdown()
        }
    
        static List consumeTime(long min) {
            Thread.sleep(min * 1000)
            [min]
        }
    }
    

    GParsExecutorsPool (Executor Service Enhancements)


    package groovy
    
    import groovyx.gpars.GParsExecutorsPool
    import org.springframework.util.StopWatch
    
    import java.util.concurrent.ExecutorService
    
    /**
     * 异步执行练习
     */
    class AsyncTask {
        static void main(String[] args) {
            StopWatch stopWatch = new StopWatch()
            stopWatch.start()
            List r = []
            GParsExecutorsPool.withPool { ExecutorService executorService ->
                executorService << {r<<consumeTime(1)}
                executorService << {r<<consumeTime(2)}
                executorService << {r<<consumeTime(3)}
                executorService << {r<<consumeTime(4)}
            }
            println r
            stopWatch.stop()
            println "all time take: " + stopWatch.getTotalTimeSeconds()
        }
    
        static List consumeTime(long min) {
            Thread.sleep(min * 1000)
            [min]
        }
    }
    
    

    java

    package multipleThread;
    
    import org.assertj.core.util.Lists;
    import org.junit.Test;
    import org.springframework.util.StopWatch;
    
    import java.util.Arrays;
    import java.util.List;
    import java.util.concurrent.ExecutionException;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.Future;
    
    /**
     * @author Kelvin范显
     * @createDate 2019年08月09日
     */
    public class ExecutorTest {
    
        List consumeTime(long min) throws InterruptedException {
            Thread.sleep(min * 1000);
            System.out.println("consumed " + min);
            return Arrays.asList(min);
        }
    
        @Test
        public void testFixedPool() throws ExecutionException, InterruptedException {
            StopWatch stopWatch = new StopWatch();
            stopWatch.start();
            ExecutorService executorService = Executors.newFixedThreadPool(3);
            Future<?> c1 = executorService.submit(()->consumeTime(3));
            Future<?> c2 = executorService.submit(()->consumeTime(4));
            stopWatch.stop();
            List<Object> objects = Arrays.asList(c1.get(), c2.get());
            System.out.println(objects);
            System.out.println("all time take: " + stopWatch.getTotalTimeSeconds());
        }
    }
    

    相关文章

      网友评论

          本文标题:ExecutorService & GParsExecutors

          本文链接:https://www.haomeiwen.com/subject/pyyadctx.html