问题描述
据我了解,我已经编写了下面简单的单线程和多线程程序来检查执行速度。但我的单线程程序执行速度比多线程快,请看下面的程序并提及是否有任何错误。
As of my understanding I have written the simple single and multithreading program below to check the execution speed. But my single-threaded program executing faster than multithreaded, kindly see the below program and mention if anything is wrong.
单线程:
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
Output:
Single Thread
Add :::1200
Sub :::800
Mul :::200000
Mul :::5
Calender - Time in milliseconds :138 415 866 7863
多线程程序:
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<Void> callableAdd = new Callable<Void>() {
@Override
public Void call() throws Exception {
Add(a, b);
return null;
}
};
Callable<Void> callableSub = new Callable<Void>() {
@Override
public Void call() throws Exception {
Sub(a, b);
return null;
}
};
Callable<Void> callableMul = new Callable<Void>() {
@Override
public Void call() throws Exception {
Mul(a, b);
return null;
}
};
Callable<Void> callableDiv = new Callable<Void>() {
@Override
public Void call() throws Exception {
Div(a, b);
return null;
}
};
// add to a list
List<Callable<Void>> taskList = new ArrayList<Callable<Void>>();
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<Void>> futureList = executor.invokeAll(taskList);
for (Future<Void> 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));
}
}
多线程输出:
多线程
Sub ::: 800
Division ::: 5
Add ::: 1200
Multiply ::: 200000
日历 - 以毫秒为单位的时间:138 415 868 0821
Mulithreading Output:
Multithreading
Sub :::800
Division :::5
Add :::1200
Multiply :::200000
Calender - Time in milliseconds :138 415 868 0821
此处单线程执行于138 415 866 7863毫秒和多线程在此执行138 415 868 0821毫秒。那么多线程的真正目的是什么?
Here single thread executed at 138 415 866 7863 milliseconds and multithreading executed at this 138 415 868 0821 milliseconds. Then what is the real purpose of multithreading ?
推荐答案
你正在进行的处理是微不足道的,所以创建线程的开销是更贵。
The processing that you are doing is trivial, so the overhead of creating threads is more expensive.
如果你有昂贵的操作可以并行完成,那么多线程是有意义的。
If you had expensive operations which could be done in parallel then multi threads make sense.
这篇关于为什么单线程比Java中的多线程更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!