【发布时间】:2013-11-22 23:49:34
【问题描述】:
据我了解,我在下面编写了简单的单线程和多线程程序来检查执行速度。但是我的单线程程序执行速度比多线程快,请看下面的程序,如果有什么问题请指出。
单线程:
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
子 :::800
穆尔 :::200000
穆尔 :::5
日历 - 以毫秒为单位的时间: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));
}
}
多线程输出:
多线程
子 :::800
分区 :::5
添加 :::1200
乘 :::200000
日历 - 以毫秒为单位的时间:138 415 868 0821
这里单线程执行时间为 138 415 866 7863 毫秒,多线程执行时间为 138 415 868 0821 毫秒。那么多线程的真正目的是什么?
【问题讨论】:
-
“那么多线程的真正目的是什么?” - 有两个目的: 1. 避免长作业阻塞短作业(例如,在 Swing 应用程序中至少有两个线程,一个负责在屏幕上绘制什么,另一个负责对按钮做出反应点击;如果对按钮点击做出反应的线程也会在屏幕上绘制,则按钮不会对您点击它们做出反应或反应缓慢) 2.当有多个“处理单元”(多核/多处理器系统)时,JVM 可能将不同的线程分派到不同的单元,也许会加快速度。
-
你在一个线程之后运行多线程 - 那个信息来自你输出的时间戳信息,而不是运行的持续时间。一些答案指出了这一点。
标签: java multithreading