欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

java 设置任务异步执行

最编程 2024-07-16 14:22:55
...
public class DemoApplication { private static ExecutorService pool; public static void main(String[] args) throws UnsupportedEncodingException, InterruptedException { SpringApplication.run(DemoApplication.class, args); System.out.println("test 开始"); test1(); System.out.println("test 结束"); } private static void test1() { pool = new ThreadPoolExecutor(5,20,1000,TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>(), Executors.defaultThreadFactory(), new ThreadPoolExecutor.CallerRunsPolicy()); pool.submit(() -> new DemoApplication().longTimeMethod()); } public void longTimeMethod() { try { System.out.println("============开始============"); Thread.sleep(2000); System.out.println("============结束============"); } catch (InterruptedException e) { e.printStackTrace(); } } }

推荐阅读