美文网首页
9.阻塞队列和线程池

9.阻塞队列和线程池

作者: 进击的勇士 | 来源:发表于2017-03-30 15:05 被阅读0次

阻塞队列

特性

  1. 队列是空的时候,从队列获取元素的操作会被阻塞
  2. 队列是满的时候,往队列添加元素的操作会被阻塞
阻塞队列示例

实现

public class BlockingQueue {

  private List queue = new LinkedList();
  private int  limit = 10;

  public BlockingQueue(int limit){
    this.limit = limit;
  }

  public synchronized void enqueue(Object item)
  throws InterruptedException  {
    // 队列满,阻塞其他线程
    while(this.queue.size() == this.limit) {
      wait();
    }
    // 队列空,唤醒阻塞线程开始enqueue
    if(this.queue.size() == 0) {
      notifyAll();
    }
    this.queue.add(item);
  }

  public synchronized Object dequeue()
  throws InterruptedException{
    // 队列空,阻塞其他线程
    while(this.queue.size() == 0){
      wait();
    }
    // 队列满,唤醒线程开始dequeue
    if(this.queue.size() == this.limit){
      notifyAll();
    }
    return this.queue.remove(0);
  }

}
    

线程池

定义

把并发任务传递给一个线程池,来替代为每个并发执行的任务启动一个新线程。如果线程池里面有空闲线程,任务就会分配给线程进行执行。

任务在线程池内部被插入一个阻塞队列(Blocking Queue),线程池里的线程会去取这个队列里的任务。

简单实现

public class ThreadPool {

    private BlockingQueue taskQueue = null;
    private List<PoolThread> threads = new ArrayList<PoolThread>();
    private boolean isStopped = false;

    public ThreadPool(int noOfThreads, int maxNoOfTasks){
        // 初始化阻塞队列和线程池中的线程
        taskQueue = new BlockingQueue(maxNoOfTasks);
        for(int i=0; i<noOfThreads; i++){
            threads.add(new PoolThread(taskQueue));
        }
        // 启动线程
        for(PoolThread thread : threads){
            thread.start();
        }
    }

    public synchronized void  execute(Runnable task) throws Exception{
        if(this.isStopped) throw
            new IllegalStateException("ThreadPool is stopped");
        // 将任务插入阻塞队列
        this.taskQueue.enqueue(task);
    }

    public synchronized void stop(){
        this.isStopped = true;
        for(PoolThread thread : threads){
           thread.doStop();
        }
    }

}


public class PoolThread extends Thread {

    private BlockingQueue taskQueue = null;
    private boolean isStopped = false;

    public PoolThread(BlockingQueue queue){
        taskQueue = queue;
    }

    public void run(){
        while(!isStopped()){
            try{
                Runnable runnable = (Runnable) taskQueue.dequeue();
                runnable.run();
            } catch(Exception e){
                //log or otherwise report exception,
                //but keep pool thread alive.
            }
        }
    }

    public synchronized void doStop(){
        isStopped = true;
        this.interrupt(); //break pool thread out of dequeue() call.
    }

    public synchronized boolean isStopped(){
        return isStopped;
    }
}

相关文章

  • 9.阻塞队列和线程池

    阻塞队列 特性 队列是空的时候,从队列获取元素的操作会被阻塞 队列是满的时候,往队列添加元素的操作会被阻塞 实现 ...

  • 线程池

    [TOC] 线程池 1. 并发队列:阻塞队列和非阻塞队列 区别如下: 入队: 非阻塞队列:当队列中满了的时候,放入...

  • 线程池

    线程池执行过程 线程池生命周期 线程池分类 阻塞队列 拒绝策略 - ThreadPoolExecutor.Abor...

  • 阻塞队列和线程池

    1.阻塞队列 1)支持阻塞的插入方法:意思是当队列满时,队列会阻塞插入元素的线程,直到队列不满。2)支持阻塞的移除...

  • 阻塞队列和线程池

    队列 队列,又称为伫列(queue),是先进先出(FIFO, First-In-First-Out)的线性表。在具...

  • Executors线程池

    newCacheThreadPool(缓存线程池):阻塞队列为SynchronousQueue,核心线程数0,最大...

  • 阻塞队列

    BlockingQueue线程池的数据结构是阻塞队列BlockingQueue。(在多线程领域:所谓阻塞,在某些情...

  • 阻塞队列和线程池原理

    阻塞队列和线程池原理 阻塞队列 队列 队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除...

  • 4、阻塞队列和线程池原理

    阻塞队列和线程池原理 阻塞队列 队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,...

  • 线程池

    1、为什么要使用线程池2、线程池的工作原理3、线程池参数4、阻塞队列5、饱和策略6、向线程池提交任务7、线程池的状...

网友评论

      本文标题:9.阻塞队列和线程池

      本文链接:https://www.haomeiwen.com/subject/obtpottx.html