• 线程池 ExecutorService execute 异步执行


    package com.cfets.cfib.thread.general;
    
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    /**
     * 使用ExecutorService线程池异步执行
     * 执行无返回结果时用execute方法
     * Created by cfets on  2018/6/26.10:33
     */
    public class ThreadTest {
    
        public static void main1(String[] args) {
            ExecutorService cachedThreadPool = Executors.newFixedThreadPool(50);
    //      ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
    
            cachedThreadPool.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        System.out.println("3秒等待开始");
                        Thread.sleep(3 * 1000);
                        System.out.println("等待结束");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
    
            System.out.println("--------------->");
        }
    
        // jdk 1.8写法
        public static void main(String[] args) {
            ExecutorService cachedThreadPool = Executors.newFixedThreadPool(50);
            cachedThreadPool.execute(() -> proexect());
            System.out.println("--------------->");
        }
    
        public static void proexect() {
            try {
                System.out.println("3秒等待开始");
                for (int i = 0; i < 3; i++) {
                    Thread.sleep(1 * 1000);
                    System.out.println(i);
                }
                System.out.println("等待结束");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
    }
  • 相关阅读:
    面试题 16.07. 最大数值
    461. 汉明距离
    1290. 二进制链表转整数
    1486. 数组异或操作
    1480. 一维数组的动态和
    面试题 17.04. 消失的数字
    626. 换座位
    125. 验证回文串
    530. 二叉搜索树的最小绝对差
    ASP.NET页面之间传递值的几种方式
  • 原文地址:https://www.cnblogs.com/xiaolei2017/p/9240123.html
Copyright © 2020-2023  润新知