当前位置: 首页 > wzjs >正文

个人网站需要什么内容织梦网站后台视频教程

个人网站需要什么内容,织梦网站后台视频教程,滕州营销型网站,郑州网球公开赛LJF-Framework 第13章 LjfAsyncManager异步任务管理 一、LjfAsyncService接口 提供两个接口,执行无返回值的异步任务和有返回值的异步任务 package com.ljf.framework.Async;import java.util.concurrent.ThreadPoolExecutor;/*** 说明:** Auther: li…

LJF-Framework 第13章 LjfAsyncManager异步任务管理

一、LjfAsyncService接口

提供两个接口,执行无返回值的异步任务和有返回值的异步任务

package com.ljf.framework.Async;import java.util.concurrent.ThreadPoolExecutor;/*** 说明:** @Auther: lijinfeng* @Date: 2024/8/1*/
public interface LjfAsyncService {/*** 异步执行且有返回值** @param ljfAsyncEvent         异步事件* @param threadPoolExecutor 执行线程池* @param <T>                返回值范型* @return*/<T> LjfAsyncResult<T> supplyAsync(LjfAsyncEvent<T> ljfAsyncEvent, ThreadPoolExecutor threadPoolExecutor);/*** 异步执行无返回值** @param ljfAsyncEvent         异步事件* @param threadPoolExecutor 执行线程池*/void runAsync(LjfAsyncEvent ljfAsyncEvent, ThreadPoolExecutor threadPoolExecutor);}

二、LjfAsyncResult 封装异步直接结果

package com.ljf.framework.Async;import java.util.concurrent.CompletableFuture;/*** 说明:异步执行结果,扩展需要添加对应的构造方法** @Auther: lijinfeng* @Date: 2024/8/2*/
public class LjfAsyncResult<T>{private CompletableFuture<T> completableFuture;public LjfAsyncResult(CompletableFuture<T> completableFuture) {this.completableFuture = completableFuture;}public T join(){return completableFuture.join();}
}

三、LjfAsyncAbstractService抽象类

内部实现一些框架需要的中间处理逻辑,客制化实现需要实现该方法而不是LjfAsyncService

package com.ljf.framework.Async;import com.ljf.framework.context.LjfContextManager;import java.util.concurrent.ThreadPoolExecutor;/*** 说明:** @Auther: lijinfeng* @Date: 2024/8/2*/
public abstract class LjfAsyncAbstractService implements LjfAsyncService {abstract <T> LjfAsyncResult<T> supplyAsyncCus(LjfAsyncEvent<T> ljfAsyncEvent, ThreadPoolExecutor threadPoolExecutor);abstract void runAsyncCus(LjfAsyncEvent ljfAsyncEvent, ThreadPoolExecutor threadPoolExecutor);@Overridepublic <T> LjfAsyncResult<T> supplyAsync(LjfAsyncEvent<T> ljfAsyncEvent, ThreadPoolExecutor threadPoolExecutor) {ljfAsyncEvent.setLjfContext(LjfContextManager.getContext());if (null == threadPoolExecutor){threadPoolExecutor = ThreadPoolConfiguration.getSystemPoolExecutorService();}return this.supplyAsyncCus(ljfAsyncEvent,threadPoolExecutor);}@Overridepublic void runAsync(LjfAsyncEvent ljfAsyncEvent, ThreadPoolExecutor threadPoolExecutor) {ljfAsyncEvent.setLjfContext(LjfContextManager.getContext());if (null == threadPoolExecutor) {threadPoolExecutor = ThreadPoolConfiguration.getSystemPoolExecutorService();}this.runAsyncCus(ljfAsyncEvent,threadPoolExecutor);}
}

四、LjfAsyncEvent封装需要执行的任务

自定义异步任务时,需要继承该抽象类,run()中编写具体的任务逻辑,这里我们封装了上下文的处理,以便在子线程中获取主线程中上下文参数。

