0
点赞
收藏
分享

微信扫一扫

java 线程池常用方法

生命中最美的是成长 2022-01-31 阅读 68


导入依赖

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>

小例子

import com.google.common.util.concurrent.ThreadFactoryBuilder;

import java.util.concurrent.*;

/**
* @author Mr_Lee
* @date 2020-07-20 11:14
*/
public class ThreadPoolDemo {

private static ExecutorService executorService = new ThreadPoolExecutor(9, 50, 0L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1024) {
}, new ThreadFactoryBuilder().setNameFormat("test1-pool-%d").build());

/**
* 可以这样创建当然我们建议上面的创建方式
*/
public static void test0() {
ExecutorService executorServiceOne = Executors.newSingleThreadExecutor();
ExecutorService executorServiceTwo = Executors.newFixedThreadPool(9);
ExecutorService executorServiceThree = Executors.newCachedThreadPool();
}

/**
* 有返回值
*/
public static void test1() throws ExecutionException, InterruptedException {

Callable<Integer> one = () -> 1 + 1;
Callable<Integer> two = () -> 1 + 2;
Callable<Integer> three = () -> 1 + 3;

FutureTask<Integer> oneTask = new FutureTask<>(one);
FutureTask<Integer> twoTask = new FutureTask<>(two);
FutureTask<Integer> threeTask = new FutureTask<>(three);

executorService.submit(oneTask);
executorService.submit(twoTask);
executorService.submit(threeTask);

//阻塞直到执行完成
Integer integerOne = oneTask.get();
Integer integerTwo = twoTask.get();
Integer integerThree = threeTask.get();

System.out.println(integerOne + integerTwo + integerThree);

}

/**
* 有返回值
*/
public static void test2() throws ExecutionException, InterruptedException {
Callable<Integer> one = () -> 1 + 1;
Callable<Integer> two = () -> 1 + 2;
Callable<Integer> three = () -> 1 + 3;

Future<Integer> oneFuture = executorService.submit(one);
Future<Integer> twoFuture = executorService.submit(two);
Future<Integer> threeFuture = executorService.submit(three);

//阻塞直到执行完成
Integer oneInteger = oneFuture.get();
Integer twoInteger = twoFuture.get();
Integer threeInteger = threeFuture.get();

System.out.println(oneInteger + twoInteger + threeInteger);
}

/**
* 无返回值
*/
public static void test3() {
Runnable one = () -> System.out.println(Thread.currentThread().getName());
Runnable two = () -> System.out.println(Thread.currentThread().getName());
Runnable three = () -> System.out.println(Thread.currentThread().getName());
executorService.execute(one);
executorService.execute(two);
executorService.execute(three);
}

/**
* 无返回值默认取值为null
*/
public static void test4() throws ExecutionException, InterruptedException {
Runnable one = () -> System.out.println(Thread.currentThread().getName());
Runnable two = () -> System.out.println(Thread.currentThread().getName());
Runnable three = () -> System.out.println(Thread.currentThread().getName());

Future<?> oneFuture = executorService.submit(one);
Future<?> twoFuture = executorService.submit(two);
Future<?> threeFuture = executorService.submit(three);

//阻塞直到执行完成
Object oneObject = oneFuture.get();
Object twoObject = twoFuture.get();
Object threeObject = threeFuture.get();

//null
System.out.println(oneObject);
System.out.println(twoObject);
System.out.println(threeObject);
}

public static void test5() throws ExecutionException, InterruptedException {
Runnable one = () -> System.out.println(Thread.currentThread().getName());
Runnable two = () -> System.out.println(Thread.currentThread().getName());
Runnable three = () -> System.out.println(Thread.currentThread().getName());

Future<Integer> oneFuture = executorService.submit(one, 1 + 1);
Future<Integer> twoFuture = executorService.submit(two, 1 + 2);
Future<Integer> threeFuture = executorService.submit(three, 1 + 3);

Integer oneInteger = oneFuture.get();
Integer twoInteger = twoFuture.get();
Integer threeInteger = threeFuture.get();

System.out.println(oneInteger + twoInteger + threeInteger);
}

public static void main(String[] args) throws ExecutionException, InterruptedException {
// test0();
// test1();
// test2();
// test3();
// test4();
// test5();
}


}



举报

相关推荐

0 条评论