箭头函数的多线程在开发中的常用配置类

拿Runable实现多线程举例,需要实体类继承runnable的接口。然后重新run方法。再新建一个Thread类去执行。我们也可以使用内部类及箭头函数去简化。

内部类

Runnable runnable = new Runnable() {
    
    
    @Override
    public void run() {
    
    
        //操作体
    }
};
Thread thread = new Thread(runnable);
thread.start();
// 继续简化
 new Thread(new Runnable() {
    
    
     @Override
     public void run() {
    
    
         System.out.println(Thread.currentThread().getName() + " - new");
     }
 }).start();

箭头函数

new Thread(() -> {
    
    
    System.out.println("11");
}).start();

Runnable 接口只有一个 run 方法的定义:public abstract void run()
即制定了一种做事情的方案(其实就是一个函数):

无参数:不需要任何条件即可执行该方案
无返回值:该方案不产生任何结果
代码块(方法体):该方案的具体执行步骤
在Lambda表达式中更为简单:() ‐> System.out.println(“方法执行”)

前面的一对小括号即 run 方法的参数(无) 代表不需要任何条件
中间的一个箭头代表将前面的参数传递给后面的代码
后面的输出语句即业务逻辑代码

开发中的多线程配置

配置类:AsyncManager.java

package com.youming.shuiku.datacenter.provider.utils;


import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

@Slf4j
public class AsyncManager {
    
    
    //线程处理类
    private static ThreadPoolTaskExecutor threadPoolTaskExecutor;
    //本类
    private static AsyncManager asyncManager;
    //该无参构造方法用于或得在容器中得线程处理类
    private AsyncManager(){
    
    
        //获得在容器中的线程处理类
        threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
        threadPoolTaskExecutor.setCorePoolSize(12);//核心线程大小
        threadPoolTaskExecutor.setMaxPoolSize(48 * 2);//最大线程大小
        threadPoolTaskExecutor.setQueueCapacity(500);//队列最大容量
        threadPoolTaskExecutor.setKeepAliveSeconds(300);
        //当提交的任务个数大于QueueCapacity,就需要设置该参数,但spring提供的都不太满足业务场景,可以自定义一个,也可以注意不要超过QueueCapacity即可
        threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        threadPoolTaskExecutor.setWaitForTasksToCompleteOnShutdown(true);
        threadPoolTaskExecutor.setAwaitTerminationSeconds(60);
        threadPoolTaskExecutor.setThreadNamePrefix("TaskLogo-Thread-");
        threadPoolTaskExecutor.initialize();
        //查看线程的数量
        if(threadPoolTaskExecutor.getCorePoolSize()!=12){
    
    
            log.info("getMaxPoolSize==="+threadPoolTaskExecutor.getCorePoolSize());
        }
    }
    //该方法用于创建AsyncManager类 就是本类
    public static AsyncManager getInstance(){
    
    
        //判断本类是不是空 如果是空的话就创建一个 返回 如果不是空就直接放回
        if (asyncManager == null){
    
    
            synchronized (AsyncManager.class){
    
    
                if(asyncManager == null){
    
    
                    asyncManager = new AsyncManager();
                    return asyncManager;
                }
            }
        }
        return asyncManager;
    }
    //该方法用于把任务提交
    public void execute(Runnable runnable){
    
    
        //execute线程的无返回值提交方法
        threadPoolTaskExecutor.execute(runnable);
    }
}

使用的话就可以直接调用,如下

Runnable runnable = () -> {
    
    
	System.out.println("执行");
}
AsyncManager.getInstance().execute(runnable);

猜你喜欢

转载自blog.csdn.net/wang121213145/article/details/130845819