package com.ljf.framework.Async;import com.ljf.framework.LjfManager;
import com.ljf.framework.context.LjfContext;import java.util.concurrent.ConcurrentHashMap;/*** 说明:异步任务事件** @Auther: lijinfeng* @Date: 2024/8/1*/
public abstract class LjfAsyncEvent<T> {private ConcurrentHashMap map = new ConcurrentHashMap();public abstract T run();public T runPrepare(){// 设置上下文LjfManager.getLjfContext().syncContext(map.get("ljfContextObject"));return run();};public void setLjfContext(LjfContext context){if (context == null)return;map.put("ljfContextObject",context.getSyncObject());};
}

五、LjfAsyncServiceDefaultImpl默认实现,使用CompletableFuture

package com.ljf.framework.Async;import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;/*** 说明:** @Auther: lijinfeng* @Date: 2024/8/1*/
public class LjfAsyncServiceDefaultImpl extends LjfAsyncAbstractService {@Overridepublic  <T> LjfAsyncResult<T> supplyAsyncCus(LjfAsyncEvent<T> ljfAsyncEvent, ThreadPoolExecutor threadPoolExecutor){// 创建一个CompletableFuture实例CompletableFuture<T> futurePrice = CompletableFuture.supplyAsync(ljfAsyncEvent::runPrepare, threadPoolExecutor);// 当结果准备好后,获取它return new LjfAsyncResult<>(futurePrice);}@Overridepublic void runAsyncCus(LjfAsyncEvent ljfAsyncEvent, ThreadPoolExecutor threadPoolExecutor) {CompletableFuture.runAsync(ljfAsyncEvent::runPrepare,threadPoolExecutor);}
}

六、配置个线程池供异步任务使用

要使用的话记得调整参数,这里只是简单测试配置的。

package com.ljf.framework.Async;import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;/*** 说明:自定义线程池** @Auther: lijinfeng* @Date: 2024/8/1*/
public class ThreadPoolConfiguration {private static ThreadPoolExecutor systemPoolExecutorService;private static ThreadPoolExecutor dbPoolExecutorService;static  {setSystemPoolExecutorService();setDbPoolExecutorService();}/*** 系统异步任务线程池** @return*/public static void setSystemPoolExecutorService() {ThreadPoolConfiguration.systemPoolExecutorService = new ThreadPoolExecutor(3,10,60,TimeUnit.SECONDS,new LinkedBlockingQueue<>(100),Executors.defaultThreadFactory(),(r, executor) -> System.out.println("system pool is full! "));}/*** 数据库异步任务线程池** @return*/public static void setDbPoolExecutorService() {ThreadPoolConfiguration.dbPoolExecutorService = new ThreadPoolExecutor(3,10,60,TimeUnit.SECONDS,new LinkedBlockingQueue<>(100),Executors.defaultThreadFactory(),(r, executor) -> System.out.println("system pool is full! "));}public static ThreadPoolExecutor getSystemPoolExecutorService() {return systemPoolExecutorService;}public static ThreadPoolExecutor getDbPoolExecutorService() {return dbPoolExecutorService;}
}

七、小管家LjfAsyncManager

异步任务的执行和管理在这里统一管理

package com.ljf.framework.Async;import com.ljf.framework.LjfManager;
import com.ljf.framework.exception.LjfAsyncException;
import com.ljf.framework.exception.LjfExceptionEnum;import java.util.concurrent.ThreadPoolExecutor;/*** 说明:** @Auther: lijinfeng* @Date: 2024/8/1*/
public class LjfAsyncManager {private static boolean isRunning = true;/*** 有返回值的异步任务,使用默认线程池** @param ljfAsyncEvent 异步事件* @param <T>           返回值类型* @return AsyncResult*/public static <T> LjfAsyncResult<T> supplyAsync(LjfAsyncEvent<T> ljfAsyncEvent) {check();return LjfManager.getAsyncService().supplyAsync(ljfAsyncEvent, null);}/*** 无返回值的异步任务,使用默认线程池** @param ljfAsyncEvent 异步事件*/public static void runAsync(LjfAsyncEvent ljfAsyncEvent) {check();LjfManager.getAsyncService().runAsync(ljfAsyncEvent, null);}/*** 有返回值的异步任务,使用指定线程池** @param ljfAsyncEvent      异步事件* @param threadPoolExecutor 指定线程池* @param <T>                返回值类型* @return AsyncResult*/public static <T> LjfAsyncResult<T> supplyAsync(LjfAsyncEvent<T> ljfAsyncEvent, ThreadPoolExecutor threadPoolExecutor) {check();return LjfManager.getAsyncService().supplyAsync(ljfAsyncEvent, threadPoolExecutor);}/*** 无返回值的异步任务,使用指定线程池** @param ljfAsyncEvent      异步事件* @param threadPoolExecutor 指定线程池*/public static void runAsync(LjfAsyncEvent ljfAsyncEvent, ThreadPoolExecutor threadPoolExecutor) {check();LjfManager.getAsyncService().runAsync(ljfAsyncEvent, threadPoolExecutor);}/*** 关闭线程池*/public static void shutdown() {isRunning = false;ThreadPoolConfiguration.getDbPoolExecutorService().shutdown();ThreadPoolConfiguration.getSystemPoolExecutorService().shutdown();System.out.println("ljf-framework ThreadPoolExecutor shutdown");}public static void check() {if (!isRunning) {throw new LjfAsyncException(LjfExceptionEnum.ASYNC_STOP,"异步任务管理器已停止,不能执行异步任务");}}
}

八、测试一下

package com.ljf.test;import com.ljf.framework.Async.LjfAsyncEvent;
import com.ljf.framework.Async.LjfAsyncManager;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** 描述 :* <p>* 版本     作者     时间      内容* 1.0      lijinfeng       2025-04-03 09:33     create*/
@SpringBootApplication
public class LjfAsyncTest {public static void main(String[] args) throws InterruptedException {SpringApplication.run(LjfAsyncTest.class, args);LjfAsyncManager.runAsync(new LjfAsyncEvent() {@Overridepublic Object run() {for (int i = 0; i < 8; i++) {System.out.println("异步:"+i);try {Thread.sleep(3 * 1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}return null;}});for (int i = 0; i < 8; i++) {System.out.println(i);if(i==4){LjfAsyncManager.shutdown();}LjfAsyncManager.runAsync(new LjfAsyncEvent() {@Overridepublic Object run() {System.out.println("新的异步异步");return null;}});Thread.sleep(3 * 1000);}}
}

测试结果:

在这里插入图片描述

我这里有点随便,大家可以再认真测试一下,有bug麻烦留言,我会持续改善它

http://www.dtcms.com/wzjs/566415.html

相关文章:

