并发/多线程何时能帮助提高性能? [英] When does concurrency/multithreading help improve performance?

查看:68
本文介绍了并发/多线程何时能帮助提高性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在了解到并发确实通过许多人的参与而增加后,我一直计划在项目中使用并发. 现在我在多线程或并发方面工作不多,因此决定在实际项目中使用它之前先学习并有一个简单的概念证明.
以下是我尝试过的两个示例:

I have been planning to use concurrency in project after learning it indeed has increased through put for many. Now I have not worked much on multi threading or concurrency so decided to learn and have a simple proof of concept before using it in actual project.
Below are the two examples I have tried:

1. With use of concurrency

    public static void main(String[] args) 

    {
        System.out.println("start main ");

        ExecutorService es = Executors.newFixedThreadPool(3);
        long startTime = new Date().getTime();
        Collection<SomeComputation> collection = new ArrayList<SomeComputation>();
        for(int i=0; i< 10000; i++){
            collection.add(new SomeComputation("SomeComputation"+i));
        }
        try {
            List<Future< Boolean >> list = es.invokeAll(collection);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("\n end main "+(new Date().getTime() - startTime));
    }

2. Without use of concurrency

        public static void main(String[] args) {

          System.out.println("start main ");
          long startTime = new Date().getTime();
          Collection<SomeComputation> collection = new ArrayList<SomeComputation>();
          for(int i=0; i< 10000; i++){
          collection.add(new SomeComputation("SomeComputation"+i));
        }
        for(SomeComputation sc:collection)
        {
        sc.compute();
        }
        System.out.println("\n end main "+(new Date().getTime() - startTime));
        }

    Both share a common class

        class SomeComputation implements Callable<Boolean>
        {
            String name;
            SomeComputation(String name){this.name=name;}
            public Boolean compute()
            {
                someDumbStuff();
                return true;
            }

            public Boolean call()
            {
                someDumbStuff();
                return true;
            }

            private void someDumbStuff()
            {
                for (int i = 0;i<50000;i++)
                {
                    Integer.compare(i,i+1);
                }
                System.out.print("\n done with "+this.name);
            }
        }

现在每种方法经过20次奇数次运行后进行分析.
具有并发性的第一台平均耗时451毫秒.
没有并发的第二个平均需要290毫秒.
现在,我了解到这取决于配置,操作系统,版本(java 7)和处理器. 但是两种方法都一样. 还了解到在计算繁重时并发成本是可以承受的,但是我不清楚这一点.
希望有人能帮助我更多地了解这一点.
PS:我尝试找到类似的问题,但可以找到这种问题.如果有,请评论链接.

Now the analysis after 20 odd runs of each approach.
1st one with concurrency takes on average 451 msecs.
2nd one without concurrency takes on average 290 msecs.
Now I learned this depends on configuration , OS , version(java 7) and processor. But all was same for both approaches. Also learned the cost of concurrency is affordable on when computation is heavy.But this point wasn't clear to me.
Hope some one can help me understand this more.
PS: I tried finding similar questions but could find this kind.Please comment the link if you do.

推荐答案

并发至少有两个不同的目的:1)性能,2)代码简单(例如1000个Web请求侦听器).

Concurrency has at least two different purposes: 1) performance, and 2) simplicity of code (like 1000 listeners for web requests).

如果您的目标是性能,那么您获得的加速不会超过您投入工作的硬件内核的数量. (这仅在线程受CPU约束的情况下.) 而且,每个线程都有大量的启动开销. 因此,如果您在4核计算机上启动1000个线程,则不可能比4倍加速做得更好,但与此相反,您将有1000个线程启动成本.

If your purpose is performance, you can't get any more speedup than the number of hardware cores you put to work. (And that's only if the threads are CPU-bound.) What's more, each thread has a significant startup overhead. So if you launch 1000 threads on a 4-core machine, you can't possibly do any better than a 4x speedup, but against that, you have 1000 thread startup costs.

这篇关于并发/多线程何时能帮助提高性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