首页 > 编程语言 > 详细

java设置任务异步执行

时间:2021-06-02 09:20:49      阅读:22      评论:0      收藏:0      [点我收藏+]
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();
        }
    }

}

 

java设置任务异步执行

原文:https://www.cnblogs.com/g120/p/14839072.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!