Nav*_*een 17 java 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));
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
Single Thread
Add ::: 1200
Sub ::: 800
Mul ::: 200000
Mul ::: 5
Calender - 以毫秒为单位的时间: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));
}
}
Run Code Online (Sandbox Code Playgroud)
多线程输出:多线程
子::: 800
Division ::: 5
Add ::: 1200
Multiply ::: 200000
Calender - 以毫秒为单位的时间:138 415 868 0821
这里单线程执行138 415 866 7863毫秒,多线程执行138 415 868 0821毫秒.那么多线程的真正目的是什么?
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;
}
}
Run Code Online (Sandbox Code Playgroud)
使用此秒表来测量执行的时间(就像你使用秒表一样),然后做3次,并意识到每次你得到完全不同的结果.这是因为测量精确的执行时间根本不是微不足道的.操作系统不断地用其他任务中断程序的执行,看似简单的命令可以有一整套需要运行的后台命令.
您所能做的就是通过运行该任务一百万次来估算所需的时间,然后取平均值.
| 归档时间: |
|
| 查看次数: |
15641 次 |
| 最近记录: |