栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

ExecutorService令人惊讶的性能收支平衡点—经验法则?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

ExecutorService令人惊讶的性能收支平衡点—经验法则?

  1. 使用执行程序是关于利用CPU和/或CPU内核的,因此,如果创建一个最多利用CPU数量的线程池,则必须拥有与CPU /内核一样多的线程。
  2. 没错,创建新对象的成本太高了。因此,减少费用的一种方法是分批使用。如果您知道要进行的计算的种类和数量,则可以创建批处理。因此,请考虑在一个执行的任务中完成了数千次计算。您为每个线程创建批处理。一旦计算完成(java.util.concurrent.Future),就可以创建下一批。甚至可以在parralel中完成新批处理的创建(4个CPU-> 3个用于计算的线程,1个用于批处理供应的线程)。最后,您可能会获得更高的吞吐量,但会带来更高的内存需求(批次,资源调配)。

编辑:我更改了您的示例,并让它在我的小双核x200笔记本电脑上运行。

provisioned 2 batches to be executedsimpleCompuation:14computationWithObjCreation:17computationWithObjCreationAndExecutors:9

如您在源代码中所看到的,我也从批处理和执行器生命周期中删除了度量。与其他两种方法相比,这更公平。

自己查看结果…

import java.util.List;import java.util.Vector;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.TimeUnit;public class ExecServicePerformance {    private static int count = 100000;    public static void main( String[] args ) throws InterruptedException {        final int cpus = Runtime.getRuntime().availableProcessors();        final ExecutorService es = Executors.newFixedThreadPool( cpus );        final Vector< Batch > batches = new Vector< Batch >( cpus );        final int batchComputations = count / cpus;        for ( int i = 0; i < cpus; i++ ) { batches.add( new Batch( batchComputations ) );        }        System.out.println( "provisioned " + cpus + " batches to be executed" );        // warmup        simpleCompuation();        computationWithObjCreation();        computationWithObjCreationAndExecutors( es, batches );        long start = System.currentTimeMillis();        simpleCompuation();        long stop = System.currentTimeMillis();        System.out.println( "simpleCompuation:" + ( stop - start ) );        start = System.currentTimeMillis();        computationWithObjCreation();        stop = System.currentTimeMillis();        System.out.println( "computationWithObjCreation:" + ( stop - start ) );        // Executor        start = System.currentTimeMillis();        computationWithObjCreationAndExecutors( es, batches ); es.shutdown();        es.awaitTermination( 10, TimeUnit.SEConDS );        // Note: Executor#shutdown() and Executor#awaitTermination() requires        // some extra time. But the result should still be clear.        stop = System.currentTimeMillis();        System.out.println( "computationWithObjCreationAndExecutors:"     + ( stop - start ) );    }    private static void computationWithObjCreation() {        for ( int i = 0; i < count; i++ ) { new Runnable() {     @Override     public void run() {         double x = Math.random() * Math.random();     } }.run();        }    }    private static void simpleCompuation() {        for ( int i = 0; i < count; i++ ) { double x = Math.random() * Math.random();        }    }    private static void computationWithObjCreationAndExecutors( ExecutorService es, List< Batch > batches ) throws InterruptedException {        for ( Batch batch : batches ) { es.submit( batch );        }    }    private static class Batch implements Runnable {        private final int computations;        public Batch( final int computations ) { this.computations = computations;        }        @Override        public void run() { int countdown = computations; while ( countdown-- > -1 ) {     double x = Math.random() * Math.random(); }        }    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/483450.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号