Java线程池的实现原理

一、线程池的作用。

      1.重复利用已创建的线程降低线程创建和销毁造成的消耗,因为线程的切换是有时间片上下文的切换。

      2.提高响应速度,当任务到达时,无需等待线程的创建而直接运行

      3.提高线程的可管理性,通过线程池进行统一的分配,管理与监控。

二、线程池的原理。【代码层实现简单的线程池】

线程池类:ThreadPool

package com.yanbo.thread;

import java.util.List;
import java.util.Vector;

public class ThreadPool {
 
 private static ThreadPool pool=null;//单利模式,创建对象
 public int initSize;//初始线程池大小
 public long timeout;//超时时间
 public List<WorkThread> workerList=new Vector<WorkThread>();//资源缓存池
 
 private ThreadPool(int initSize,long timeout){
  this.initSize=initSize;
  this.timeout=timeout;
  for (int i = 0; i <initSize; i++) {
   workerList.add(new WorkThread(this));
  }
 }
 
 public synchronized static ThreadPool getInstance(){
  return getInstance(10,0);
 }
 
 public synchronized static ThreadPool getInstance(int initSize,long timeout){
  if(pool==null){
   pool=new ThreadPool(initSize, timeout);
  }
  return pool;
 }
 /**
  * 添加任务
  * @param target
  */
 public void addWorker(Runnable target){
  WorkThread worker=null;
  if(workerList.size()>0){
   worker=workerList.get(0);
   workerList.remove(0);
   worker.setTarget(target);
  }else{
   worker=new WorkThread(this, target);
   workerList.add(worker);
  }
 }
 
 /**
  * 释放资源
  * @param worker
  */
 public void releaseWorker(WorkThread worker){
  workerList.add(worker);
 }
 
 /**
  * 关闭所有线程池
  */
 public void shutdown(){
  for (int i = 0; i <initSize; i++) {
   workerList.get(i).shutdown();
  }
 }
 
}





资源类:WorkThread

package com.yanbo.thread;

public class WorkThread extends Thread{
 
 protected ThreadPool pool;
 protected Runnable target;
 private boolean isShutdown=false;//是否关闭
 
 public void operate(){
  System.out.println("========Now do database operate");
 }
 
 public WorkThread(ThreadPool pool) {
  System.out.println("========初始化线程");
  this.pool = pool;
 }

 public WorkThread(ThreadPool pool, Runnable target) {
  System.out.println("<><><><>创建新线程<><><><>");
  this.pool = pool;
  this.target = target;
 }

 @Override
 public void run() {
  while(!isShutdown){
   if(target!=null){
    target.run();
   }
   pool.releaseWorker(this);
   synchronized (this) {
    try {
     this.wait();
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
   }
  }
  
 }
   
 public synchronized void setTarget(Runnable target){
  this.target=target;
  this.notifyAll();
 }
 
 public synchronized void shutdown(){
  this.isShutdown=true;
 }
}




工作线程:DoWork

package com.yanbo.thread;


public class DoWork implements Runnable{
 
 @Override
 public void run() {
  int k=0;
  for (int i = 0; i < 100; i++) {
   k+=i;
  }
  System.out.println("结束。。。");
 }


}



测试类:

package com.yanbo.thread;

import java.util.concurrent.CountDownLatch;


public class PoolTest {
 
 
 public static void main(String[] args) {
  long start=System.currentTimeMillis();
  ThreadPool pool=ThreadPool.getInstance(20,1000);
  for (int i = 0; i < 100; i++) {
   pool.addWorker(new DoWork());
  }
  
  CountDownLatch count=new CountDownLatch(100);
  for (int i = 0; i < 100; i++) {
   new Thread(new DoWork2(count)).start();
  }
  try {
   count.await();
  } catch (InterruptedException e) {
   e.printStackTrace();
  }
  /**线程池耗时4秒,而普通的线程耗时24秒**/
  System.out.println("总共耗时:"+(System.currentTimeMillis()-start));
 }
 
}








  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值