码迷,mamicode.com
首页 > 编程语言 > 详细

线程池基础

时间:2019-10-07 09:56:05      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:sch   rac   workqueue   sys   等于   大量   keep   tst   thread   

package com.how2java.tmall.train.ThreadPool;

import java.util.concurrent.*;

import static java.util.concurrent.TimeUnit.NANOSECONDS;

/**
 * @Author: yslu
 * @Date: 2019-09-30 8:44
 * @description:
 */
public class ThreadPoolExecutorDemo {
    public static void main(String[] args) {
        /**
         * 1.FixedThreadPool  固定线程池方式启用线程
         * 特点:最大线程数等于核心线程数,使用无界阻塞队列
         * 可用于Web服务瞬时削峰,但需注意长时间持续高峰情况造成的队列阻塞。
         */
        ExecutorService executor2 = new ThreadPoolExecutor(5, 5,
                0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>());

        /**
         * 2.SingleThreadExecutor 单线程线程池
         * 特点:最大线程数等于核心线程数等于1,使用无界阻塞队列
         * 可用于系统在某个时间唯一执行的任务
         */
        ExecutorService executor3 = new ThreadPoolExecutor(1, 1,
                0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>());

        /**
         * 3.CachedThreadPool 缓存线程池
         * 特点:最大线程数为Integer.MAX_VALUE,即无限大,keepAliveTime = 60s,线程空闲60s后自动结束并回收
         * workQueue 为 SynchronousQueue 同步队列,这个队列类似于一个接力棒,入队出队必须同时传递,
         * 因为CachedThreadPool线程创建无限制,不会有队列等待,所以使用SynchronousQueue;
         * 快速处理大量耗时较短的任务,如Netty的NIO接受请求时,可使用CachedThreadPool。
         */
        ExecutorService executor4 = new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());


        /**
         * 执行延时任务
         */
        ExecutorService executor = new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                0, NANOSECONDS, new DelayQueue());
        Executors.newScheduledThreadPool(5);
        for (int i = 0; i < 10; i++) {
            //创建WorkerThread对象(WorkerThread类实现了Runnable 接口)
            Runnable worker = new WorkerThread("" + i);
            //执行Runnable
            executor.execute(worker);
        }
        //终止线程池
        executor.shutdown();
//        while (!executor.isTerminated()) {
//        }
        System.out.println("Finished all threads");
    }

    //创建一个FixedThreadPool对象 总共只有5个线程
//        ExecutorService executor = Executors.newFixedThreadPool(5);
}

 

 

package com.how2java.tmall.train.ThreadPool;

import java.util.Date;

/**
 * 线程执行
 * @Author: yslu
 * @Date: 2019-09-30 8:43
 * @description:
 */
public class WorkerThread implements Runnable {

    private String command;

    public WorkerThread(String s) {
        this.command = s;
    }

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + " Start. Time = " + new Date());
        processCommand();
        System.out.println(Thread.currentThread().getName() + " End. Time = " + new Date());
    }

    private void processCommand() {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String toString() {
        return this.command;
    }
}

 

线程池基础

标签:sch   rac   workqueue   sys   等于   大量   keep   tst   thread   

原文地址:https://www.cnblogs.com/yslu/p/11629396.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!