multithreading比单线程更快吗?

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

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)); } } 

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

 cpu number:4 499999500000 499999500000 cost time:6719000 

然后我运行secondThreadStart,结果是

 cpu number:4 499999500000 499999500000 cost time:14299000 

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

 cpu number:4 499999500000 499999500000 499999500000 499999500000 499999500000 cost time:10416000 

最后我运行fiveThreadStart,结果是

 cpu number:4 499999500000 499999500000 499999500000 499999500000 499999500000 cost time:15708000 

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

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

当然,创建线程会产生开销。

2.尽管创建线程的成本很高,但是cpu数是4,所以使用超过4的线程数会比使用四个线程慢吗?

如果线程很快完成(不受IO绑定和CPU限制),即使线程数超过CPU核心数,也可以获得良好的结果。

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

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

请参阅此SE问题:

Java的Fork / Join vs ExecutorService – 何时使用哪个?

一般来说:

multithreading可以通过使用更多CPU功率来提高应用程序的吞吐量。

这取决于很多因素。

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

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

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

  2. 没有共享数据

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

一些好的SE问题:

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

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

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

好文章:

thetechsolo.wordpress.com文章

java-performance文章

  1. 创建额外的线程绝对是一个成本。 在启动新线程之前,您应该完成大量的工作。
  2. 我认为这意味着你有一个四核CPU。 最佳线程数实际上取决于工作负载,如果线程正在等待他们可能能够将上下文切换到另一个线程的任何原因,并且您可能会看到许multithreading大于物理核心数的好处。
  3. 我不明白这个问题。