多线程与锁(2)

多线程与锁(2)

  • 线程池

    • 好处

      • 降低资源消耗。通过重复利⽤已创建的线程降低线程创建和销毁造成的消耗。
      • 提⾼响应速度。当任务到达时,任务可以不需要的等到线程创建就能⽴即执⾏。
      • 提⾼线程的可管理性。线程是稀缺资源,如果⽆限制的创建,不仅会消耗系统资源,还会降低系统的稳定性,使⽤线程池可以进⾏统⼀的分配,调优和监控。
    • ThreadPoolExecutor构造方法说明

      • /*
        corePoolSize:核心线程数,线程池最低的线程数
        
        maximumPoolSize:允许的最大的线程数
        
        keepAliveTime:当前线程数超过corePoolSize的时候,空闲线程保留的时间
        
        unit: keepAliveTime线程保留的时间的单位
        
        workQueue: 任务缓冲区
        
        threadFactory: 线程的构造工厂
        
        handler: 线程池饱和时候的处理策略
        */
        public ThreadPoolExecutor(int corePoolSize,
                                      int maximumPoolSize,
                                      long keepAliveTime,
                                      TimeUnit unit,
                                      BlockingQueue<Runnable> workQueue,
                                      ThreadFactory threadFactory,
                                      RejectedExecutionHandler handler) 
                                      
        
    • 自带四种连接池(Executors静态方法)

      • 	//创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。
        	public static ExecutorService newFixedThreadPool(int nThreads) {
                return new ThreadPoolExecutor(nThreads, nThreads,
                                              0L, TimeUnit.MILLISECONDS,
                                              new LinkedBlockingQueue<Runnable>());
            }
        
      •    //创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行。
           public static ExecutorService newSingleThreadExecutor() {
                return new FinalizableDelegatedExecutorService
                    (new ThreadPoolExecutor(1, 1,
                                            0L, TimeUnit.MILLISECONDS,
                                            new LinkedBlockingQueue<Runnable>()));
            }
        
      •   //创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。
          public static ExecutorService newCachedThreadPool() {
                return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                              60L, TimeUnit.SECONDS,
                                              new SynchronousQueue<Runnable>());
            }
        
      •    //创建一个定长线程池,支持定时及周期性任务执行。
           public ScheduledThreadPoolExecutor(int corePoolSize) {
                super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
                      new DelayedWorkQueue());
            }
        
      • 线程池特点建议使用场景
        CachedThreadPool1、线程数无上限
        2、空闲线程存活60s
        3、阻塞队列
        1、任务执行时间短
        2、任务要求响应时间短
        FixedThreadPool1、线程数固定
        2、无界队列
        1、任务比较平缓
        2、控制最大的线程数
        ScheduledThreadPool核心线程数量固定、非核心线程数量无限制(闲置时马上回收)执行定时 / 周期性 任务
        SingleThreadExecutor只有一个核心线程(保证所有任务按照指定顺序在一个线程中执行,不需要处理线程同步的问题)不适合并发但可能引起IO阻塞性及影响UI线程响应的操作,如数据库操作,文件操作等
  • 使用线程池

    • ThreadPoolExecutor pool =new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                            60L, TimeUnit.SECONDS,
                                            new SynchronousQueue<Runnable>());
      Runnable runnable = new Runnable(){
                  @Override
                  public void run(){
                      //.....
                  }
              };
      
      
      Callable<Object> callable = new Callable<Object>(){
                  @Override
                  public Object call() throws Exception{
                      return new Object();
                  }
              };
      
      pool.execute(runnable);
      
      Future<Object> future = pool.sumbit(callable);
      
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值