  • 河南网站公司电子商务与网络营销论文
  • 顺德微网站建设苏州网站制作推广
  • 免费行情软件app网站直播下载玩具外贸网站模板
  • 小说类网站怎么做广州专业展台制作价格
  • 好的网站布局那个网站做直播好
  • win10 电脑做网站服务器吗长沙市停课最新消息
  • 连云港建设公司网站app怎么推广运营
  • 云虚拟主机和网站建设网销的网站建设与管理
  • 注册公司网站模版网络推广培训班课程
  • 网络产品服务的提供者不得设置天津网站优化流程
  • 购物网站图标厦门优化网站
  • 以营销导向型建设网站方案交换机做网站
  • 做淘宝的网站有哪些wordpress做论坛
  • 北京做erp报价的网站山东联通网站备案
  • wordpress文章打不开网站改版优化
  • 免费素材网站图库阜新门户网站建设
  • 默认网站停止手机在线app下载
  • 南京网站a建设云世家桂阳网页定制
  • 公司网站建设会计你分录深圳市官网网站建设报价
  • 找人做海报在什么网站找汕尾东莞网站建设
  • 怎么样增加网站权重wordpress实现付费阅读
  • 手机像素网站河北区网站建设
  • 嘉兴制作手机网站wordpress用于商业
  • 站内搜索本网站怎么做哪位大神推荐一下好网站
  • 网站建设的模块电子政务与网站建设 总结
  • 公司网站设计案例定制网站制作服务商
  • 做兼职网站赚钱吗杭州网站提升排名
  • 导视标识设计做搜狗网站优化首页软
  • seo推广必须要做的9种方法上海seo推广
  • 网站建设 质量标准沙井做网站公司