尚于博女友,致命武器第二季,张平选
好久没有写过博客了,都是看大牛的文章,略过~~
突然感觉成长在于总结!废话不多说,开干
由于是公司项目,所以不方便给出代码,看图操作
在项目util目录下创建工具类taskexecutorconfig 并且实现 org.springframework.aop.interceptor.asyncuncaughtexceptionhandler;
该工具类用@enableasync修饰,表示可以用于异步;并且要实现
getasyncexecutor()方法和
getasyncuncaughtexceptionhandler()方法;在
getasyncexecutor()内创建线程池并返回调用;
在需要异步调用的地方创建异步类taskexecutorconfig并调用它的getasyncexecutor()方法,得到线程池java.util.concurrent.
executor对象;再通过executor.execute(new myrunnable(mapparam))实现异步;myrunnable是我创建的内部类
public static class myrunnable implements runnable {
private map<string,object> mapparam;
public myrunnable(map<string,object> mapparam){
this.mapparam = mapparam;
}
/**
* 重写run方法
*/
@override
public void run() {
concertvideo(mapparam);
}
}
需要注意的是executor在java.util.concurrent中;
如果这样不行可以再试试用@componentscan("com.videoadmin.async")修饰异步类taskexecutorconfig;再通过以下方式实现异步:
annotationconfigapplicationcontext context = new annotationconfigapplicationcontext(taskexecutorconfig.class);
taskservice taskservice = context.getbean(taskservice.class);
taskservice.doasync(destfile,objectid);
// 这里调用异步方法...
context.close();
@service
public class taskservice {
private static string piantouts = "f:\\shiping_test\\tsfile\\test\\2.ts";
@async
public string doasync(string filepath,string objectid) throws exception {
if (filepath != null) {
filepath = convertvideo.transtots(filepath);
if (filepath != null) {
filepath = convertvideo.concatvideo(piantouts, filepath);
if (filepath != null) {
filepath = convertvideo.processmp4(filepath);
if (filepath != null) {
return filepath;
}
}
}
}
return null;
}
}
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
新补丁让Windows 10出现死机、卡顿等 微软坐不住称正解决
Ubuntu 20.04 LTS进驻Windows子系统:只能用3年
ThinkPad正式加入:预装Linux发行版而非Win10的PC越来越多了
最新版Android 11推送!谷歌Pixel 5被曝光:支持反向充电
高度致敬Windows!开源优麒麟20.04 LTS发布:支持5年
电脑小知识:Windows 10是用什么语言写的?到底有多少行代码?
Win10 5月更新准正式版:微软引入大量新功能 系统响应速度快
Windows 10新预览版19613推送:修复任务栏图标显示异常BUG
Windows AD 报错解决:UAC File Virtualization服务启动失败 此驱动程序被阻止加载
网友评论