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

作者:彬彬寒灵 时间:2023-08-11 23:04:19 

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

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

使用线程池的好处:

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接口";
 }
}

标签:Java,线程
0
投稿

猜你喜欢

  • 导入项目出现Java多个工程相互引用异常A cycle was detected in the build path of project的解决办法

    2023-06-26 16:27:17
  • springmvc拦截器登录验证示例

    2022-11-28 12:15:24
  • 使用java的Calendar对象获得当前日期

    2023-10-11 16:24:23
  • Android TabLayout(选项卡布局)简单用法实例分析

    2023-12-17 09:52:28
  • Android Studio实现标题栏和状态栏的隐藏

    2022-11-29 08:45:10
  • Android中分析Jetpack Compose动画内部的实现原理

    2021-06-13 05:23:02
  • SpringBoot2零基础到精通之映射与常用注解请求处理

    2022-06-11 15:41:51
  • Spring Bean常用依赖注入方式详解

    2022-06-05 21:43:33
  • Java7之forkjoin简介_动力节点Java学院整理

    2023-08-31 14:08:28
  • Android LayerDrawable超详细讲解

    2023-12-03 16:36:30
  • springboot 自定义异常并捕获异常返给前端的实现代码

    2022-07-23 03:09:52
  • Android 圆角边框的实现方式汇总

    2023-03-20 04:27:46
  • 1秒实现Springboot 图片添加水印功能

    2022-07-21 04:30:59
  • MyEclipse设置Console输出到文件的实现方法

    2022-01-14 10:37:03
  • Android稳定性:可远程配置化的Looper兜底框架

    2022-02-18 09:02:23
  • java动态口令登录实现过程详解

    2022-01-01 10:16:28
  • SpringSecurity解决POST方式下CSRF问题

    2023-07-18 18:59:51
  • Eclipse运行android项目报错Unable to build: the file dx.jar was not loaded from the SDK folder的解决办法

    2023-06-14 15:28:30
  • Java(基于Struts2) 分页实现代码

    2023-11-04 05:58:58
  • Java中synchronized锁升级的过程

    2022-09-27 16:52:10
  • asp之家 软件编程 m.aspxhome.com