@configuration
@enableasync
public class asyncconfig implements asyncconfigurer {
@override
public executor getasyncexecutor() {
threadpooltaskexecutor taskexecutor = new threadpooltaskexecutor();
//核心线程数
taskexecutor.setcorepoolsize(8);
//最大线程数
taskexecutor.setmaxpoolsize(16);
//队列大小
taskexecutor.setqueuecapacity(100);
taskexecutor.initialize();
return taskexecutor;
}
}
4.futureservice
@service
public class futureservice {
@async
public future<string> futuretest() throws interruptedexception {
system.out.println("任务执行开始,需要:1000ms");
for (int i = 0; i < 10; i++) {
thread.sleep(100);
system.out.println("do:" + i);
}
system.out.println("完成任务");
return new asyncresult<>(thread.currentthread().getname());
}
}