多线程比单线程快吗?

pet*_*ang 5 java multithreading processor

我想检查多线程是否比单线程快,然后我在这里做一个演示:

public class ThreadSpeedTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        System.out.println("cpu number:"
                + Runtime.getRuntime().availableProcessors());
        singleThreadStart();
//      secondThreadStart();
//      fiveThreadStart();
    }

    private static void sum() {
        long sum = 0;
        for (int i = 0; i < 1000000; i++) {
            sum += i;
        }
        System.out.println(sum);
    }

    private static void singleThreadStart() {
        new Thread(new Runnable() {

            public void run() {
                long start = System.nanoTime();
    //          sum();
    //          sum();
    //          sum();
                sum();
                sum();
                long end = System.nanoTime();
                System.out.println("cost time:" + (end - start));
            }
        }).start();
    }

    private static void secondThreadStart() {
        long start = System.nanoTime();
        Thread thread1 = new Thread(new Runnable() {

            public void run() {
                sum();
            }
        });
        thread1.start();
        Thread thread2 = new Thread(new Runnable() {

            public void run() {
                sum();
            }
        });
        thread2.start();

        try {
            thread1.join();
            thread2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        long end = System.nanoTime();
        System.out.println("cost time:" + (end - start));
    }

    private static void fiveThreadStart() {
        long start = System.nanoTime();
        Thread thread1 = new Thread(new Runnable() {

            public void run() {
                sum();
            }
        });
        thread1.start();
        Thread thread2 = new Thread(new Runnable() {

            public void run() {
                sum();
            }
        });
        thread2.start();
        Thread thread3 = new Thread(new Runnable() {

            public void run() {
                sum();
            }
        });
        thread3.start();
        Thread thread4 = new Thread(new Runnable() {

            public void run() {
                sum();
            }
        });
        thread4.start();
        Thread thread5 = new Thread(new Runnable() {

            public void run() {
                sum();
            }
        });
        thread5.start();

        try {
            thread1.join();
            thread2.join();
            thread3.join();
            thread4.join();
            thread5.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        long end = System.nanoTime();
        System.out.println("cost time:" + (end - start));
    }
}
Run Code Online (Sandbox Code Playgroud)

首先我用两个 sum 方法运行 singleThreadStart,结果是

cpu number:4
499999500000
499999500000
cost time:6719000
Run Code Online (Sandbox Code Playgroud)

然后我运行secondThreadStart,结果是

cpu number:4
499999500000
499999500000
cost time:14299000
Run Code Online (Sandbox Code Playgroud)

然后我用五个 sum 方法运行 singleThreadStart,结果是

cpu number:4
499999500000
499999500000
499999500000
499999500000
499999500000
cost time:10416000
Run Code Online (Sandbox Code Playgroud)

最后我运行了 FiveThreadStart,结果是

cpu number:4
499999500000
499999500000
499999500000
499999500000
499999500000
cost time:15708000
Run Code Online (Sandbox Code Playgroud)

我的问题是:

  1. SecondThreadStart比singleThreadStart花费更多的时间,是不是因为创建线程的成本?
  2. cpu 数是 4,尽管创建线程有成本,那么使用 4 个以上的线程会比使用 4 个线程慢吗?
  3. 如果我想做一些需要更多时间的事情,使用四个线程来做最好吗?

Rav*_*abu 5

1.SecondThreadStart比singleThreadStart花费更多的时间,是不是因为创建线程的成本?

当然,创建线程是有开销的。

2.cpu数为4,尽管创建线程有成本,所以使用4个以上的线程数会比使用4个线程慢吗?

如果线程完成得非常快(不受 IO 限制和 CPU 限制),即使线程数多于 CPU 内核数,您也可以获得良好的结果。

3.如果我想做一些花费很多时间的事情,使用四个线程来做最好吗?

您可以使用高级 java 并发类 ( newWorkStealingPoolof Executors)

请参阅此 SE 问题:

Java 的 Fork/Join 与 ExecutorService - 何时使用哪个?

一般来说:

多线程可以通过使用更多的 CPU 能力来提高应用程序的吞吐量。

这取决于很多因素。

  1. 线程数
  2. CPU内核
  3. 线程创建成本和上下文切换(可能对多线程起作用)
  4. 数据结构
  5. 数据的可变性(可能对多线程起作用)
  6. 数据结构的共享访问/并发(可能对多线程起作用)
  7. 应用类型 : CPU bound 或 IO Bound

如果您的应用程序是多线程将提供出色的结果

  1. 更少的 CPU 绑定,更少的 IO 绑定(但仍然可以为这些应用程序使用多线程)

  2. 没有共享数据

如果不是,则性能取决于上述因素,单线程应用程序和多线程应用程序之间的吞吐量会有所不同。

一些很好的 SE 问题:

https://softwareengineering.stackexchange.com/questions/97615/what-c​​an-multiple-threads-do-that-a-single-thread-cannot

多线程总是比单线程产生更好的性能吗?

为什么单线程比 Java 中的多线程快?

好文章:

thetechsolo.wordpress.com文章

java-性能文章