package com.atguigu.gulimall.product.thread;
import java.util.concurrent.*;
/**
* @ClassName ThreadTest
* @Description TODO
* @Author Administrator
* @Version 1.0
*/
public class ThreadTest {
/**
* @Author sunyafei
* @Description 创建多线程的集中方式
* 1)继承Thread
* System.out.println("main start...........");
* Thread01 thread01 = new Thread01();
* thread01.run();
* System.out.println("main end...........");
* 2)实现Runnable接口
* System.out.println("main start...........");
* Runnable01 runnable01 = new Runnable01();
* new Thread(runnable01).start();
* System.out.println("main end...........");
* 3)实现Callable接口 + FutureTask(可以拿到返回结果,可以处理异常)
* System.out.println("main start...........");
* //Callable接口 要配合FutureTask来使用
* FutureTask<Integer> integerFutureTask = new FutureTask<>(new Callable01());
* //然后通过new Thread来启动
* new Thread(integerFutureTask).start();
* //FutureTask可以等整个异步完成任务,可以通过get来获得异步执行返回的结果
* Integer integer = integerFutureTask.get();
* System.out.println("main start..........." + integer);
* 4)线程池
* 给线程池直接提交任务、
* 以后我们在业务代码里面,以上三种方式我们都不使用 new Tread().start可能会使的我们资源耗尽
* 区别: 1,2 都是没有返回值 。3,是有返回值, 只有方式4能够控制资源,性能稳定,不会导致系统崩溃
* 线程池的创建:
* 1)利用工具类 Executors( ExecutorService service = Executors.newFixedThreadPool(10);)
* 2)原生的方法 new ThreadPoolExecutor()
* 七大参数:int corePoolSize,
* 核心线程池的数量,线程池创建好以后就准备就绪的线程数量,就等待线程异步任务过来执行
* int maximumPoolSize,
* 最大线程数量,控制资源
* long keepAliveTime,
* 存活时间,如果当前线程数数量大于大于core核心数量,释放空闲的线程【maximumPoolSize - corePoolSize】
*
* TimeUnit unit,
* 时间单位
* BlockingQueue<Runnable> workQueue
* 阻塞队列,如果有很多任务过来,就会将多余的任务放在队列里面
* 只要有线程空闲,就会去队列里面取出新的任务执行(银行办理业务)
* ThreadFactory threadFactory,
* 线程的创建工厂
* RejectedExecutionHandler handler
* 拒绝策略:如果我们工作队列满了,按照我们指定的拒绝策略拒绝执行任务
*
* 工作顺序:
* 1.线程池的创建:准备好核心数量核心线程,准备接受任务
* 2.新的任务进来,就用core准备好的空闲线程执行
* 1)core满了,就将在进来的任务放到阻塞队列中。空闲的core就会自己会去阻塞队列获取并执行任务
* 2)如果阻塞队列也满了,就会开新的线程执行最大只能开到max指定的数量
* 3) max都执行好了。max-core 数量的空闲的线程会在keepAliveTime指定的时间后自动销毁,最终保持核心线程
* 的大小
* 4)如果max满了就执行拒绝策略
*
*
*
* @Param
* @return
**/
public static ExecutorService service = Executors.newFixedThreadPool(10);
public static void main(String[] args) throws ExecutionException, InterruptedException {
//执行异步任务
System.out.println("main start...........");
/* CompletableFuture<Void> futrue = CompletableFuture.runAsync(() -> {
System.out.println("当前线程号" + Thread.currentThread().getId());
int i = 10 / 2;
System.out.println("运行结果" + i);
}, service);*/
/**
* @Author sunyafei
* @Description 方法完成后的感知处理
* @Param
* @return
**/
/* CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
System.out.println("当前线程号" + Thread.currentThread().getId());
int i = 10 / 0;
System.out.println("运行结果" + i);
return i;
}, service).whenComplete((res,exception)->{
//whenComplete虽然能感知到结果,但是无法修改返回数据
System.out.println("异步任务执行成功了........结果是" + res + ";异常是:"+ exception);
}).exceptionally((throwable)->{
//exceptionally 可以感知异常,返回默认值
return 10;});
// R apply(T t);*/
/**
* @Author sunyafei
* @Description //方法完成后的处理
* @Param
* @return
**/
/* CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
System.out.println("当前线程号" + Thread.currentThread().getId());
int i = 10 / 2;
System.out.println("运行结果" + i);
return i;
}, service).handle((res,throwable)->{
if (res != null){
return res*7;
}
if (throwable!= null){
return 0;
}
return 1;
});*/
// R apply(T t, U u);
/**
*
* 线程串行化
* *
* 1) thenRunAsync无返回值,不能获取到上一步结果
* .thenRunAsync(() -> {
* //thenRunAsync无返回值
* System.out.println("异步任务2执行.......");
* },service);
*
* 2)thenAcceptAsync能接受结果无返回值
* .thenAcceptAsync((res)->{
* System.out.println("异步任务2执行了.......的结果" + res);
* },service)
* 3) thenApplyAsync既能接受上一步的结果又有返回值
**/
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
System.out.println("当前线程号" + Thread.currentThread().getId());
int i = 10 / 3;
System.out.println("运行结果" + i);
return i;
}, service).thenApplyAsync((res) -> {
return res * 9;
});
Integer integer = future.get();
System.out.println("main end..........." + integer);
}
//公共线程池,以后执行任务直接到线程池里面去拿,一个系统至少有一两个线程池
public void thread(String[] args) throws ExecutionException, InterruptedException {
System.out.println("main start...........");
//service.execute(new Runnable01());
//原生的创建方法
ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(5,
200,
2,
TimeUnit.SECONDS,
new LinkedBlockingDeque<>(10000),
Executors.defaultThreadFactory(),
new ThreadPoolExecutor.AbortPolicy());
System.out.println("main end...........");
}
public static class Thread01 extends Thread{
@Override
public void run() {
System.out.println("当前线程号" + Thread.currentThread().getId());
int i = 10/2;
System.out.println("运行结果" + i);
}
}
public static class Runnable01 implements Runnable{
@Override
public void run() {
System.out.println("当前线程号" + Thread.currentThread().getId());
int i = 10/2;
System.out.println("运行结果" + i);
}
}
public static class Callable01 implements Callable<Integer>{
@Override
public Integer call() throws Exception {
System.out.println("当前线程号" + Thread.currentThread().getId());
int i = 10/2;
System.out.println("运行结果" + i);
return i;
}
}
}
线程池七大参数
最新推荐文章于 2025-04-18 23:04:48 发布