- 浏览: 373503 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (292)
- jbpm3.2 (4)
- hibernate (1)
- struts (2)
- spring (7)
- oracle (20)
- TCP/UDP (3)
- SpringSide (4)
- maven (4)
- eclipse插件 (11)
- 模板引擎 (2)
- javascript (4)
- 设计模式 (2)
- 工作中遇到异常及解决 (3)
- java文件编译问题 (1)
- ehcache应用 (1)
- java反射 (1)
- jbpm4 (1)
- Google-Gson (1)
- Jquery (6)
- XML (5)
- 工作记事 (2)
- flash builder 4 (1)
- Lucene (8)
- struts2 (1)
- AspectJ (1)
- spring proxool连接池配置 (1)
- StringUtils (1)
- spring security (5)
- JAVA点滴 (9)
- jbpm4.3 (1)
- ACL (0)
- 线程 (7)
- Java线程:新特征-线程池 (1)
- MemCache (5)
- compass (0)
- NIO (2)
- zookeeper (4)
- 并发 (2)
- redis (9)
- Nginx (5)
- jvm (1)
- 互联网 (24)
- shell (3)
- CAS (1)
- storm (4)
- 数据结构 (3)
- MYSQL (3)
- fsdfsdfsd (0)
- hadoop (19)
- hive (3)
- IntelliJ (3)
- python (3)
- 23423 (0)
- spark (7)
- netty (9)
- nmon (1)
- hbase (8)
- cassandra (28)
- kafka (2)
- haproxy (3)
- nodejs (3)
- ftp (1)
最新评论
-
记忆无泪:
遇到这个问题我用的sed -i 's/\r$//' /mnt/ ...
CentOS6 Shell脚本/bin/bash^M: bad interpreter错误解决方法 -
alenstudent:
Windows 下Nexus搭建Maven私服 -
dandongsoft:
lucene3+IK分词器 改造 lucene2.x+paoding -
duanyanrui:
学习了,支持
Google-Gson -
yscyfy:
这是你直接翻译过来的???
Google-Gson
原文在这里
下面是一个计算的框架代码:
/** * A class that calculates the optimal thread pool boundaries. It takes the desired target utilization and the desired * work queue memory consumption as input and retuns thread count and work queue capacity. * * @author Niklas Schlimm * */ public abstract class PoolSizeCalculator { /** * The sample queue size to calculate the size of a single {@link Runnable} element. */ private final int SAMPLE_QUEUE_SIZE = 1000; /** * Accuracy of test run. It must finish within 20ms of the testTime otherwise we retry the test. This could be * configurable. */ private final int EPSYLON = 20; /** * Control variable for the CPU time investigation. */ private volatile boolean expired; /** * Time (millis) of the test run in the CPU time calculation. */ private final long testtime = 3000; /** * Calculates the boundaries of a thread pool for a given {@link Runnable}. * * @param targetUtilization * the desired utilization of the CPUs (0 <= targetUtilization <= 1) * @param targetQueueSizeBytes * the desired maximum work queue size of the thread pool (bytes) */ protected void calculateBoundaries(BigDecimal targetUtilization, BigDecimal targetQueueSizeBytes) { calculateOptimalCapacity(targetQueueSizeBytes); Runnable task = creatTask(); start(task); start(task); // warm up phase long cputime = getCurrentThreadCPUTime(); start(task); // test intervall cputime = getCurrentThreadCPUTime() - cputime; long waittime = (testtime * 1000000) - cputime; calculateOptimalThreadCount(cputime, waittime, targetUtilization); } private void calculateOptimalCapacity(BigDecimal targetQueueSizeBytes) { long mem = calculateMemoryUsage(); BigDecimal queueCapacity = targetQueueSizeBytes.divide(new BigDecimal(mem), RoundingMode.HALF_UP); System.out.println("Target queue memory usage (bytes): " + targetQueueSizeBytes); System.out.println("createTask() produced " + creatTask().getClass().getName() + " which took " + mem + " bytes in a queue"); System.out.println("Formula: " + targetQueueSizeBytes + " / " + mem); System.out.println("* Recommended queue capacity (bytes): " + queueCapacity); } /** * Brian Goetz' optimal thread count formula, see 'Java Concurrency in Practice' (chapter 8.2) * * @param cpu * cpu time consumed by considered task * @param wait * wait time of considered task * @param targetUtilization * target utilization of the system */ private void calculateOptimalThreadCount(long cpu, long wait, BigDecimal targetUtilization) { BigDecimal waitTime = new BigDecimal(wait); BigDecimal computeTime = new BigDecimal(cpu); BigDecimal numberOfCPU = new BigDecimal(Runtime.getRuntime().availableProcessors()); BigDecimal optimalthreadcount = numberOfCPU.multiply(targetUtilization).multiply( new BigDecimal(1).add(waitTime.divide(computeTime, RoundingMode.HALF_UP))); System.out.println("Number of CPU: " + numberOfCPU); System.out.println("Target utilization: " + targetUtilization); System.out.println("Elapsed time (nanos): " + (testtime * 1000000)); System.out.println("Compute time (nanos): " + cpu); System.out.println("Wait time (nanos): " + wait); System.out.println("Formula: " + numberOfCPU + " * " + targetUtilization + " * (1 + " + waitTime + " / " + computeTime + ")"); System.out.println("* Optimal thread count: " + optimalthreadcount); } /** * Runs the {@link Runnable} over a period defined in {@link #testtime}. Based on Heinz Kabbutz' ideas * (http://www.javaspecialists.eu/archive/Issue124.html). * * @param task * the runnable under investigation */ public void start(Runnable task) { long start = 0; int runs = 0; do { if (++runs > 5) { throw new IllegalStateException("Test not accurate"); } expired = false; start = System.currentTimeMillis(); Timer timer = new Timer(); timer.schedule(new TimerTask() { public void run() { expired = true; } }, testtime); while (!expired) { task.run(); } start = System.currentTimeMillis() - start; timer.cancel(); } while (Math.abs(start - testtime) > EPSYLON); collectGarbage(3); } private void collectGarbage(int times) { for (int i = 0; i < times; i++) { System.gc(); try { Thread.sleep(10); } catch (InterruptedException e) { Thread.currentThread().interrupt(); break; } } } /** * Calculates the memory usage of a single element in a work queue. Based on Heinz Kabbutz' ideas * (http://www.javaspecialists.eu/archive/Issue029.html). * * @return memory usage of a single {@link Runnable} element in the thread pools work queue */ public long calculateMemoryUsage() { BlockingQueue<Runnable> queue = createWorkQueue(); for (int i = 0; i < SAMPLE_QUEUE_SIZE; i++) { queue.add(creatTask()); } long mem0 = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); long mem1 = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); queue = null; collectGarbage(15); mem0 = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); queue = createWorkQueue(); for (int i = 0; i < SAMPLE_QUEUE_SIZE; i++) { queue.add(creatTask()); } collectGarbage(15); mem1 = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); return (mem1 - mem0) / SAMPLE_QUEUE_SIZE; } /** * Create your runnable task here. * * @return an instance of your runnable task under investigation */ protected abstract Runnable creatTask(); /** * Return an instance of the queue used in the thread pool. * * @return queue instance */ protected abstract BlockingQueue<Runnable> createWorkQueue(); /** * Calculate current cpu time. Various frameworks may be used here, depending on the operating system in use. (e.g. * http://www.hyperic.com/products/sigar). The more accurate the CPU time measurement, the more accurate the results * for thread count boundaries. * * @return current cpu time of current thread */ protected abstract long getCurrentThreadCPUTime(); }
下面是一个具体的计算场景:
public class MyPoolSizeCalculator extends PoolSizeCalculator { public static void main(String[] args) throws InterruptedException, InstantiationException, IllegalAccessException, ClassNotFoundException { MyThreadSizeCalculator calculator = new MyThreadSizeCalculator(); calculator.calculateBoundaries(new BigDecimal(1.0), new BigDecimal(100000)); } protected long getCurrentThreadCPUTime() { return ManagementFactory.getThreadMXBean().getCurrentThreadCpuTime(); } protected Runnable creatTask() { return new AsynchronousTask(0, "IO", 1000000); } protected BlockingQueue<runnable> createWorkQueue() { return new LinkedBlockingQueue<>(); } }
运行得到的计算结果:
Target queue memory usage (bytes): 100000 createTask() produced com.schlimm.java7.nio.threadpools.AsynchronousTask which took 40 bytes in a queue Formula: 100000 / 40 * Recommended queue capacity (bytes): 2500 Number of CPU: 2 Target utilization: 1.0 Elapsed time (nanos): 3000000000 Compute time (nanos): 906250000 Wait time (nanos): 2093750000 Formula: 2 * 1.0 * (1 + 2093750000 / 906250000) * Optimal thread count: 6.0
最后的一个推荐设置:
ThreadPoolExecutor pool = new ThreadPoolExecutor(6, 6, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(2500)); pool.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
发表评论
-
无备案小站的最好的广告收益来源
2020-08-09 10:52 253现在无备案的无名小站运营维持真的很难,广告难接,各大广告联盟 ... -
老番茄访问IP突破1W
2019-11-13 10:19 5感谢大家,老番(www.laofanqie.com) 用户访问 ... -
Nmon工具的使用以及通过nmon_analyse生成分析报表
2016-09-07 11:44 394Nmon工具的使用以及通过nmon_a ... -
用“逐步排除”的方法定位Java服务线上“系统性”故障
2015-08-27 19:12 742一、摘要 由于硬件问题、系统资源紧缺或者程序本身的BUG, ... -
simple-spring-memcached简介
2015-07-09 15:06 423memcached是一款非常优秀的分布式缓存工具,有效提升了 ... -
Tomcat 7 的新JDBC连接池的使用说明
2015-06-28 22:55 1322Tomcat 7 的JDBC连接池实 ... -
CentOS安装JDK1.6
2015-04-01 11:56 10371、获得程序包 jdk-6u16-dlj-linux- ... -
abtest
2014-12-01 11:51 1046Apache服务自带了应该用于压力测试的工具ab(Apach ... -
mysql优化
2014-08-29 11:36 60919195.cn 手游网站mysql 优化一: v ... -
怎么快速搭建游戏网站,手游网站,手机应用网站??
2014-07-28 13:14 1604大家好,作为19195手游 ... -
mysql备份还原
2014-06-13 11:31 2备份数据库 -
centos6利用yum安装php mysql
2014-05-18 13:47 916一、安装mysql #yum -y install my ... -
Centos如何挂载硬盘
2014-05-15 13:03 776远程SSH登录上Centos服务器后,进行如下操作提醒:挂 ... -
远程监控JVM--VisualVM
2013-01-09 18:26 1173对于使用命令行远程监控jvm太麻烦?那可以试试sun ... -
聊聊并发(五)原子操作的实现原理
2013-01-07 17:50 01 引言 原子(atom)本意是“不能被进一步分 ... -
聊聊并发(四)深入分析ConcurrentHashMap
2013-01-07 17:48 0术语定义 术语 英文 解释 哈希算法 ... -
聊聊并发(三)Java线程池的分析和使用
2013-01-07 17:45 8381. 引言 合理利用线程池能够带来三个好处。第一 ... -
聊聊并发(二)Java SE1.6中的Synchronized
2013-01-07 17:42 9151 引言 在多线程并发编程中Synchronized一 ... -
聊聊并发(一)深入分析Volatile的实现原理
2013-01-07 17:41 811作者http://ifeve.com 引言 在 ... -
虚拟机stack全解析
2013-01-07 14:40 1345转载 通过jps -lv 获取到本地的一个JVM实例进 ...
相关推荐
1. 当提交一个新任务时,如果当前线程池中的线程数量少于corePoolSize,会直接创建新线程来执行任务。 2. 如果线程池已达到corePoolSize,但任务队列未满,新任务会放入任务队列中等待。 3. 当线程池中的线程数大于...
1. **线程池大小**:线程池中线程的最大数量,可以通过参数进行配置。 2. **工作队列**:用于存放待处理任务的队列,当所有线程都在忙碌时,新任务会被放入队列等待。 3. **线程管理策略**:当工作队列满时,线程池...
- **线程池管理器**:控制线程池的生命周期,如添加/删除线程,设置线程池大小等。 4. **线程池的优势**: - **性能优化**:避免了线程创建和销毁的开销,减少上下文切换次数。 - **资源管理**:动态调整线程...
线程池是多线程编程中的一个重要概念,它在Windows操作系统以及其他支持多线程操作系统的环境中广泛应用。线程池是一种管理线程资源的有效方式,通过预先创建并维护一定数量的线程来处理任务,而不是每次需要执行新...
本实例将深入探讨如何在Linux下实现一个简单的线程池,并介绍相关的关键知识点。 1. **线程与线程池的概念** - **线程**:是操作系统分配CPU时间片的基本单位,是程序执行的流,一个进程中可以包含多个线程,它们...
可以根据系统承载能力动态调整线程池大小,避免服务器资源过度消耗。 在Java中,与线程池相关的几个重要类和接口包括: 1. Executor:它是Java中线程池的顶级接口,定义了执行线程的抽象方法,但它本身并不直接...
在SpringBoot应用程序中,线程池参数的配置是一个非常重要的环节,直接影响着应用程序的性能和稳定性。然而,许多开发者对线程池参数的配置感到困惑和不了解,导致线上环境出现问题。本文将通过一个简单的案例,详细...
线程池技术的设计和实现需要考虑多个因素,例如线程池的大小、线程池的类型和线程池的管理方式。在设计和实现线程池技术时,需要选择合适的线程池模型和参数,以确保线程池的稳定运行和高效运算。 小结 ...
2. **`Worker` 类**:这是一个内部类,继承自`Thread`,负责实际的任务执行。 - `runner`:当前`Worker`正在执行的`Runnable`任务。 - `wakeup` 方法:用于唤醒当前处于等待状态的`Worker`,使其开始执行新的任务...
线程池是多线程编程中的一个重要概念,它是一种线程使用模式,通过预先创建一定数量的线程来处理任务,而不是每当有新任务提交时就创建新的线程。线程池的使用可以有效地减少系统资源的消耗,提高系统的响应速度和...
3. 执行任务:工作线程从队列中取出任务,执行完毕后返回工作队列等待下一个任务。 4. 线程管理:根据系统负载和设定策略,动态调整线程池的大小。 5. 销毁线程池:当线程池不再需要时,可以安全地关闭线程池,释放...
任务参数通常封装在一个结构体中,包含输入参数和返回值。 4. **线程状态管理**:线程的状态由一个布尔标志(如`is_busy`)表示,用于跟踪线程是否正在处理任务。此外,使用条件变量(`pthread_cond_t`)和互斥锁...
也就是说corePoolSize就是线程池大小,maximumPoolSize在我看来是线程池的一种补救措施,即任务量突然过大时的一种补救措施。 不过为了方便理解,在本文后面还是将corePoolSize翻译成核心池大小。 ...
2. **任务调度**:线程池内部维护了一个或多个工作线程,它们会从任务队列中取出任务并执行。当工作线程完成任务后,会返回到等待状态或者被销毁(取决于当前线程池的状态)。 3. **线程管理**:线程池会根据当前的...
1. **FixedThreadPool**:创建一个固定大小的线程池,线程数量由用户指定。当所有线程都在执行任务时,新提交的任务会被放入队列中等待执行。 2. **CachedThreadPool**:创建一个可根据需要创建新线程的线程池,但在...
2. **FixedThreadPool**: `Executors.newFixedThreadPool(nThreads)`创建一个固定大小的线程池,它维护的线程数量始终保持不变。当一个新任务提交而所有线程都在工作时,任务会被添加到队列中等待。这种线程池适合...
因此,合理设定线程池大小是优化并发性能的关键。 5. **工作线程与IO完成端口(IOCP)**:在Windows下,结合IO完成端口可以进一步提高线程池的效率,特别是处理大量I/O操作时。IOCP允许一个线程处理多个已完成的I/O...
MySQL的线程池原理是MySQL ...正确配置线程池大小、合理设置调度策略,可以有效提高数据库服务的稳定性和响应速度。在实际应用中,应根据服务器的硬件资源、并发量和业务特性来调整线程池的相关参数,以达到最佳性能。
其次,资源不足是一个关键问题,不适当的线程池大小可能导致内存和CPU资源的过度消耗。线程本身就需要内存和堆栈空间,而且可能还需要额外的系统资源,如本机线程。过多的线程切换也会带来性能损失。此外,如果任务...
线程池是多线程编程中的一个重要概念,它在服务器端程序中被广泛使用,以提高资源利用率和系统性能。线程池通过预先创建并管理一定数量的线程,可以有效地减少线程创建和销毁的开销,同时提供了一种灵活的线程调度和...