栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

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

使用线程池的好处:

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 threadPool = new ThreadPoolExecutor( 
    5, 
    10, 
    3, 
    TimeUnit.SECONDS, 
    new ArrayBlockingQueue(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(); 
   } 
  } 
 } 
 
  
 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(); 
   } 
  } 
 } 
  
  
 public void testNewCachedThreadPool_callable(){ 
  ExecutorService es=Executors.newFixedThreadPool(10); 
  try { 
    
//   String result=es.submit(new MyCallable()).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; 
 } 
} 
 
 
class MyCallable implements Callable{ 
  
 @Override 
 public T call() throws Exception { 
   System.out.println("开始执行Callable"); 
   return (T) "测试callable接口"; 
  } 
} 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/151239.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号