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

据我所知,我已经编写了下面简单的单线程和multithreading程序来检查执行速度。 但我的单线程程序执行速度比multithreading快,请看下面的程序并提及是否有任何错误。

单线程:

import java.util.Calendar; public class NormalJava { public static void main(String[] args) { System.out.println("Single Thread"); int a = 1000; int b = 200; NormalJava nj = new NormalJava(); nj.Add(a, b); nj.Sub(a, b); nj.Mul(a, b); nj.Div(a, b); Calendar lCDateTime = Calendar.getInstance(); System.out.println("Calender - Time in milliseconds :" + lCDateTime.getTimeInMillis()); } private void Add(int a, int b) { System.out.println("Add :::" + (a + b)); } private void Sub(int a, int b) { System.out.println("Sub :::" + (a - b)); } private void Mul(int a, int b) { System.out.println("Mul :::" + (a * b)); } private void Div(int a, int b) { System.out.println("Mul :::" + (a / b)); } } 

输出:
单线程
添加::: 1200
Sub ::: 800
Mul ::: 200000
Mul ::: 5
日历 – 以毫秒为单位的时间:138 415 866 7863

multithreading程序:

 package runnableandcallable; import java.util.ArrayList; import java.util.Calendar; import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; public class MainThread { private static ExecutorService service = Executors.newFixedThreadPool(10); // connection // pool @SuppressWarnings("unchecked") public static void main(String[] args) throws InterruptedException { System.out.println("Multithreading"); MainThread mt = new MainThread(); mt.testThread(1000, 200); Calendar lCDateTime = Calendar.getInstance(); System.out.println("Calender - Time in milliseconds :" + lCDateTime.getTimeInMillis()); } public void testThread(final int a, final int b) { // create a callable for each method Callable callableAdd = new Callable() { @Override public Void call() throws Exception { Add(a, b); return null; } }; Callable callableSub = new Callable() { @Override public Void call() throws Exception { Sub(a, b); return null; } }; Callable callableMul = new Callable() { @Override public Void call() throws Exception { Mul(a, b); return null; } }; Callable callableDiv = new Callable() { @Override public Void call() throws Exception { Div(a, b); return null; } }; // add to a list List<Callable> taskList = new ArrayList<Callable>(); taskList.add(callableAdd); taskList.add(callableSub); taskList.add(callableMul); taskList.add(callableDiv); // create a pool executor with 3 threads ExecutorService executor = Executors.newFixedThreadPool(3); try { // start the threads List<Future> futureList = executor.invokeAll(taskList); for (Future voidFuture : futureList) { try { // check the status of each future. get will block until the // task // completes or the time expires voidFuture.get(100, TimeUnit.MILLISECONDS); } catch (ExecutionException e) { System.err .println("Error executing task " + e.getMessage()); } catch (TimeoutException e) { System.err.println("Timed out executing task" + e.getMessage()); } } } catch (InterruptedException ie) { // do something if you care about interruption; } } private void Add(int a, int b) { System.out.println("Add :::" + (a + b)); } private void Sub(int a, int b) { System.out.println("Sub :::" + (a - b)); } private void Mul(int a, int b) { System.out.println("Multiply :::" + (a * b)); } private void Div(int a, int b) { System.out.println("Division :::" + (a / b)); } } 

multithreading输出:
multithreading
Sub ::: 800
师::: 5
添加::: 1200
乘::: 200000
日历 – 以毫秒为单位的时间:138 415 868 0821

这里单线程执行138 415 866 7863毫秒,multithreading执行138 415 868 0821毫秒。 那么multithreading的真正目的是什么?

您正在进行的处理非常简单,因此创建线程的开销更加昂贵。

如果你有可以并行完成的昂贵操作,那么multithreading是有意义的。

第一 :因为创建线程的开销超过了它们执行的有用工作。如果你在线程中运行更多的工作,它将使它比一个线程更快。必须在一个线程中运行必须的代码。

第二 :对于创建微基准,你应该使用JMH

1,384,158,667,863毫秒约为44年。 所以你告诉我们你等了44年这次行动的结果? 或者你测量执行速度的方式有什么问题吗?

要测量两次之间的差异,您需要至少两次,而您只能在程序结束时获得当前日期,这甚至不准确。

简单的时间测量类:

 public class StopWatch { private long startTime = -1; public void start() { this.startTime = System.nanoTime(); } public long timeNanos() { return System.nanoTime() - this.startTime; } public double timeMillis() { return this.timeNanos() / 1000000.0; } } 

使用此秒表来测量执行的时间(就像你使用秒表一样),然后做3次,并意识到每次你得到完全不同的结果。 这是因为测量精确的执行时间根本不是微不足道的。 操作系统不断地用其他任务中断程序的执行,看似简单的命令可以有一整套需要运行的后台命令。

您所能做的就是通过运行该任务一百万次来估算所需的时间,然后取平均值。

首先,你的毫秒时间就是时间戳。 您需要在调用之前和之后的毫秒差异,以便测量经过的时间。 我想你先运行单线程应用程序。 如果您首先尝试运行multithreading应用程序,您会注意到它具有较低的“以毫秒为单位的时间”值。

第二。 创建和管理线程会产生开销,这远远高于您执行的非常简单的算术运算的运行时间。 如果您尝试将操作迭代几百万次,则可能会通过并行执行操作来获得性能提升。

如果你考虑一台处理器机器。 所有线程都在单个处理器上运行。 假设你的程序(jvm)每秒在处理器上有0.2秒的执行时间。 如果在单个线程上执行,则0.2秒将仅专用于此主线程。 如果你在4个线程上执行它,例如0.2秒,你就不会有0.05 + 0.05 + 0.05 + 0.05。 您将需要额外的时间来同步,恢复和处置线程。 如果我们假设每次上下文切换这个操作需要0.001秒。 如果线程每秒执行一次,您将每秒丢失0.004秒的执行时间。 在现实生活中,线程上下文切换每秒进行多次,并且是不可预测的。 现在情况正在发生变化,因为有多核机器和线程可以在不同的核心上同时执行。

请参阅此链接以获取更多信息: Java是否支持多核处理器/并行处理?