java多线程与单线程

编程入门 行业动态 更新时间:2024-10-10 09:16:56

java<a href=https://www.elefans.com/category/jswz/34/1767532.html style=多线程与单线程"/>

java多线程与单线程

据我了解,我已经编写了简单的单线程和多线程程序,以检查执行速度.但是我的单线程程序执行速度比多线程程序要好,请看下面的程序,并提及是否有任何错误.

单线程:

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

Mul ::: 200000

Mul ::: 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 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> taskList = new ArrayList>();

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

}

}

多线程输出:

多线程

子:::800

师::: 5

添加::: 1200

乘以::: 200000

日历 – 时间(毫秒):138 415 868 0821

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

更多推荐

java多线程与单线程

本文发布于:2024-03-14 16:29:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1736872.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多线程   单线程   java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!