Jetty学习【四】 线程

本文详细介绍了Jetty中的线程池实现,包括QueuedThreadPool的属性、构造函数、启动流程和任务执行过程。同时,探讨了线程池在Jetty内部的应用,如Acceptor线程和Selector线程的角色,以及ShutdownThread的机制,最后进行了全面的总结。
摘要由CSDN通过智能技术生成

1、线程池

在Jetty 8 中实现了两个线程池,ExcutorThreadPool和QueuedThreadPool。其中ExcutorThreadPool是对java已有线程池的封装,而QueuedThreadPool是对Executor的实现。下面来看一下这两个线程池的定义。

/* ------------------------------------------------------------ */
/**
 * Jetty ThreadPool using java 5 ThreadPoolExecutor
 * This class wraps a {@link ExecutorService} as a {@link ThreadPool} and
 * {@link LifeCycle} interfaces so that it may be used by the Jetty <code>org.eclipse.jetty.server.Server</code>
 */
public class ExecutorThreadPool extends AbstractLifeCycle implements ThreadPool, LifeCycle

public class QueuedThreadPool extends AbstractLifeCycle implements SizedThreadPool, Executor, Dumpable

2、QueuedThreadPool的实现

2.1 、属性

   // 已经启动的线程数目
    private final AtomicInteger _threadsStarted = new AtomicInteger();
    // 空闲的线程数目
    private final AtomicInteger _threadsIdle = new AtomicInteger();
    // 
    private final AtomicLong _lastShrink = new AtomicLong();
    // 所有启动的线程的集合
    private final ConcurrentLinkedQueue<Thread> _threads=new ConcurrentLinkedQueue<Thread>();
    // 共享变量
    private final Object _joinLock = new Object();
    // 阻塞队列,用于存放需要执行的job
    private BlockingQueue<Runnable> _jobs;
    // _name
    private String _name;
    // 最大空闲时间,这里应该可以理解为线程池中有空闲的线程的最大的持续时间
    private int _maxIdleTimeMs=60000;
    // 最大线程数
    private int _maxThreads=254;
    // 最下线程数
    private int _minThreads=8;
    // 最大排队
    private int _maxQueued=-1;
    // 线程优先级
    private int _priority=Thread.NORM_PRIORITY;
    // 是否是守护线程
    private boolean _daemon=false;
    // 最大stop时间
    private int _maxStopTime=100;
    private boolean _detailedDump=false;

2.2、构造函数

    /* ------------------------------------------------------------------- */
    /** Construct
     */
    public QueuedThreadPool()
    {
    	// 设置线程名称以qtp开头
        _name="qtp"+super.hashCode();
    }

    /* ------------------------------------------------------------------- */
    /** Construct
     */
    public QueuedThreadPool(int maxThreads)
    {
        this();
        // 设置最大线程数
        setMaxThreads(maxThreads);
    }

    /* ------------------------------------------------------------------- */
    /** Construct
     */
    public QueuedThreadPool(BlockingQueue<Runnable> jobQ)
    {
        this();
        // 设置阻塞队列
        _jobs=jobQ;
        // 清除阻塞队列
        _jobs.clear();
    }

2.3、启动线程池

实际启动线程是通过doStart()函数。

    @Override
    protected void doStart() throws Exception
    {
    	// 调用父类doStart()
        super.doStart();
        // 设置_threadsStarted为0,则表示线程从0开始编号
        _threadsStarted.set(0);

        // 如果_jobs为空,则为线程池添加默认队列。
        if (_jobs==null)
        {
        	// 如果_maxQueued大于0,则创建ArrayBlockingQueue,大小为_maxQueued
        	// 如果_maxQueued小于等于0,则创建BlockingArrayQueue,大小为_minThreads,可增长为_minThreads
            _jobs=_maxQueued>0 ?new ArrayBlockingQueue<Runnable>(_maxQueued)
                :new BlockingArrayQueue<Runnable>(_minThreads,_minThreads);
        }

        // 获取线程编号初始值
        int threads=_threadsStarted.get();
        // 启动线程数为_minThreads,默认为8
        while (isRunning() && threads<_minThreads)
        {
        	// 启动线程,编号为threads
            startThread(threads);
            threads=_threadsStarted.get();
        }
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值