当前位置: 移动技术网 > IT编程>开发语言>Java > 以实例简介Java中线程池的工作特点

以实例简介Java中线程池的工作特点

2019年07月22日  | 移动技术网IT编程  | 我要评论

什么原因使我们不得不使用线程池?

个人认为主要原因是:短时间内需要处理的任务数量很多

使用线程池的好处:

1.减少在创建和销毁线程上所花的时间以及系统资源的开销
2.如不使用线程池,有可能造成系统创建大量线程而导致消耗完系统内存

以下是java自带的几种线程池:

1、newfixedthreadpool  创建一个指定工作线程数量的线程池。

每当提交一个任务就创建一个工作线程,如果工作线程数量达到线程池初始的最大数,则将提交的任务存入到池队列中。

2、newcachedthreadpool 创建一个可缓存的线程池。

这种类型的线程池特点是:

1).工作线程的创建数量几乎没有限制(其实也有限制的,数目为interger. max_value), 这样可灵活的往线程池中添加线程。

2).如果长时间没有往线程池中提交任务,即如果工作线程空闲了指定的时间(默认为1分钟),则该工作线程将自动终止。终止后,如果你又提交了新的任务,则线程池重新创建一个工作线程。

3、newsinglethreadexecutor 创建一个单线程化的executor,即只创建唯一的工作者线程来执行任务,如果这个线程异常结束,会有另一个取代它,保证顺序执行(我觉得这点是它的特色)。

单工作线程最大的特点是可保证顺序地执行各个任务,并且在任意给定的时间不会有多个线程是活动的 。

4、newschedulethreadpool  创建一个定长的线程池,而且支持定时的以及周期性的任务执行,类似于timer。

总结:

一.fixedthreadpool是一个典型且优秀的线程池,它具有线程池提高程序效率和节省创建线程时所耗的开销的优点。但在线程池空闲时,即线程池中没有可运行任务时,它不会释放工作线程,还会占用一定的系统资源。

二.cachedthreadpool的特点就是在线程池空闲时,即线程池中没有可运行任务时,它会释放工作线程,从而释放工作线程所占用的资源。但是,但当出现新任务时,又要创建一新的工作线程,又要一定的系统开销。并且,在使用cachedthreadpool时,一定要注意控制任务的数量,否则,由于大量线程同时运行,很有会造成系统瘫痪。

java线程池 threadpoolexecutor使用实例

package com.sondon.mayi.jpool; 
 
import java.util.concurrent.arrayblockingqueue; 
import java.util.concurrent.callable; 
import java.util.concurrent.executionexception; 
import java.util.concurrent.executorservice; 
import java.util.concurrent.executors; 
import java.util.concurrent.threadpoolexecutor; 
import java.util.concurrent.timeunit; 
 
public class jpoollearn { 
 
 private static int producetasksleeptime = 3; 
 private static int producetaskmaxnumber = 20; 
  
 public void testthreadpoolexecutor(){ 
  /* 
   * threadpoolexecutor( 
   * int corepoolsize, //线程池维护线程的最少数量 
   * int maximumpoolsize, //线程池维护线程的最大数量 
   * long keepalivetime, //线程池维护线程所允许的空闲时间 
   * timeunit unit, //线程池维护线程所允许的空闲时间的单位 
   * blockingqueue<runnable> workqueue, //线程池所使用的缓冲队列 
   * rejectedexecutionhandler handler //线程池对拒绝任务的处理策略 ) 
   */ 
  threadpoolexecutor threadpool = new threadpoolexecutor( 
    5, 
    10, 
    3, 
    timeunit.seconds, 
    new arrayblockingqueue<runnable>(10), 
    new threadpoolexecutor.discardoldestpolicy() 
    ); 
 
  for (int i = 1; i <= producetaskmaxnumber; i++) { 
   try { 
    // 产生一个任务,并将其加入到线程池 
    string task = "task---" + i; 
    threadpool.execute(new threadpooltask(task)); 
    system.out.println("activecount :"+ threadpool.getactivecount()); 
    // 便于观察,等待一段时间 
    thread.sleep(producetasksleeptime); 
   } catch (exception e) { 
    e.printstacktrace(); 
   } 
  } 
   
  //查看当前的线程池状况 
  while(true){ 
   try { 
    thread.sleep(3000); 
    system.out.println("pool size :"+threadpool.getpoolsize());//线程池中线程数量 
    system.out.println("active count :"+threadpool.getactivecount());//线程池中活动的线程数量 
   } catch (interruptedexception e) { 
    e.printstacktrace(); 
   } 
  } 
 } 
 
