Java异步

    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;

public static void main(String[] args) {
        //创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。目前线程为1
        ExecutorService threadPool = Executors.newFixedThreadPool(1);
        threadPool.submit(() -> {
            try {
                log.info("start");
                int a = 10/0;
                log.info("end");
            } catch (Exception e) {
                System.out.println("异常");
            } finally {
                //通知结束线程
                threadPool.shutdown();
            }});
    }

你可能感兴趣的:(java,开发语言)