zoukankan      html  css  js  c++  java
  • java基础——线程池

    package threadpool;
    /*
    创建线程的方式四:使用线程池
    1.提供指定线程数量的线程池
    2.执行指定的线程的操作,需要实现Runnable接口或Callable接口实现类的对象
    3.关闭连接池
    
    好处:
    1.提高响应速度,减少了创建线程的时间
    2.降低资源消耗(重复利用线程池中线程,不需要每次都创建)
    3.便于线程管理,设置线程池
    @author zsben
    @create 2020-01-05 17:05
    */
    
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.ThreadPoolExecutor;
    
    class NumThread implements Runnable{
    
        private int number = 100;
    
        @Override
        public void run() {
            for(int i=2;i<=100;i+=2){
                System.out.println(Thread.currentThread().getName()+":"+i);
            }
        }
    }
    class NumThread1 implements Runnable{
    
        private int number = 100;
    
        @Override
        public void run() {
            for(int i=1;i<=100;i+=2){
                System.out.println(Thread.currentThread().getName()+":"+i);
            }
        }
    }
    
    public class ThreadPool {
    
        public static void main(String[] args) {
            NumThread numThread = new NumThread();
            NumThread1 numThread1 = new NumThread1();
            //1.提供指定线程数量的线程池,进行转型
            ExecutorService executorService = Executors.newFixedThreadPool(10);
            ThreadPoolExecutor service1 = (ThreadPoolExecutor)executorService;
    
            //设置线程池的属性,进行管理
            service1.setCorePoolSize(15);
            //service1.setKeepAliveTime();
    
            //2.执行指定的线程的操作,需要实现Runnable接口或Callable接口实现类的对
            executorService.execute(numThread);//适合Runnable
            executorService.execute(numThread1);
            //executorService.submit();//适合Callable
    
            //3.线程池关闭
            executorService.shutdown();
        }
    }

    后续博客 https://www.cnblogs.com/zzuli/p/9386463.html

    https://blog.csdn.net/weixin_40271838/article/details/79998327

    https://blog.csdn.net/weixin_40990818/article/details/93408523

  • 相关阅读:
    PHP microtime() 函数
    PHP localtime() 函数
    PHP idate() 函数
    PHP gmstrftime() 函数
    Orchestrator安装
    [BJOI2017]开车
    cant found Microsoft.VSSDK.BuildTools.15.0.26201
    如何移动 nuget 缓存文件夹
    如何移动 nuget 缓存文件夹
    如何移动 nuget 缓存文件夹
  • 原文地址:https://www.cnblogs.com/zsben991126/p/12153104.html
Copyright © 2011-2022 走看看