 /** 
  * 
  * @author 蔡文锋 
  * @data_time 2015年7月25日 下午4:06:28 
  * @description { 测试不同线程池模式 } 
  */ 
 public void testnewcachedthreadpool(){ 
  threadpoolexecutor threadpool=(threadpoolexecutor) executors.newcachedthreadpool(); 
//  threadpoolexecutor threadpool=(threadpoolexecutor) executors.newfixedthreadpool(100); 
//  threadpoolexecutor threadpool=(threadpoolexecutor) executors.newscheduledthreadpool(100); 
//  threadpoolexecutor threadpool=(threadpoolexecutor) executors.newsinglethreadexecutor(); 
  try { 
  for (int i = 0; i < 100; i++) { 
   // 产生一个任务,并将其加入到线程池 
   string task = "task---" + i; 
   threadpool.execute(new threadpooltask(task)); 
   system.out.println("activecount :"); 
   // 便于观察,等待一段时间 
   thread.sleep(producetasksleeptime); 
    
   } 
  } catch (interruptedexception e) { 
   e.printstacktrace(); 
  } 
  //查看当前的线程池状况 
  while(true){ 
   try { 
    thread.sleep(3000); 
    system.out.println("pool size :"+threadpool.getpoolsize());//线程池中线程数量 
    system.out.println("active count :"+threadpool.getactivecount());//线程池中活动的线程数量 
   } catch (interruptedexception e) { 
    e.printstacktrace(); 
   } 
  } 
 } 
  
 /** 
  * 
  * @author 蔡文锋 
  * @data_time 2015年7月25日 下午4:06:58 
  * @description { 测试callable与runable方法的区别 } 
  */ 
 public void testnewcachedthreadpool_callable(){ 
  executorservice es=executors.newfixedthreadpool(10); 
  try { 
    
//   string result=es.submit(new mycallable<string>()).get(); 
//   system.out.println("callable result :"+result); 
    
   string result=(string) es.submit(new threadpooltask("")).get(); 
   system.out.println("runable result :"+result); 
    
  } catch (interruptedexception | executionexception e) { 
   e.printstacktrace(); 
  } 
 } 
  
  
 public static void main(string[] args) { 
  new jpoollearn().testnewcachedthreadpool(); 
 } 
} 
 
 
 
/** 
 * 线程池执行的任务 
 */ 
class threadpooltask implements runnable { 
 private static int consumetasksleeptime = 2000; 
 // 保存任务所需要的数据 
 private object threadpooltaskdata; 
 
 threadpooltask(object tasks) { 
  this.threadpooltaskdata = tasks; 
 } 
 
 public void run() { 
  system.out.println("start .." + threadpooltaskdata); 
  try { 
   // sleep 2秒 模拟耗时操作 
   thread.sleep(consumetasksleeptime); 
  } catch (exception e) { 
   e.printstacktrace(); 
  } 
  threadpooltaskdata = null; 
 } 
 
 public object gettask() { 
  return this.threadpooltaskdata; 
 } 
} 
 
/** 
 * 
 * @project : jpool 
 * @package : com.sondon.mayi.jpool 
 * @class : mycallable 
 * @param <t> 
 */ 
class mycallable<t> implements callable<t>{ 
  
 @override 
 public t call() throws exception { 
   system.out.println("开始执行callable"); 
   return (t) "测试callable接口"; 
  } 
} 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网