PriorityBlockingQueue 和 Executors.newCachedThreadPool()
1、PriorityBlockingQueue里面存储的对象必须是实现Comparable接口。
2、队列通过这个接口的compare方法确定对象的优先级priority。
规则是:当前和其他对象比较,如果compare方法返回负数,那么在队列里面的优先级就比较高。
下面的测试可以说明这个断言:
查看打印结果,比较take出来的Entity和left的entity,比较他们的priority
public class TestPriorityQueue {
static Random r=new Random(47);
public static void main(String args[]){
final PriorityBlockingQueue q = new PriorityBlockingQueue();
ExecutorService se = Executors.newCachedThreadPool();
final int qTime = r.nextInt(100);
//execute producer
se.execute(new Runnable(){
public void run() {
int i=0;
while(true){
q.put(new PriorityEntity(r.nextInt(10), i++)); //优先级,索引
try {
TimeUnit.MILLISECONDS.sleep(qTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
//execute consumer
se.execute(new Runnable(){
public void run() {
while(true){
try {
int rTime = r.nextInt(500);
//打印进队和出队的时间可以看到队列一直在累积
System.out.println(qTime + ": " rTime + " --take-- "+q.take()+" left: "+q.size()+" -- ["+q.toString()+"]");
try {
TimeUnit.MILLISECONDS.sleep(rTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
//try {
// TimeUnit.SECONDS.sleep(5);
//} catch (InterruptedException e) {
// e.printStackTrace();
//}
System.out.println("shutdown");
}
}
class PriorityEntity implements Comparable<PriorityEntity> {
private int priority;
private int index=0;
public PriorityEntity(int _priority,int _index) {
this.priority = _priority;
this.index=_index;
}
public String toString(){
return "# [index="+index+" priority="+priority+"]";
}
//数字小,优先级高
public int compareTo(PriorityEntity o) {
return this.priority > o.priority ? 1 : this.priority < o.priority ? -1 : 0;
}
//数字大,优先级高
// public int compareTo(PriorityTask o) {
// return this.priority < o.priority ? 1 : this.priority > o.priority ? -1 : 0;
// }
}
理解 newCachedThreadPool()
1、创建线程池的根源:
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler)
2、使用场景:
2.1、耗时较短的任务。
2.2、任务处理速度 > 任务提交速度 ,这样才能保证不会不断创建新的进程,避免内存被占满。newCachedThreadPool可以看成newFixedThreadPool(无穷大),虽然无法限制线程总数,但是可以减少不必要的线程创建和销毁上的消耗,
3、取名为cached-threadpool的原因在于线程池中的线程是被线程池缓存了的,也就是说,线程没有任务要执行时,便处于空闲状态,处于空闲状态的线程并不会被立即销毁(会被缓存住),只有当空闲时间超出一段时间(默认为60s)后,线程池才会销毁该线程(相当于清除过时的缓存)。新任务到达后,线程池首先会让被缓存住的线程(空闲状态)去执行任务,如果没有可用线程(无空闲线程),便会创建新的线程。
4、
看一段测试代码:
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; public class newCachedThreadPoolTest {
public static void main(String[] args) {
ExecutorService executorService = Executors.newCachedThreadPool();
for (int i = 1; i < 10000; i++)
executorService.submit(new task());
}
} class task implements Runnable {
@Override
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
运行结果为:Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread
可以看出来是堆外内存溢出,因为我们新建的线程都在工作(代码中用sleep表示在工作中),newCachedThreadPool 只会重用空闲并且可用的线程,所以上述代码只能不停地创建新线程,在 64-bit JDK 1.7 中 -Xss 默认是 1024k,也就是 1M,那就是需要 10000*1M = 10G 的堆外内存空间来给线程使用,但是我的机器总共就 8G 内存,不够创建新的线程,所以就 OOM 了。
所以这个newCachedThreadPool 大家一般不用就是这样的原因,因为它的最大值是在初始化的时候设置为Integer.MAX_VALUE,一般来说机器都没那么大内存给它不断使用。当然知道可能出问题的点,就可以去重写一个方法限制一下这个最大值,但是出于后期维护原因,一般来说用 newFixedThreadPool 也就足够了。
Java如何依据cpu核数设置合适的线程数
newFixedThreadPool 定长线程池的大小最好根据系统资源进行设置。如Runtime.getRuntime().availableProcessors()获取cpu核心数。
executorservice pool = executors.newfixedthreadpool(runtime.getruntime().availableprocessors()*10);一般建议每cpu不超过25个线程,当然多一点也没什么问题,不过就费点线程资源.
java.lang.Runtime.availableProcessors() 方法返回到Java虚拟机的可用的处理器数量。此值可能会改变在一个特定的虚拟机调用。应用程序可用处理器的数量是敏感的,因此偶尔查询该属性,并适当地调整自己的资源使用情况.
PriorityBlockingQueue 和 Executors.newCachedThreadPool()的更多相关文章
- 线程池(3)Executors.newCachedThreadPool
例子: ExecutorService es = Executors.newCachedThreadPool(); try { for (int i = 0; i < 20; i++) { Ru ...
- java 多线程:线程池的使用Executors~ExecutorService; newCachedThreadPool;newFixedThreadPool(int threadNum);ScheduledExecutorService
1,为什么要使用线程池:Executors 系统启动一个新线程的成本是比较高的,因为它涉及与操作系统交互.在这种情形下,使用线程池可以很好地提高性能,尤其是当程序中需要创建大量生存期很短暂的线程时,更 ...
- Java并发之BlockingQueue 阻塞队列(ArrayBlockingQueue、LinkedBlockingQueue、DelayQueue、PriorityBlockingQueue、SynchronousQueue)
package com.thread.test.thread; import java.util.Random; import java.util.concurrent.*; /** * Create ...
- Java四种线程池newCachedThreadPool,newFixedThreadPool,newScheduledThreadPool,newSingleThreadExecutor
1.new Thread的弊端 执行一个异步任务你还只是如下new Thread吗? Java new Thread(new Runnable() { @Override public void ru ...
- Java多线程之新类库中的构件PriorityBlockingQueue
package concurrent2; import java.util.ArrayList; import java.util.List; import java.util.Queue; impo ...
- Executors常用的创建ExecutorService的几个方法说明
一.线程池的创建 我们可以通过ThreadPoolExecutor来创建一个线程池. new ThreadPoolExecutor(corePoolSize, maximumPoolSize, kee ...
- java PriorityBlockingQueue 基于优先级队列,的读出操作可以阻止.
java PriorityBlockingQueue 基于优先级队列.的读出操作可以阻止. package org.rui.thread.newc; import java.util.ArrayLis ...
- Java线程池实现原理与技术(ThreadPoolExecutor、Executors)
本文将通过实现一个简易的线程池理解线程池的原理,以及介绍JDK中自带的线程池ThreadPoolExecutor和Executor框架. 1.无限制线程的缺陷 多线程的软件设计方法确实可以最大限度地发 ...
- Java线程池ThreadPoolExecutor&&Executors
一.先看看传统的开启线程. new Thread(new Runnable() { @Override public void run() { } }).start(); 缺点: 1.每次new Th ...
随机推荐
- 【Spark】SparkStreaming的容错机制
文章目录 检查点机制 驱动器程序容错 工作节点容错 接收器容错 处理保证 检查点机制 Metadata checkpointing -- 将定义流计算的信息存入容错的系统如HDFS. Data che ...
- 【Spark】通过SparkStreaming实现从socket接受数据,并进行简单的单词计数
文章目录 步骤 一.创建maven工程并导入jar包 二.安装并启动生产者 三.开发SparkStreaming代码 四.查看结果 步骤 一.创建maven工程并导入jar包 <properti ...
- android实现计时器
新建布局文件activity_main.xml <?xml version="1.0" encoding="utf-8"?> <LinearL ...
- jvm入门及理解(五)——运行时数据区(虚拟机栈)和本地方法接口
一.虚拟机栈背景 由于跨平台性的设计,java的指令都是根据栈来设计的.不同平台CPU架构不同,所以不能设计为基于寄存器的. 优点是跨平台,指令集小,编译器容易实现,缺点是性能下降,实现同样的功能需要 ...
- python100例 21-30
021 猴子吃桃 题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个第二天早上又将剩下的桃子吃掉一半,又多吃了一个.以后每天早上都吃了前一天剩下的一半零一个.到第10天早 ...
- js 正则(部分)
/** * 增加大于某个值的验证 */window.ParsleyValidator.addValidator( 'greater', function (value,greater) { if(is ...
- 仅需60秒,使用k3sup快速部署高可用K3s集群
作者简介 Dmitriy Akulov,连续创业者,16岁时搭建了开源CDN公共服务jsDelivr的v1版本.目前是边缘托管平台appfleet创始人. 原文链接: https://ma.ttias ...
- jenkins 流水线学习
最佳实践: https://www.cnblogs.com/itech/p/5678643.html 一些样例 https://jenkins.io/doc/pipeline/examples/ gi ...
- 3.10 Go Map哈希表
3.10 Go Map哈希表 map是key-value类型数据结构,读作(哈希表.字典),是一堆未排序的键值对集合. map是引用类型,使用make函数或者初始化表达式创建. map的key必须是支 ...
- System.Web.mail ----虚拟发件人发送邮件
转载别人的 使用SMTP发送邮件 说到邮件发送,先提一下SMTP. SMTP的全称是“Simple Mail Transfer Protocol”,即简单邮件传输协议.它是一组用于从源地址到目的 ...