Java性能调优笔记

10 篇文章 0 订阅

调优步骤:衡量系统现状、设定调优目标、寻找性能瓶颈、性能调优、衡量是否到达目标(如果未到达目标,需重新寻找性能瓶颈)、性能调优结束。


寻找性能瓶颈
性能瓶颈的表象:资源消耗过多、外部处理系统的性能不足、资源消耗不多但程序的响应速度却仍达不到要求。

资源消耗:CPU、文件IO、网络IO、内存。
外部处理系统的性能不足:所调用的其他系统提供的功能或数据库操作的响应速度不够。
资源消耗不多但程序的响应速度却仍达不到要求:程序代码运行效率不够高、未充分使用资源、程序结构不合理。


CPU消耗分析
CPU主要用于中断、内核、用户进程的任务处理,优先级为中断>内核>用户进程。

上下文切换:
每个线程分配一定的执行时间,当到达执行时间、线程中有IO阻塞或高优先级线程要执行时,将切换执行的线程。在切换时要存储目前线程的执行状态,并恢复要执行的线程的状态。
对于Java应用,典型的是在进行文件IO操作、网络IO操作、锁等待、线程Sleep时,当前线程会进入阻塞或休眠状态,从而触发上下文切换,上下文切换过多会造成内核占据较多的CPU的使用。

运行队列:
每个CPU核都维护一个可运行的线程队列。系统的load主要由CPU的运行队列来决定。
运行队列值越大,就意味着线程会要消耗越长的时间才能执行完成。

利用率:
CPU在用户进程、内核、中断处理、IO等待、空闲,这五个部分使用百分比。


文件IO消耗分析
Linux在操作文件时,将数据放入文件缓存区,直到内存不够或系统要释放内存给用户进程使用。所以通常情况下只有写文件和第一次读取文件时会产生真正的文件IO。
对于Java应用,造成文件IO消耗高主要是多个线程需要进行大量内容写入(例如频繁的日志写入)的动作、磁盘设备本身的处理速度慢、文件系统慢、操作的文件本身已经很大。


网络IO消耗分析
对于分布式Java应用,网卡中断是不是均衡分配到各CPU(cat/proc/interrupts查看)。


内存消耗分析(-Xms和-Xmx设为相同的值,避免运行期JVM堆内存要不断申请内存)
对于Java应用,内存的消耗主要在Java堆内存上,只有创建线程和使用Direct ByteBuffer才会操作JVM堆外的内存。
JVM内存消耗过多会导致GC执行频繁,CPU消耗增加,应用线程的执行速度严重下降,甚至造成OutOfMemoryError,最终导致Java进程退出。

JVM堆外的内存
swap的消耗、物理内存的消耗、JVM内存的消耗。

 

 

程序执行慢原因分析

锁竞争激烈:很多线程竞争互斥资源,但资源有限, 造成其他线程都处于等待状态。

未充分使用硬件资源:线程操作被串行化。

数据量增长:单表数据量太大(如1个亿)造成数据库读写速度大幅下降(操作此表)。

 

 

 

调优

 

JVM调优(最关键参数为:-Xms -Xmx -Xmn -XX:SurvivorRatio -XX:MaxTenuringThreshold)

 

代大小调优:避免新生代大小设置过小、避免新生代大小设置过大、避免Survivor设置过小或过大、合理设置新生代存活周期。

-Xmn 调整新生代大小,新生代越大通常也意味着更多对象会在minor GC阶段被回收,但可能有可能造成旧生代大小,造成频繁触发Full GC,甚至是OutOfMemoryError。

-XX:SurvivorRatio调整Eden区与Survivor区的大小,Eden 区越大通常也意味着minor GC发生频率越低,但可能有可能造成Survivor区太小,导致对象minor GC后就直接进入旧生代,从而更频繁触发Full GC。

 

GC策略的调优:CMS GC多数动作是和应用并发进行的,确实可以减小GC动作给应用造成的暂停时间。对于Web应用非常需要一个对应用造成暂停时间短的GC,再加上Web应用 的瓶颈都不在CPU上,在G1还不够成熟的情况下,CMS GC是不错的选择。

(如果系统不是CPU密集型,且从新生代进入旧生代的大部分对象是可以回收的,那么采用CMS GC可以更好地在旧生代满之前完成对象的回收,更大程度降低Full GC发生的可能)

 

在调整了内存管理方面的参数后应通过-XX:PrintGCDetails、-XX:+PrintGCTimeStamps、 -XX:+PrintGCApplicationStoppedTime以及jstat或visualvm等方式观察调整后的GC状况。

出内存管理以外的其他方面的调优参数:-XX:CompileThreshold、-XX:+UseFastAccessorMethods、 -XX:+UseBaiasedLocking。

 

 

 

程序调优

 

CPU消耗严重的解决方法

 

CPU us高的解决方法:

CPU us 高的原因主要是执行线程不需要任何挂起动作,且一直执行,导致CPU 没有机会去调度执行其他的线程。

调优方案: 增加Thread.sleep,以释放CPU 的执行权,降低CPU 的消耗。以损失单次执行性能为代价的,但由于其降低了CPU 的消耗,对于多线程的应用而言,反而提高了总体的平均性能。

(在实际的Java应用中类似场景, 对于这种场景最佳方式是改为采用wait/notify机制)

对于其他类似循环次数过多、正则、计算等造成CPU us过高的状况, 则需要结合业务调优。

对于GC频繁,则需要通过JVM调优或程序调优,降低GC的执行次数。

 

CPU sy高的解决方法:

CPU sy 高的原因主要是线程的运行状态要经常切换,对于这种情况,常见的一种优化方法是减少线程数。

调优方案: 将线程数降低

这种调优过后有可能会造成CPU us过高,所以合理设置线程数非常关键。

 

对于Java分布式应用,还有一种典型现象是应用中有较多的网络IO操作和确实需要一些锁竞争机制(如数据库连接池),但为了能够支撑搞得并发量,可采用协程(Coroutine)来支撑更高的并发量,避免并发量上涨后造成CPU sy消耗严重、系统load迅速上涨和系统性能下降。

在Java中实现协程的框架有Kilim,Kilim执行一项任务创建Task,使用Task的暂停机制,而不是Thread,Kilim承担了线程调度以及上下切换动作,Task相对于原生Thread而言就轻量级多了,且能更好利用CPU。Kilim带来的是线程使用率的提升,但同时由于要在JVM堆中保存Task上下文信息,因此在采用Kilim的情况下要消耗更多的内存。(目前JDK 7中也有一个支持协程方式的实现,另外基于JVM的Scala的Actor也可用于在Java使用协程)

 

 

 

文件IO消耗严重的解决方法

从程序的角度而言,造成文件IO消耗严重的原因主要是多个线程在写进行大量的数据到同一文件,导致文件很快变得很大,从而写入速度越来越慢,并造成各线程激烈争抢文件锁。

 

常用调优方法:

异步写文件

批量读写

限流

限制文件大小

 

 

 

网络IO消耗严重的解决方法

从程序的角度而言,造成网络IO消耗严重的原因主要是同时需要发送或接收的包太多。

 

常用调优方法:

限流,限流通常是限制发送packet的频率,从而在网络IO消耗可接受的情况下来发送packget。

 

 

内存消耗严重的解决方法

释放不必要的引用:代码持有了不需要的对象引用,造成这些对象无法被GC,从而占据了JVM堆内存。(使用ThreadLocal:注意在线程内动作执行完毕时,需执行ThreadLocal.set把对象清除,避免持有不必要的对象引用)

使用对象缓存池:创建对象要消耗一定的CPU以及内存,使用对象缓存池一定程度上可降低JVM堆内存的使用。

采用合理的缓存失效算法:如果放入太多对象在缓存池中,反而会造成内存的严重消耗, 同时由于缓存池一直对这些对象持有引用,从而造成Full GC增多,对于这种状况要合理控制缓存池的大小,避免缓存池的对象数量无限上涨。(经典的缓存失效算法来清除缓存池中的对象:FIFO、LRU、LFU等)

合理使用SoftReference和WeekReference:SoftReference的对象会在内存不够用的时候回收,WeekReference的对象会在Full GC的时候回收。

 

 

资源消耗不多但程序执行慢的情况的解决方法

 

降低锁竞争: 多线多了,锁竞争的状况会比较明显,这时候线程很容易处于等待锁的状况,从而导致性能下降以及CPU sy上升。

使用并发包中的类:大多数采用了lock-free、nonblocking算法。

使用Treiber算法:基于CAS以及AtomicReference。

使用Michael-Scott非阻塞队列算法:基于CAS以及AtomicReference,典型ConcurrentLindkedQueue。

(基于CAS和AtomicReference来实现无阻塞是不错的选择,但值得注意的是,lock-free算法需不断的循环比较来保证资源的一致性的,对于冲突较多的应用场景而言,会带来更高的CPU消耗,因此不一定采用CAS实现无阻塞的就一定比采用lock方式的性能好。 还有一些无阻塞算法的改进:MCAS、WSTM等)

尽可能少用锁:尽可能只对需要控制的资源做加锁操作(通常没有必要对整个方法加锁,尽可能让锁最小化,只对互斥及原子操作的地方加锁,加锁时尽可能以保护资源的最小化粒度为单位–如只对需要保护的资源加锁而不是this)。

拆分锁:独占锁拆分为多把锁(读写锁拆分、类似ConcurrentHashMap中默认拆分为16把锁),很多程度上能提高读写的性能,但需要注意在采用拆分锁后,全局性质的操作会变得比较复杂(如ConcurrentHashMap中size操作)。(拆分锁太多也会造成副作用,如CPU消耗明显增加)

去除读写操作的互斥:在修改时加锁,并复制对象进行修改,修改完毕后切换对象的引用,从而读取时则不加锁。这种称为CopyOnWrite,CopyOnWriteArrayList是典型实现,好处是可以明显提升读的性能,适合读多写少的场景, 但由于写操作每次都要复制一份对象,会消耗更多的内存。

 

 

充分利用硬件资源(CPU和内存):

 

充分利用CPU

在能并行处理的场景中未使用足够的线程(线程增加:CPU资源消耗可接受且不会带来激烈竞争锁的场景下), 例如单线程的计算,可以拆分为多个线程分别计算,最后将结果合并,JDK 7中的fork-join框架。

Amdahl定律公式:1/(F+(1-F)/N)。

 

充分利用内存

数据的缓存、耗时资源的缓存(数据库连接创建、网络连接的创建等)、页面片段的缓存。

毕竟内存的读取肯定远快于硬盘、网络的读取, 在内存消耗可接受、GC频率、以及系统结构(例如集群环境可能会带来缓存的同步)可接受情况下,应充分利用内存来缓存数据,提升系统的性能。

 

 

总结:

好的调优策略是收益比(调优后提升的效果/调优改动所需付出的代价)最高的,通常来说简单的系统调优比较好做,因此尽量保持单机上应用的纯粹性, 这是大型系统的基本架构原则。

调优的三大有效原则:充分而不过分使用硬件资源、合理调整JVM、合理使用JDK包。

 

 

学习参考资料:

《分布式Java应用:基础与实践》

 

 

补充《分布式Java应用:基础与实践》一些代码样例:

 

 

cpu———————————–

 

CpuNotUseEffectiveDemo

  1. /** 
  2.  *  
  3.  */  
  4. package tune.program.cpu;  
  5. import java.util.ArrayList;  
  6. import java.util.List;  
  7. import java.util.Random;  
  8. /** 
  9.  * 未充分利用CPU:在能并行处理的场景中未使用足够的线程(线程增加:CPU资源消耗可接受且不会带来激烈竞争锁的场景下) 
  10.  *  
  11.  * @author yangwm Aug 25, 2010 9:54:50 AM 
  12.  */  
  13. public class CpuNotUseEffectiveDemo {  
  14.       
  15.     private static int executeTimes = 10;  
  16.     private static int taskCount = 200;  
  17.       
  18.     public static void main(String[] args) throws Exception {  
  19.         Task task = new Task();  
  20.         for (int i = 0; i < taskCount; i++) {  
  21.             task.addTask(Integer.toString(i));  
  22.         }  
  23.           
  24.         long beginTime = System.currentTimeMillis();  
  25.         for (int i = 0; i < executeTimes; i++) {  
  26.             System.out.println(”Round: ” + (i + 1));  
  27.             Thread thread = new Thread(task);  
  28.             thread.start();  
  29.             thread.join();  
  30.         }  
  31.         long endTime = System.currentTimeMillis();  
  32.         System.out.println(”Execute summary: Round( ” + executeTimes + “ ) TaskCount Per Round( ” + taskCount   
  33.                 + ” ) Execute Time ( ” + (endTime - beginTime) + “ ) ms”);  
  34.     }  
  35.       
  36.     static class Task implements Runnable {  
  37.         List<String> tasks = new ArrayList<String>();  
  38.         Random random = new Random();  
  39.         boolean exitFlag = false;  
  40.           
  41.         public void addTask(String task) {  
  42.             List<String> copyTasks = new ArrayList<String>(tasks);  
  43.             copyTasks.add(task);  
  44.               
  45.             tasks = copyTasks;  
  46.         }  
  47.         @Override  
  48.         public void run() {  
  49.             List<String> runTasks = tasks;  
  50.             List<String> removeTasks = new ArrayList<String>();  
  51.             for (String task : runTasks) {  
  52.                 try {  
  53.                     Thread.sleep(random.nextInt(10));  
  54.                 } catch (Exception e) {  
  55.                     e.printStackTrace();  
  56.                 }  
  57.                   
  58.                 removeTasks.add(task);  
  59.             }  
  60.               
  61.             try {  
  62.                 Thread.sleep(10);  
  63.             } catch (Exception e) {  
  64.                 e.printStackTrace();  
  65.             }  
  66.         }  
  67.           
  68.     }  
  69.       
  70. }  
  71. /* 
  72. Round: 1 
  73. …… 
  74. Round: 10 
  75. Execute summary: Round( 10 ) TaskCount Per Round( 200 ) Execute Time ( 10687 ) ms 
  76. */  
/** * */ package tune.program.cpu; import java.util.ArrayList; import java.util.List; import java.util.Random; /** * 未充分利用CPU:在能并行处理的场景中未使用足够的线程(线程增加:CPU资源消耗可接受且不会带来激烈竞争锁的场景下) * * @author yangwm Aug 25, 2010 9:54:50 AM */ public class CpuNotUseEffectiveDemo { private static int executeTimes = 10; private static int taskCount = 200; public static void main(String[] args) throws Exception { Task task = new Task(); for (int i = 0; i < taskCount; i++) { task.addTask(Integer.toString(i)); } long beginTime = System.currentTimeMillis(); for (int i = 0; i < executeTimes; i++) { System.out.println(“Round: ” + (i + 1)); Thread thread = new Thread(task); thread.start(); thread.join(); } long endTime = System.currentTimeMillis(); System.out.println(“Execute summary: Round( ” + executeTimes + ” ) TaskCount Per Round( ” + taskCount + ” ) Execute Time ( ” + (endTime - beginTime) + ” ) ms”); } static class Task implements Runnable { List<String> tasks = new ArrayList<String>(); Random random = new Random(); boolean exitFlag = false; public void addTask(String task) { List<String> copyTasks = new ArrayList<String>(tasks); copyTasks.add(task); tasks = copyTasks; } @Override public void run() { List<String> runTasks = tasks; List<String> removeTasks = new ArrayList<String>(); for (String task : runTasks) { try { Thread.sleep(random.nextInt(10)); } catch (Exception e) { e.printStackTrace(); } removeTasks.add(task); } try { Thread.sleep(10); } catch (Exception e) { e.printStackTrace(); } } } } /* Round: 1 …… Round: 10 Execute summary: Round( 10 ) TaskCount Per Round( 200 ) Execute Time ( 10687 ) ms */

 

 

CpuUseEffectiveDemo

  1. /** 
  2.  *  
  3.  */  
  4. package tune.program.cpu;  
  5. import java.util.ArrayList;  
  6. import java.util.List;  
  7. import java.util.Random;  
  8. import java.util.concurrent.CountDownLatch;  
  9.   
  10. /** 
  11.  * 充分利用CPU:在能并行处理的场景中使用足够的线程(线程增加:CPU资源消耗可接受且不会带来激烈竞争锁的场景下) 
  12.  *  
  13.  * @author yangwm Aug 25, 2010 9:54:50 AM 
  14.  */  
  15. public class CpuUseEffectiveDemo {  
  16.       
  17.     private static int executeTimes = 10;  
  18.     private static int taskCount = 200;  
  19.     private static final int TASK_THREADCOUNT = 16;  
  20.     private static CountDownLatch latch;  
  21.       
  22.     public static void main(String[] args) throws Exception {  
  23.         Task[] tasks = new Task[TASK_THREADCOUNT];  
  24.         for (int i = 0; i < TASK_THREADCOUNT; i++) {  
  25.             tasks[i] = new Task();  
  26.         }  
  27.         for (int i = 0; i < taskCount; i++) {  
  28.             int mod = i % TASK_THREADCOUNT;  
  29.             tasks[mod].addTask(Integer.toString(i));  
  30.         }  
  31.           
  32.         long beginTime = System.currentTimeMillis();  
  33.         for (int i = 0; i < executeTimes; i++) {  
  34.             System.out.println(”Round: ” + (i + 1));  
  35.             latch = new CountDownLatch(TASK_THREADCOUNT);  
  36.             for (int j = 0; j < TASK_THREADCOUNT; j++) {  
  37.                 Thread thread = new Thread(tasks[j]);  
  38.                 thread.start();  
  39.             }  
  40.             latch.await();  
  41.         }  
  42.         long endTime = System.currentTimeMillis();  
  43.         System.out.println(”Execute summary: Round( ” + executeTimes + “ ) TaskCount Per Round( ” + taskCount   
  44.                 + ” ) Execute Time ( ” + (endTime - beginTime) + “ ) ms”);  
  45.     }  
  46.       
  47.     static class Task implements Runnable {  
  48.         List<String> tasks = new ArrayList<String>();  
  49.         Random random = new Random();  
  50.         boolean exitFlag = false;  
  51.           
  52.         public void addTask(String task) {  
  53.             List<String> copyTasks = new ArrayList<String>(tasks);  
  54.             copyTasks.add(task);  
  55.               
  56.             tasks = copyTasks;  
  57.         }  
  58.         @Override  
  59.         public void run() {  
  60.             List<String> runTasks = tasks;  
  61.             List<String> removeTasks = new ArrayList<String>();  
  62.             for (String task : runTasks) {  
  63.                 try {  
  64.                     Thread.sleep(random.nextInt(10));  
  65.                 } catch (Exception e) {  
  66.                     e.printStackTrace();  
  67.                 }  
  68.                   
  69.                 removeTasks.add(task);  
  70.             }  
  71.               
  72.             try {  
  73.                 Thread.sleep(10);  
  74.             } catch (Exception e) {  
  75.                 e.printStackTrace();  
  76.             }  
  77.             latch.countDown();  
  78.         }  
  79.           
  80.     }  
  81.       
  82. }  
  83. /* 
  84. Round: 1 
  85. …… 
  86. Round: 10 
  87. Execute summary: Round( 10 ) TaskCount Per Round( 200 ) Execute Time ( 938 ) ms 
  88. */  
/** * */ package tune.program.cpu; import java.util.ArrayList; import java.util.List; import java.util.Random; import java.util.concurrent.CountDownLatch; /** * 充分利用CPU:在能并行处理的场景中使用足够的线程(线程增加:CPU资源消耗可接受且不会带来激烈竞争锁的场景下) * * @author yangwm Aug 25, 2010 9:54:50 AM */ public class CpuUseEffectiveDemo { private static int executeTimes = 10; private static int taskCount = 200; private static final int TASK_THREADCOUNT = 16; private static CountDownLatch latch; public static void main(String[] args) throws Exception { Task[] tasks = new Task[TASK_THREADCOUNT]; for (int i = 0; i < TASK_THREADCOUNT; i++) { tasks[i] = new Task(); } for (int i = 0; i < taskCount; i++) { int mod = i % TASK_THREADCOUNT; tasks[mod].addTask(Integer.toString(i)); } long beginTime = System.currentTimeMillis(); for (int i = 0; i < executeTimes; i++) { System.out.println(“Round: ” + (i + 1)); latch = new CountDownLatch(TASK_THREADCOUNT); for (int j = 0; j < TASK_THREADCOUNT; j++) { Thread thread = new Thread(tasks[j]); thread.start(); } latch.await(); } long endTime = System.currentTimeMillis(); System.out.println(“Execute summary: Round( ” + executeTimes + ” ) TaskCount Per Round( ” + taskCount + ” ) Execute Time ( ” + (endTime - beginTime) + ” ) ms”); } static class Task implements Runnable { List<String> tasks = new ArrayList<String>(); Random random = new Random(); boolean exitFlag = false; public void addTask(String task) { List<String> copyTasks = new ArrayList<String>(tasks); copyTasks.add(task); tasks = copyTasks; } @Override public void run() { List<String> runTasks = tasks; List<String> removeTasks = new ArrayList<String>(); for (String task : runTasks) { try { Thread.sleep(random.nextInt(10)); } catch (Exception e) { e.printStackTrace(); } removeTasks.add(task); } try { Thread.sleep(10); } catch (Exception e) { e.printStackTrace(); } latch.countDown(); } } } /* Round: 1 …… Round: 10 Execute summary: Round( 10 ) TaskCount Per Round( 200 ) Execute Time ( 938 ) ms */

 

 

fileio——————————————————————-

 

IOWaitHighDemo

  1. /** 
  2.  *  
  3.  */  
  4. package tune.program.fileio;  
  5. import java.io.BufferedWriter;  
  6. import java.io.File;  
  7. import java.io.FileWriter;  
  8. import java.util.Random;  
  9. /** 
  10.  * 文件IO消耗严重的原因主要是多个线程在写进行大量的数据到同一文件, 
  11.  * 导致文件很快变得很大,从而写入速度越来越慢,并造成各线程激烈争抢文件锁。 
  12.  *  
  13.  * @author yangwm Aug 21, 2010 9:48:34 PM 
  14.  */  
  15. public class IOWaitHighDemo {  
  16.     private String fileName = “iowait.log”;  
  17.       
  18.     private static int threadCount = Runtime.getRuntime().availableProcessors();  
  19.       
  20.     private Random random = new Random();  
  21.       
  22.     public static void main(String[] args) throws Exception {  
  23.         if (args.length == 1) {  
  24.             threadCount = Integer.parseInt(args[1]);  
  25.         }  
  26.           
  27.         IOWaitHighDemo demo = new IOWaitHighDemo();  
  28.         demo.runTest();  
  29.     }  
  30.       
  31.     private void runTest() throws Exception {  
  32.         File file = new File(fileName);  
  33.         file.createNewFile();  
  34.           
  35.         for (int i = 0; i < threadCount; i++) {  
  36.             new Thread(new Task()).start();  
  37.         }  
  38.           
  39.     }  
  40.       
  41.     class Task implements Runnable {  
  42.         @Override  
  43.         public void run() {  
  44.             while (true) {  
  45.                 try {  
  46.                     StringBuilder strBuilder = new StringBuilder(“====begin====/n”);  
  47.                     String threadName = Thread.currentThread().getName();  
  48.                     for (int i = 0; i < 100000; i++) {  
  49.                         strBuilder.append(threadName);  
  50.                         strBuilder.append(”/n”);  
  51.                     }  
  52.                     strBuilder.append(”====end====/n”);  
  53.                       
  54.                     BufferedWriter writer = new BufferedWriter(new FileWriter(fileName, true));  
  55.                     writer.write(strBuilder.toString());  
  56.                     writer.close();  
  57.                     Thread.sleep(random.nextInt(10));  
  58.                 } catch (Exception e) {  
  59.                       
  60.                 }  
  61.             }  
  62.         }  
  63.           
  64.     }  
  65.       
  66. }  
  67. /* 
  68. C:/Documents and Settings/yangwm>jstack 2656 
  69. 2010-08-21 23:24:17 
  70. Full thread dump Java HotSpot(TM) Client VM (17.0-b05 mixed mode): 
  71. ”DestroyJavaVM” prio=6 tid=0x00868c00 nid=0xde0 waiting on condition [0x00000000] 
  72.    java.lang.Thread.State: RUNNABLE 
  73. ”Thread-1” prio=6 tid=0x0ab9dc00 nid=0xb7c runnable [0x0b0bf000] 
  74.    java.lang.Thread.State: RUNNABLE 
  75.         at java.io.FileOutputStream.close0(Native Method) 
  76.         at java.io.FileOutputStream.close(FileOutputStream.java:336) 
  77.         at sun.nio.cs.StreamEncoder.implClose(StreamEncoder.java:320) 
  78.         at sun.nio.cs.StreamEncoder.close(StreamEncoder.java:149) 
  79.         - locked <0x034dd268> (a java.io.FileWriter) 
  80.         at java.io.OutputStreamWriter.close(OutputStreamWriter.java:233) 
  81.         at java.io.BufferedWriter.close(BufferedWriter.java:265) 
  82.         - locked <0x034dd268> (a java.io.FileWriter) 
  83.         at tune.IOWaitHighDemoTask.run(IOWaitHighDemo.java:58)</span>&nbsp;</span></li><li class=""><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;java.lang.Thread.run(Thread.java:717)</span>&nbsp;</span></li><li class="alt"><span><span class="comment">"Thread-0"&nbsp;prio=6&nbsp;tid=0x0ab9d400&nbsp;nid=0x80c&nbsp;runnable&nbsp;[0x0b06f000]</span>&nbsp;</span></li><li class=""><span><span class="comment">&nbsp;&nbsp;&nbsp;java.lang.Thread.State:&nbsp;RUNNABLE</span>&nbsp;</span></li><li class="alt"><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;java.io.FileOutputStream.writeBytes(Native&nbsp;Method)</span>&nbsp;</span></li><li class=""><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;java.io.FileOutputStream.write(FileOutputStream.java:292)</span>&nbsp;</span></li><li class="alt"><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:221)</span>&nbsp;</span></li><li class=""><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;sun.nio.cs.StreamEncoder.implWrite(StreamEncoder.java:282)</span>&nbsp;</span></li><li class="alt"><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;sun.nio.cs.StreamEncoder.write(StreamEncoder.java:125)</span>&nbsp;</span></li><li class=""><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;locked&nbsp;&lt;0x034e1290&gt;&nbsp;(a&nbsp;java.io.FileWriter)</span>&nbsp;</span></li><li class="alt"><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;java.io.OutputStreamWriter.write(OutputStreamWriter.java:207)</span>&nbsp;</span></li><li class=""><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;java.io.BufferedWriter.flushBuffer(BufferedWriter.java:128)</span>&nbsp;</span></li><li class="alt"><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;locked&nbsp;&lt;0x034e1290&gt;&nbsp;(a&nbsp;java.io.FileWriter)</span>&nbsp;</span></li><li class=""><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;java.io.BufferedWriter.write(BufferedWriter.java:229)</span>&nbsp;</span></li><li class="alt"><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;locked&nbsp;&lt;0x034e1290&gt;&nbsp;(a&nbsp;java.io.FileWriter)</span>&nbsp;</span></li><li class=""><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;java.io.Writer.write(Writer.java:157)</span>&nbsp;</span></li><li class="alt"><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;tune.IOWaitHighDemoTask.run(IOWaitHighDemo.java:57) 
  84.         at java.lang.Thread.run(Thread.java:717) 
  85. ”Low Memory Detector” daemon prio=6 tid=0x0ab6f800 nid=0xfb0 runnable [0x00000000] 
  86.    java.lang.Thread.State: RUNNABLE 
  87. ”CompilerThread0” daemon prio=10 tid=0x0ab6c800 nid=0x5fc waiting on condition [0x00000000] 
  88.    java.lang.Thread.State: RUNNABLE 
  89. ”Attach Listener” daemon prio=10 tid=0x0ab67800 nid=0x6fc waiting on condition [0x00000000] 
  90.    java.lang.Thread.State: RUNNABLE 
  91. ”Signal Dispatcher” daemon prio=10 tid=0x0ab66800 nid=0x5a0 runnable [0x00000000] 
  92.    java.lang.Thread.State: RUNNABLE 
  93. ”Finalizer” daemon prio=8 tid=0x0ab54000 nid=0xe74 in Object.wait() [0x0ac8f000] 
  94.    java.lang.Thread.State: WAITING (on object monitor) 
  95.         at java.lang.Object.wait(Native Method) 
  96.         - waiting on <0x02f15d90> (a java.lang.ref.ReferenceQueueLock)</span>&nbsp;</span></li><li class="alt"><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:135)</span>&nbsp;</span></li><li class=""><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;locked&nbsp;&lt;0x02f15d90&gt;&nbsp;(a&nbsp;java.lang.ref.ReferenceQueueLock) 
  97.         at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:151) 
  98.         at java.lang.ref.FinalizerFinalizerThread.run(Finalizer.java:177)</span>&nbsp;</span></li><li class="alt"><span><span class="comment">"Reference&nbsp;Handler"&nbsp;daemon&nbsp;prio=10&nbsp;tid=0x0ab4f800&nbsp;nid=0x8a4&nbsp;in&nbsp;Object.wait()&nbsp;[0x0ac3f000]</span>&nbsp;</span></li><li class=""><span><span class="comment">&nbsp;&nbsp;&nbsp;java.lang.Thread.State:&nbsp;WAITING&nbsp;(on&nbsp;object&nbsp;monitor)</span>&nbsp;</span></li><li class="alt"><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;java.lang.Object.wait(Native&nbsp;Method)</span>&nbsp;</span></li><li class=""><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;waiting&nbsp;on&nbsp;&lt;0x02f15af8&gt;&nbsp;(a&nbsp;java.lang.ref.ReferenceLock) 
  99.         at java.lang.Object.wait(Object.java:502) 
  100.         at java.lang.ref.ReferenceReferenceHandler.run(Reference.java:133)</span>&nbsp;</span></li><li class="alt"><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;locked&nbsp;&lt;0x02f15af8&gt;&nbsp;(a&nbsp;java.lang.ref.ReferenceLock) 
  101. ”VM Thread” prio=10 tid=0x0ab4a800 nid=0x1d0 runnable 
  102. ”VM Periodic Task Thread” prio=10 tid=0x0ab7d400 nid=0x464 waiting on condition 
  103. JNI global references: 693 
  104.  
  105. C:/Documents and Settings/yangwm> 
  106. */  
/** * */ package tune.program.fileio; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.util.Random; /** * 文件IO消耗严重的原因主要是多个线程在写进行大量的数据到同一文件, * 导致文件很快变得很大,从而写入速度越来越慢,并造成各线程激烈争抢文件锁。 * * @author yangwm Aug 21, 2010 9:48:34 PM */ public class IOWaitHighDemo { private String fileName = “iowait.log”; private static int threadCount = Runtime.getRuntime().availableProcessors(); private Random random = new Random(); public static void main(String[] args) throws Exception { if (args.length == 1) { threadCount = Integer.parseInt(args[1]); } IOWaitHighDemo demo = new IOWaitHighDemo(); demo.runTest(); } private void runTest() throws Exception { File file = new File(fileName); file.createNewFile(); for (int i = 0; i < threadCount; i++) { new Thread(new Task()).start(); } } class Task implements Runnable { @Override public void run() { while (true) { try { StringBuilder strBuilder = new StringBuilder(“====begin====/n”); String threadName = Thread.currentThread().getName(); for (int i = 0; i < 100000; i++) { strBuilder.append(threadName); strBuilder.append(“/n”); } strBuilder.append(“====end====/n”); BufferedWriter writer = new BufferedWriter(new FileWriter(fileName, true)); writer.write(strBuilder.toString()); writer.close(); Thread.sleep(random.nextInt(10)); } catch (Exception e) { } } } } } /* C:/Documents and Settings/yangwm>jstack 2656 2010-08-21 23:24:17 Full thread dump Java HotSpot(TM) Client VM (17.0-b05 mixed mode): “DestroyJavaVM” prio=6 tid=0x00868c00 nid=0xde0 waiting on condition [0x00000000] java.lang.Thread.State: RUNNABLE “Thread-1” prio=6 tid=0x0ab9dc00 nid=0xb7c runnable [0x0b0bf000] java.lang.Thread.State: RUNNABLE at java.io.FileOutputStream.close0(Native Method) at java.io.FileOutputStream.close(FileOutputStream.java:336) at sun.nio.cs.StreamEncoder.implClose(StreamEncoder.java:320) at sun.nio.cs.StreamEncoder.close(StreamEncoder.java:149) - locked <0x034dd268> (a java.io.FileWriter) at java.io.OutputStreamWriter.close(OutputStreamWriter.java:233) at java.io.BufferedWriter.close(BufferedWriter.java:265) - locked <0x034dd268> (a java.io.FileWriter) at tune.IOWaitHighDemo Task.run(IOWaitHighDemo.java:58)atjava.lang.Thread.run(Thread.java:717)Thread0prio=6tid=0x0ab9d400nid=0x80crunnable[0x0b06f000]java.lang.Thread.State:RUNNABLEatjava.io.FileOutputStream.writeBytes(NativeMethod)atjava.io.FileOutputStream.write(FileOutputStream.java:292)atsun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:221)atsun.nio.cs.StreamEncoder.implWrite(StreamEncoder.java:282)atsun.nio.cs.StreamEncoder.write(StreamEncoder.java:125)locked<0x034e1290>(ajava.io.FileWriter)atjava.io.OutputStreamWriter.write(OutputStreamWriter.java:207)atjava.io.BufferedWriter.flushBuffer(BufferedWriter.java:128)locked<0x034e1290>(ajava.io.FileWriter)atjava.io.BufferedWriter.write(BufferedWriter.java:229)locked<0x034e1290>(ajava.io.FileWriter)atjava.io.Writer.write(Writer.java:157)attune.IOWaitHighDemo Task.run(IOWaitHighDemo.java:57) at java.lang.Thread.run(Thread.java:717) “Low Memory Detector” daemon prio=6 tid=0x0ab6f800 nid=0xfb0 runnable [0x00000000] java.lang.Thread.State: RUNNABLE “CompilerThread0” daemon prio=10 tid=0x0ab6c800 nid=0x5fc waiting on condition [0x00000000] java.lang.Thread.State: RUNNABLE “Attach Listener” daemon prio=10 tid=0x0ab67800 nid=0x6fc waiting on condition [0x00000000] java.lang.Thread.State: RUNNABLE “Signal Dispatcher” daemon prio=10 tid=0x0ab66800 nid=0x5a0 runnable [0x00000000] java.lang.Thread.State: RUNNABLE “Finalizer” daemon prio=8 tid=0x0ab54000 nid=0xe74 in Object.wait() [0x0ac8f000] java.lang.Thread.State: WAITING (on object monitor) at java.lang.Object.wait(Native Method) - waiting on <0x02f15d90> (a java.lang.ref.ReferenceQueue Lock)atjava.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:135)locked<0x02f15d90>(ajava.lang.ref.ReferenceQueue Lock) at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:151) at java.lang.ref.Finalizer FinalizerThread.run(Finalizer.java:177)ReferenceHandlerdaemonprio=10tid=0x0ab4f800nid=0x8a4inObject.wait()[0x0ac3f000]java.lang.Thread.State:WAITING(onobjectmonitor)atjava.lang.Object.wait(NativeMethod)waitingon<0x02f15af8>(ajava.lang.ref.Reference Lock) at java.lang.Object.wait(Object.java:502) at java.lang.ref.Reference ReferenceHandler.run(Reference.java:133)locked<0x02f15af8>(ajava.lang.ref.Reference Lock) “VM Thread” prio=10 tid=0x0ab4a800 nid=0x1d0 runnable “VM Periodic Task Thread” prio=10 tid=0x0ab7d400 nid=0x464 waiting on condition JNI global references: 693 C:/Documents and Settings/yangwm> */

 

 

LogControl

  1. /** 
  2.  *  
  3.  */  
  4. package tune.program.fileio;  
  5. import java.util.concurrent.atomic.AtomicInteger;  
  6. /** 
  7.  * 日志控制:采用简单策略为统计一段时间内日志输出频率, 当超出这个频率时,一段时间内不再写log  
  8.  *  
  9.  * @author yangwm Aug 24, 2010 10:41:43 AM 
  10.  */  
  11. public class LogControl {  
  12.       
  13.     public static void main(String[] args) {  
  14.         for (int i = 1; i <= 1000; i++) {  
  15.             if (LogControl.isLog()) {  
  16.                 //logger.error(errorInfo, throwable);  
  17.                 System.out.println(”errorInfo ” + i);  
  18.             }  
  19.               
  20.             //    
  21.             if (i % 100 == 0) {  
  22.                 try {  
  23.                     Thread.sleep(1000);  
  24.                 } catch (InterruptedException e) {  
  25.                     e.printStackTrace();  
  26.                 }  
  27.             }  
  28.         }  
  29.     }  
  30.       
  31.       
  32.     private static final long INTERVAL = 1000;  
  33.     private static final long PUNISH_TIME = 5000;  
  34.     private static final int ERROR_THRESHOLD = 100;  
  35.     private static AtomicInteger count = new AtomicInteger(0);  
  36.     private static long beginTime;  
  37.     private static long punishTimeEnd;  
  38.       
  39.     // 由于控制不用非常精确, 因此忽略此处的并发问题  
  40.     public static boolean isLog() {  
  41.         //System.out.println(count.get() + ”, ” + beginTime + ”, ” + punishTimeEnd + ”, ” + System.currentTimeMillis());  
  42.           
  43.         // 不写日志阶段   
  44.         if (punishTimeEnd > 0 && punishTimeEnd > System.currentTimeMillis()) {  
  45.             return false;  
  46.         }  
  47.           
  48.         // 重新计数  
  49.         if (count.getAndIncrement() == 0) {  
  50.             beginTime = System.currentTimeMillis();  
  51.             return true;  
  52.         } else { // 已在计数  
  53.             // 超过阀门值, 设置count为0并设置一段时间内不写日志  
  54.             if (count.get() > ERROR_THRESHOLD) {  
  55.                 count.set(0);  
  56.                 punishTimeEnd = PUNISH_TIME + System.currentTimeMillis();  
  57.                 return false;  
  58.             }   
  59.             // 没超过阀门值, 且当前时间已超过计数周期,则重新计算   
  60.             else if (System.currentTimeMillis() > (beginTime + INTERVAL)) {  
  61.                 count.set(0);  
  62.             }  
  63.               
  64.             return true;  
  65.         }  
  66.     }  
  67.       
  68. }  
  69. /* 
  70. errorInfo 1 
  71. errorInfo 2 
  72. …… 
  73. errorInfo 99 
  74. errorInfo 100 
  75. errorInfo 601 
  76. errorInfo 602 
  77. …… 
  78. errorInfo 699 
  79. errorInfo 700 
  80. */   
/** * */ package tune.program.fileio; import java.util.concurrent.atomic.AtomicInteger; /** * 日志控制:采用简单策略为统计一段时间内日志输出频率, 当超出这个频率时,一段时间内不再写log * * @author yangwm Aug 24, 2010 10:41:43 AM */ public class LogControl { public static void main(String[] args) { for (int i = 1; i <= 1000; i++) { if (LogControl.isLog()) { //logger.error(errorInfo, throwable); System.out.println(“errorInfo ” + i); } // if (i % 100 == 0) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } } private static final long INTERVAL = 1000; private static final long PUNISH_TIME = 5000; private static final int ERROR_THRESHOLD = 100; private static AtomicInteger count = new AtomicInteger(0); private static long beginTime; private static long punishTimeEnd; // 由于控制不用非常精确, 因此忽略此处的并发问题 public static boolean isLog() { //System.out.println(count.get() + “, ” + beginTime + “, ” + punishTimeEnd + “, ” + System.currentTimeMillis()); // 不写日志阶段 if (punishTimeEnd > 0 && punishTimeEnd > System.currentTimeMillis()) { return false; } // 重新计数 if (count.getAndIncrement() == 0) { beginTime = System.currentTimeMillis(); return true; } else { // 已在计数 // 超过阀门值, 设置count为0并设置一段时间内不写日志 if (count.get() > ERROR_THRESHOLD) { count.set(0); punishTimeEnd = PUNISH_TIME + System.currentTimeMillis(); return false; } // 没超过阀门值, 且当前时间已超过计数周期,则重新计算 else if (System.currentTimeMillis() > (beginTime + INTERVAL)) { count.set(0); } return true; } } } /* errorInfo 1 errorInfo 2 …… errorInfo 99 errorInfo 100 errorInfo 601 errorInfo 602 …… errorInfo 699 errorInfo 700 */ 

 

 

memory——————————————————————-

 

MemoryHighDemo

 

  1. /** 
  2.  *  
  3.  */  
  4. package tune.program.memory;  
  5. import java.nio.ByteBuffer;  
  6. /** 
  7.  * direct bytebuffer消耗的是jvm堆外的内存,但同样是基于GC方式来释放的。 
  8.  *  
  9.  * @author yangwm Aug 21, 2010 9:40:18 PM 
  10.  */  
  11. public class MemoryHighDemo {  
  12.       
  13.     public static void main(String[] args) throws Exception{  
  14.         Thread.sleep(20000);  
  15.         System.out.println(”read to create bytes,so jvm heap will be used”);  
  16.         byte[] bytes=new byte[128*1000*1000];  
  17.         bytes[0]=1;  
  18.         bytes[1]=2;  
  19.         Thread.sleep(10000);  
  20.         System.out.println(”read to allocate & put direct bytebuffer,no jvm heap should be used”);  
  21.         ByteBuffer buffer=ByteBuffer.allocateDirect(128*1024*1024);  
  22.         buffer.put(bytes);  
  23.         buffer.flip();  
  24.         Thread.sleep(10000);  
  25.         System.out.println(”ready to gc,jvm heap will be freed”);  
  26.         bytes=null;  
  27.         System.gc();  
  28.         Thread.sleep(10000);  
  29.         System.out.println(”read to get bytes,then jvm heap will be used”);  
  30.         byte[] resultbytes=new byte[128*1000*1000];  
  31.         buffer.get(resultbytes);  
  32.         System.out.println(”resultbytes[1] is: ”+resultbytes[1]);  
  33.         Thread.sleep(10000);  
  34.         System.out.println(”read to gc all”);  
  35.         buffer=null;  
  36.         resultbytes=null;  
  37.         System.gc();  
  38.         Thread.sleep(10000);  
  39.     }  
  40.       
  41. }  
  42. /* 
  43. D:/study/tempProject/JavaLearn/classes>java -Xms140M -Xmx140M tune.MemoryHighDemo 
  44. read to create bytes,so jvm heap will be used 
  45. read to allocate & put direct bytebuffer,no jvm heap should be used 
  46. ready to gc,jvm heap will be freed 
  47. read to get bytes,then jvm heap will be used 
  48. resultbytes[1] is: 2 
  49. read to gc all 
  50. */  
/** * */ package tune.program.memory; import java.nio.ByteBuffer; /** * direct bytebuffer消耗的是jvm堆外的内存,但同样是基于GC方式来释放的。 * * @author yangwm Aug 21, 2010 9:40:18 PM */ public class MemoryHighDemo { public static void main(String[] args) throws Exception{ Thread.sleep(20000); System.out.println(“read to create bytes,so jvm heap will be used”); byte[] bytes=new byte[128*1000*1000]; bytes[0]=1; bytes[1]=2; Thread.sleep(10000); System.out.println(“read to allocate & put direct bytebuffer,no jvm heap should be used”); ByteBuffer buffer=ByteBuffer.allocateDirect(128*1024*1024); buffer.put(bytes); buffer.flip(); Thread.sleep(10000); System.out.println(“ready to gc,jvm heap will be freed”); bytes=null; System.gc(); Thread.sleep(10000); System.out.println(“read to get bytes,then jvm heap will be used”); byte[] resultbytes=new byte[128*1000*1000]; buffer.get(resultbytes); System.out.println(“resultbytes[1] is: “+resultbytes[1]); Thread.sleep(10000); System.out.println(“read to gc all”); buffer=null; resultbytes=null; System.gc(); Thread.sleep(10000); } } /* D:/study/tempProject/JavaLearn/classes>java -Xms140M -Xmx140M tune.MemoryHighDemo read to create bytes,so jvm heap will be used read to allocate & put direct bytebuffer,no jvm heap should be used ready to gc,jvm heap will be freed read to get bytes,then jvm heap will be used resultbytes[1] is: 2 read to gc all */

 

ObjectCachePool

  1. /** 
  2.  *  
  3.  */  
  4. package tune.program.memory;  
  5. import java.util.LinkedHashMap;  
  6. import java.util.Map;  
  7. import java.util.Set;  
  8. /** 
  9.  * 采用合理的缓存失效算法: FIFO、LRU、LFU等  
  10.  *  
  11.  * @author yangwm Aug 24, 2010 6:06:48 PM 
  12.  */  
  13. public class ObjectCachePool<K, V> {  
  14.     public static void main(String[] args) {  
  15.         // FIFO_POLICY  
  16.         int size = 10;  
  17.         int policy = 1;  
  18.         ObjectCachePool<Integer, Integer> objectCachePool = new ObjectCachePool<Integer, Integer>(size, policy);  
  19.         for (int i = 1; i <= 15; i++) {  
  20.             objectCachePool.put(i, i);  
  21.         }  
  22.         for (int i = 15; i >= 1; i–) {  
  23.             objectCachePool.put(i, i);  
  24.         }  
  25.         System.out.println(”size(“ + size + “), policy(“ + policy + “) FIFO ”);  
  26.         for (Map.Entry<Integer, Integer> entry : objectCachePool.entrySet()) {  
  27.             System.out.println(entry.getKey() + ”, ” + entry.getValue());  
  28.         }  
  29.           
  30.         // LRU_POLICY  
  31.         size = 10;  
  32.         policy = 2;  
  33.         objectCachePool = new ObjectCachePool<Integer, Integer>(size, policy);  
  34.         for (int i = 1; i <= 15; i++) {  
  35.             objectCachePool.put(i, i);  
  36.         }  
  37.         for (int i = 15; i >= 1; i–) {  
  38.             objectCachePool.put(i, i);  
  39.         }  
  40.         System.out.println(”size(“ + size + “), policy(“ + policy + “) LRU ”);  
  41.         for (Map.Entry<Integer, Integer> entry : objectCachePool.entrySet()) {  
  42.             System.out.println(entry.getKey() + ”, ” + entry.getValue());  
  43.         }  
  44.     }  
  45.     private static final int FIFO_POLICY = 1;  
  46.     private static final int LRU_POLICY = 2;  
  47.     private static final int DEFAULT_SIZE = 10;  
  48.     private Map<K, V> cacheObjects;  
  49.     public ObjectCachePool() {  
  50.         this(DEFAULT_SIZE);  
  51.     }  
  52.     public ObjectCachePool(int size) {  
  53.         this(size, FIFO_POLICY);  
  54.     }  
  55.     public ObjectCachePool(final int size, final int policy) {  
  56.         switch (policy) {  
  57.           
  58.         case FIFO_POLICY:  
  59.             cacheObjects = new LinkedHashMap<K, V>(size) {  
  60.                 /** 
  61.                  *  
  62.                  */  
  63.                 private static final long serialVersionUID = 1L;  
  64.                 protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {  
  65.                     return size() > size;  
  66.                 }  
  67.             };  
  68.             break;  
  69.         case LRU_POLICY:  
  70.             cacheObjects = new LinkedHashMap<K, V>(size, 0.75f, true) {  
  71.                 /** 
  72.                  *  
  73.                  */  
  74.                 private static final long serialVersionUID = 1L;  
  75.                 protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {  
  76.                     return size() > size;  
  77.                 }  
  78.             };  
  79.             break;  
  80.         default:  
  81.             throw new IllegalArgumentException(“Unknown policy: ” + policy);  
  82.         }  
  83.     }  
  84.     public void put(K key, V value) {  
  85.         cacheObjects.put(key, value);  
  86.     }  
  87.     public void get(K key) {  
  88.         cacheObjects.get(key);  
  89.     }  
  90.     public void remove(K key) {  
  91.         cacheObjects.remove(key);  
  92.     }  
  93.     public void clear() {  
  94.         cacheObjects.clear();  
  95.     }  
  96.       
  97.     public Set<Map.Entry<K, V>> entrySet() {  
  98.         return cacheObjects.entrySet();  
  99.     }  
  100. }  
  101. /* 
  102. size(10), policy(1) FIFO  
  103. 11, 11 
  104. 12, 12 
  105. 13, 13 
  106. 14, 14 
  107. 15, 15 
  108. 5, 5 
  109. 4, 4 
  110. 3, 3 
  111. 2, 2 
  112. 1, 1 
  113. size(10), policy(2) LRU  
  114. 10, 10 
  115. 9, 9 
  116. 8, 8 
  117. 7, 7 
  118. 6, 6 
  119. 5, 5 
  120. 4, 4 
  121. 3, 3 
  122. 2, 2 
  123. 1, 1 
  124. */  
/** * */ package tune.program.memory; import java.util.LinkedHashMap; import java.util.Map; import java.util.Set; /** * 采用合理的缓存失效算法: FIFO、LRU、LFU等 * * @author yangwm Aug 24, 2010 6:06:48 PM */ public class ObjectCachePool<K, V> { public static void main(String[] args) { // FIFO_POLICY int size = 10; int policy = 1; ObjectCachePool<Integer, Integer> objectCachePool = new ObjectCachePool<Integer, Integer>(size, policy); for (int i = 1; i <= 15; i++) { objectCachePool.put(i, i); } for (int i = 15; i >= 1; i–) { objectCachePool.put(i, i); } System.out.println(“size(” + size + “), policy(” + policy + “) FIFO “); for (Map.Entry<Integer, Integer> entry : objectCachePool.entrySet()) { System.out.println(entry.getKey() + “, ” + entry.getValue()); } // LRU_POLICY size = 10; policy = 2; objectCachePool = new ObjectCachePool<Integer, Integer>(size, policy); for (int i = 1; i <= 15; i++) { objectCachePool.put(i, i); } for (int i = 15; i >= 1; i–) { objectCachePool.put(i, i); } System.out.println(“size(” + size + “), policy(” + policy + “) LRU “); for (Map.Entry<Integer, Integer> entry : objectCachePool.entrySet()) { System.out.println(entry.getKey() + “, ” + entry.getValue()); } } private static final int FIFO_POLICY = 1; private static final int LRU_POLICY = 2; private static final int DEFAULT_SIZE = 10; private Map<K, V> cacheObjects; public ObjectCachePool() { this(DEFAULT_SIZE); } public ObjectCachePool(int size) { this(size, FIFO_POLICY); } public ObjectCachePool(final int size, final int policy) { switch (policy) { case FIFO_POLICY: cacheObjects = new LinkedHashMap<K, V>(size) { /** * */ private static final long serialVersionUID = 1L; protected boolean removeEldestEntry(Map.Entry<K, V> eldest) { return size() > size; } }; break; case LRU_POLICY: cacheObjects = new LinkedHashMap<K, V>(size, 0.75f, true) { /** * */ private static final long serialVersionUID = 1L; protected boolean removeEldestEntry(Map.Entry<K, V> eldest) { return size() > size; } }; break; default: throw new IllegalArgumentException(“Unknown policy: ” + policy); } } public void put(K key, V value) { cacheObjects.put(key, value); } public void get(K key) { cacheObjects.get(key); } public void remove(K key) { cacheObjects.remove(key); } public void clear() { cacheObjects.clear(); } public Set<Map.Entry<K, V>> entrySet() { return cacheObjects.entrySet(); } } /* size(10), policy(1) FIFO 11, 11 12, 12 13, 13 14, 14 15, 15 5, 5 4, 4 3, 3 2, 2 1, 1 size(10), policy(2) LRU 10, 10 9, 9 8, 8 7, 7 6, 6 5, 5 4, 4 3, 3 2, 2 1, 1 */

 

 

ObjectPoolDemo

  1. /** 
  2.  *  
  3.  */  
  4. package tune.program.memory;  
  5. import java.util.HashMap;  
  6. import java.util.Map;  
  7. import java.util.concurrent.CountDownLatch;  
  8. /** 
  9.  * 使用对象缓存池:创建对象要消耗一定的CPU以及内存,使用对象缓存池一定程度上可降低JVM堆内存的使用。 
  10.  *  
  11.  * @author yangwm Aug 24, 2010 4:34:47 PM 
  12.  */  
  13. public class ObjectPoolDemo {  
  14.     private static int executeTimes = 10;  
  15.     private static int maxFactor = 10;  
  16.     private static int threadCount = 100;  
  17.     private static final int NOTUSE_OBJECTPOOL = 1;  
  18.     private static final int USE_OBJECTPOOL = 2;  
  19.     private static int runMode =  NOTUSE_OBJECTPOOL;  
  20.     private static CountDownLatch latch = null;  
  21.     public static void main(String[] args) throws Exception {  
  22.         Task task = new Task();  
  23.         long beginTime = System.currentTimeMillis();  
  24.         for (int i = 0; i < executeTimes; i++) {  
  25.             System.out.println(”Round: ” + (i + 1));  
  26.             latch = new CountDownLatch(threadCount);  
  27.             for (int j = 0; j < threadCount; j++) {  
  28.                 new Thread(task).start();  
  29.             }  
  30.             latch.await();  
  31.         }  
  32.         long endTime = System.currentTimeMillis();  
  33.         System.out.println(”Execute summary: Round( ” + executeTimes + “ ) Thread Per Round( ” + threadCount   
  34.                 + ” ) Object Factor ( ” + maxFactor + “ ) Execute Time ( ” + (endTime - beginTime) + “ ) ms”);  
  35.     }  
  36.       
  37.     static class Task implements Runnable {  
  38.         @Override  
  39.         public void run() {  
  40.             for (int j = 0; j < maxFactor; j++) {  
  41.                 if (runMode == USE_OBJECTPOOL) {  
  42.                     BigObjectPool.getInstance().getBigObject(j);  
  43.                 } else {  
  44.                     new BigObject(j);  
  45.                 }  
  46.             }  
  47.             latch.countDown();  
  48.         }  
  49.           
  50.     }  
  51.       
  52.     static class BigObjectPool {  
  53.         private static final BigObjectPool self = new BigObjectPool();  
  54.         private final Map<Integer, BigObject> cacheObjects = new HashMap<Integer, BigObject>();  
  55.         private BigObjectPool() {  
  56.               
  57.         }  
  58.         public static BigObjectPool getInstance() {  
  59.             return self;  
  60.         }  
  61.         public BigObject getBigObject(int factor) {  
  62.             if (cacheObjects.containsKey(factor)) {  
  63.                 return cacheObjects.get(factor);  
  64.             } else {  
  65.                 BigObject object = new BigObject(factor);  
  66.                 cacheObjects.put(factor, object);  
  67.                 return object;  
  68.             }  
  69.         }  
  70.     }  
  71.       
  72.     static class BigObject {  
  73.         private byte[] bytes = null;  
  74.         public BigObject(int factor) {  
  75.             bytes = new byte[(factor + 1) * 1024 * 1024];  
  76.         }  
  77.         public byte[] getBytes() {  
  78.             return bytes;  
  79.         }  
  80.     }  
  81.       
  82. }  
  83. /* 
  84. -Xms128M -Xmx128M -Xmn64M , runMode is NOTUSE_OBJECTPOOL: 
  85. Round: 1 
  86. …… 
  87. Execute summary: Round( 10 ) Thread Per Round( 100 ) Object Factor ( 10 ) Execute Time ( 50672 ) ms 
  88. -Xms128M -Xmx128M -Xmn64M , runMode is USE_OBJECTPOOL: 
  89. Round: 1 
  90. …… 
  91. Execute summary: Round( 10 ) Thread Per Round( 100 ) Object Factor ( 10 ) Execute Time ( 344 ) ms 
  92. */  
/** * */ package tune.program.memory; import java.util.HashMap; import java.util.Map; import java.util.concurrent.CountDownLatch; /** * 使用对象缓存池:创建对象要消耗一定的CPU以及内存,使用对象缓存池一定程度上可降低JVM堆内存的使用。 * * @author yangwm Aug 24, 2010 4:34:47 PM */ public class ObjectPoolDemo { private static int executeTimes = 10; private static int maxFactor = 10; private static int threadCount = 100; private static final int NOTUSE_OBJECTPOOL = 1; private static final int USE_OBJECTPOOL = 2; private static int runMode = NOTUSE_OBJECTPOOL; private static CountDownLatch latch = null; public static void main(String[] args) throws Exception { Task task = new Task(); long beginTime = System.currentTimeMillis(); for (int i = 0; i < executeTimes; i++) { System.out.println(“Round: ” + (i + 1)); latch = new CountDownLatch(threadCount); for (int j = 0; j < threadCount; j++) { new Thread(task).start(); } latch.await(); } long endTime = System.currentTimeMillis(); System.out.println(“Execute summary: Round( ” + executeTimes + ” ) Thread Per Round( ” + threadCount + ” ) Object Factor ( ” + maxFactor + ” ) Execute Time ( ” + (endTime - beginTime) + ” ) ms”); } static class Task implements Runnable { @Override public void run() { for (int j = 0; j < maxFactor; j++) { if (runMode == USE_OBJECTPOOL) { BigObjectPool.getInstance().getBigObject(j); } else { new BigObject(j); } } latch.countDown(); } } static class BigObjectPool { private static final BigObjectPool self = new BigObjectPool(); private final Map<Integer, BigObject> cacheObjects = new HashMap<Integer, BigObject>(); private BigObjectPool() { } public static BigObjectPool getInstance() { return self; } public BigObject getBigObject(int factor) { if (cacheObjects.containsKey(factor)) { return cacheObjects.get(factor); } else { BigObject object = new BigObject(factor); cacheObjects.put(factor, object); return object; } } } static class BigObject { private byte[] bytes = null; public BigObject(int factor) { bytes = new byte[(factor + 1) * 1024 * 1024]; } public byte[] getBytes() { return bytes; } } } /* -Xms128M -Xmx128M -Xmn64M , runMode is NOTUSE_OBJECTPOOL: Round: 1 …… Execute summary: Round( 10 ) Thread Per Round( 100 ) Object Factor ( 10 ) Execute Time ( 50672 ) ms -Xms128M -Xmx128M -Xmn64M , runMode is USE_OBJECTPOOL: Round: 1 …… Execute summary: Round( 10 ) Thread Per Round( 100 ) Object Factor ( 10 ) Execute Time ( 344 ) ms */

 

ThreadLocalDemo

  1. /** 
  2.  *  
  3.  */  
  4. package tune.program.memory;  
  5. import java.util.concurrent.ExecutorService;  
  6. import java.util.concurrent.Executors;  
  7. /** 
  8.  * 释放不必要的引用:代码持有了不需要的对象引用,造成这些对象无法被GC,从而占据了JVM堆内存。 
  9.  * (使用ThreadLocal:注意在线程内动作执行完毕时,需执行 ThreadLocal.set把对象清除,避免持有不必要的对象引用) 
  10.  *  
  11.  * @author yangwm Aug 24, 2010 11:29:59 AM 
  12.  */  
  13. public class ThreadLocalDemo {  
  14.       
  15.     public static void main(String[] args) {  
  16.         ThreadLocalDemo demo = new ThreadLocalDemo();  
  17.         demo.run();  
  18.     }  
  19.       
  20.     public void run() {  
  21.         ExecutorService executor = Executors.newFixedThreadPool(1);  
  22.         executor.execute(new Task());  
  23.         System.gc();  
  24.     }  
  25.       
  26.     class Task implements Runnable {  
  27.         @Override  
  28.         public void run() {  
  29.             ThreadLocal<byte[]> localString = new ThreadLocal<byte[]>();  
  30.             localString.set(new byte[1024 * 1024 * 30]);  
  31.               
  32.             // 业务逻辑   
  33.               
  34.             //localString.set(null); // 释放不必要的引用   
  35.         }  
  36.     }  
  37. }  
/** * */ package tune.program.memory; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** * 释放不必要的引用:代码持有了不需要的对象引用,造成这些对象无法被GC,从而占据了JVM堆内存。 * (使用ThreadLocal:注意在线程内动作执行完毕时,需执行 ThreadLocal.set把对象清除,避免持有不必要的对象引用) * * @author yangwm Aug 24, 2010 11:29:59 AM */ public class ThreadLocalDemo { public static void main(String[] args) { ThreadLocalDemo demo = new ThreadLocalDemo(); demo.run(); } public void run() { ExecutorService executor = Executors.newFixedThreadPool(1); executor.execute(new Task()); System.gc(); } class Task implements Runnable { @Override public void run() { ThreadLocal<byte[]> localString = new ThreadLocal<byte[]>(); localString.set(new byte[1024 * 1024 * 30]); // 业务逻辑 //localString.set(null); // 释放不必要的引用 } } }

 

 

concurrent———————————————————————–

 

LockHotDemo

  1. /** 
  2.  *  
  3.  */  
  4. package tune.program.concurrent;  
  5. import java.util.Random;  
  6. import java.util.concurrent.CountDownLatch;  
  7. import java.util.concurrent.locks.Lock;  
  8. import java.util.concurrent.locks.ReentrantLock;  
  9. /** 
  10.  * 锁竞争的状况会比较明显,这时候线程很容易处于等待锁的状况,从而导致性能下降以及CPU sy上升 
  11.  *  
  12.  * @author yangwm Aug 24, 2010 11:59:35 PM 
  13.  */  
  14. public class LockHotDemo {  
  15.     private static int executeTimes = 10;  
  16.     private static int threadCount = Runtime.getRuntime().availableProcessors() * 100;  
  17.     private static CountDownLatch latch = null;  
  18.       
  19.     public static void main(String[] args) throws Exception {  
  20.         HandleTask task = new HandleTask();  
  21.         long beginTime = System.currentTimeMillis();  
  22.         for (int i = 0; i < executeTimes; i++) {  
  23.             System.out.println(”Round: ” + (i + 1));  
  24.             latch = new CountDownLatch(threadCount);  
  25.             for (int j = 0; j < threadCount; j++) {  
  26.                 new Thread(task).start();  
  27.             }  
  28.             latch.await();  
  29.         }  
  30.         long endTime = System.currentTimeMillis();  
  31.         System.out.println(”Execute summary: Round( ” + executeTimes + “ ) Thread Per Round( ” + threadCount   
  32.                 + ” ) Execute Time ( ” + (endTime - beginTime) + “ ) ms”);  
  33.     }  
  34.       
  35.     static class HandleTask implements Runnable {  
  36.         private final Random random = new Random();  
  37.         @Override  
  38.         public void run() {  
  39.             Handler.getInstance().handle(random.nextInt(10000));  
  40.             latch.countDown();  
  41.         }  
  42.           
  43.     }  
  44.       
  45.     static class Handler {  
  46.         private static final Handler self = new Handler();  
  47.         private final Random random = new Random();  
  48.         private final Lock lock = new ReentrantLock();  
  49.         private Handler() {  
  50.               
  51.         }  
  52.         public static Handler getInstance() {  
  53.             return self;  
  54.         }  
  55.         public void handle(int id) {  
  56.             try {  
  57.                 lock.lock();  
  58.                   
  59.                 // execute sth  
  60.                 try {  
  61.                     Thread.sleep(random.nextInt(10));  
  62.                 } catch (Exception e) {  
  63.                     e.printStackTrace();  
  64.                 }  
  65.             } finally {  
  66.                 lock.unlock();  
  67.             }  
  68.         }  
  69.     }  
  70. }  
  71. /* 
  72. Round: 1 
  73. …… 
  74. Round: 10 
  75. Execute summary: Round( 10 ) Thread Per Round( 200 ) Execute Time ( 10625 ) ms 
  76. */  
/** * */ package tune.program.concurrent; import java.util.Random; import java.util.concurrent.CountDownLatch; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; /** * 锁竞争的状况会比较明显,这时候线程很容易处于等待锁的状况,从而导致性能下降以及CPU sy上升 * * @author yangwm Aug 24, 2010 11:59:35 PM */ public class LockHotDemo { private static int executeTimes = 10; private static int threadCount = Runtime.getRuntime().availableProcessors() * 100; private static CountDownLatch latch = null; public static void main(String[] args) throws Exception { HandleTask task = new HandleTask(); long beginTime = System.currentTimeMillis(); for (int i = 0; i < executeTimes; i++) { System.out.println(“Round: ” + (i + 1)); latch = new CountDownLatch(threadCount); for (int j = 0; j < threadCount; j++) { new Thread(task).start(); } latch.await(); } long endTime = System.currentTimeMillis(); System.out.println(“Execute summary: Round( ” + executeTimes + ” ) Thread Per Round( ” + threadCount + ” ) Execute Time ( ” + (endTime - beginTime) + ” ) ms”); } static class HandleTask implements Runnable { private final Random random = new Random(); @Override public void run() { Handler.getInstance().handle(random.nextInt(10000)); latch.countDown(); } } static class Handler { private static final Handler self = new Handler(); private final Random random = new Random(); private final Lock lock = new ReentrantLock(); private Handler() { } public static Handler getInstance() { return self; } public void handle(int id) { try { lock.lock(); // execute sth try { Thread.sleep(random.nextInt(10)); } catch (Exception e) { e.printStackTrace(); } } finally { lock.unlock(); } } } } /* Round: 1 …… Round: 10 Execute summary: Round( 10 ) Thread Per Round( 200 ) Execute Time ( 10625 ) ms */

 

ReduceLockHotDemo

  1. /** 
  2.  *  
  3.  */  
  4. package tune.program.concurrent;  
  5. import java.util.Random;  
  6. import java.util.concurrent.CountDownLatch;  
  7. import java.util.concurrent.locks.Lock;  
  8. import java.util.concurrent.locks.ReentrantLock;  
  9. /** 
  10.  * 尽可能少用锁:尽可能只对需要控制的资源做加锁操作 
  11.  *  
  12.  * @author yangwm Aug 24, 2010 11:59:35 PM 
  13.  */  
  14. public class ReduceLockHotDemo {  
  15.     private static int executeTimes = 10;  
  16.     private static int threadCount = Runtime.getRuntime().availableProcessors() * 100;  
  17.     private static CountDownLatch latch = null;  
  18.       
  19.     public static void main(String[] args) throws Exception {  
  20.         HandleTask task = new HandleTask();  
  21.         long beginTime = System.currentTimeMillis();  
  22.         for (int i = 0; i < executeTimes; i++) {  
  23.             System.out.println(”Round: ” + (i + 1));  
  24.             latch = new CountDownLatch(threadCount);  
  25.             for (int j = 0; j < threadCount; j++) {  
  26.                 new Thread(task).start();  
  27.             }  
  28.             latch.await();  
  29.         }  
  30.         long endTime = System.currentTimeMillis();  
  31.         System.out.println(”Execute summary: Round( ” + executeTimes + “ ) Thread Per Round( ” + threadCount   
  32.                 + ” ) Execute Time ( ” + (endTime - beginTime) + “ ) ms”);  
  33.     }  
  34.       
  35.     static class HandleTask implements Runnable {  
  36.         private final Random random = new Random();  
  37.         @Override  
  38.         public void run() {  
  39.             Handler.getInstance().handle(random.nextInt(10000));  
  40.             latch.countDown();  
  41.         }  
  42.           
  43.     }  
  44.       
  45.     static class Handler {  
  46.         private static final Handler self = new Handler();  
  47.         private final Random random = new Random();  
  48.         private final Lock lock = new ReentrantLock();  
  49.         private Handler() {  
  50.               
  51.         }  
  52.         public static Handler getInstance() {  
  53.             return self;  
  54.         }  
  55.         public void handle(int id) {  
  56.             // execute sth don’t need lock  
  57.             try {  
  58.                 Thread.sleep(random.nextInt(5));  
  59.             } catch (Exception e) {  
  60.                 e.printStackTrace();  
  61.             }  
  62.               
  63.             try {  
  64.                 lock.lock();  
  65.                   
  66.                 // execute sth  
  67.                 try {  
  68.                     Thread.sleep(random.nextInt(5));  
  69.                 } catch (Exception e) {  
  70.                     e.printStackTrace();  
  71.                 }  
  72.             } finally {  
  73.                 lock.unlock();  
  74.             }  
  75.         }  
  76.     }  
  77. }  
  78. /* 
  79. Round: 1 
  80. …… 
  81. Round: 10 
  82. Execute summary: Round( 10 ) Thread Per Round( 200 ) Execute Time ( 5547 ) ms 
  83. */  
/** * */ package tune.program.concurrent; import java.util.Random; import java.util.concurrent.CountDownLatch; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; /** * 尽可能少用锁:尽可能只对需要控制的资源做加锁操作 * * @author yangwm Aug 24, 2010 11:59:35 PM */ public class ReduceLockHotDemo { private static int executeTimes = 10; private static int threadCount = Runtime.getRuntime().availableProcessors() * 100; private static CountDownLatch latch = null; public static void main(String[] args) throws Exception { HandleTask task = new HandleTask(); long beginTime = System.currentTimeMillis(); for (int i = 0; i < executeTimes; i++) { System.out.println(“Round: ” + (i + 1)); latch = new CountDownLatch(threadCount); for (int j = 0; j < threadCount; j++) { new Thread(task).start(); } latch.await(); } long endTime = System.currentTimeMillis(); System.out.println(“Execute summary: Round( ” + executeTimes + ” ) Thread Per Round( ” + threadCount + ” ) Execute Time ( ” + (endTime - beginTime) + ” ) ms”); } static class HandleTask implements Runnable { private final Random random = new Random(); @Override public void run() { Handler.getInstance().handle(random.nextInt(10000)); latch.countDown(); } } static class Handler { private static final Handler self = new Handler(); private final Random random = new Random(); private final Lock lock = new ReentrantLock(); private Handler() { } public static Handler getInstance() { return self; } public void handle(int id) { // execute sth don’t need lock try { Thread.sleep(random.nextInt(5)); } catch (Exception e) { e.printStackTrace(); } try { lock.lock(); // execute sth try { Thread.sleep(random.nextInt(5)); } catch (Exception e) { e.printStackTrace(); } } finally { lock.unlock(); } } } } /* Round: 1 …… Round: 10 Execute summary: Round( 10 ) Thread Per Round( 200 ) Execute Time ( 5547 ) ms */

 

SplitReduceLockHotDemo

  1. /** 
  2.  *  
  3.  */  
  4. package tune.program.concurrent;  
  5. import java.util.Random;  
  6. import java.util.concurrent.CountDownLatch;  
  7. import java.util.concurrent.locks.Lock;  
  8. import java.util.concurrent.locks.ReentrantLock;  
  9. /** 
  10.  * 尽可能少用锁:尽可能只对需要控制的资源做加锁操作 
  11.  * 拆分锁:独占锁拆分为多把锁(读写锁拆分、类似ConcurrentHashMap中默认拆分为16把锁) 
  12.  *  
  13.  * @author yangwm Aug 24, 2010 11:59:35 PM 
  14.  */  
  15. public class SplitReduceLockHotDemo {  
  16.     private static int executeTimes = 10;  
  17.     private static int threadCount = Runtime.getRuntime().availableProcessors() * 100;  
  18.     private static CountDownLatch latch = null;  
  19.       
  20.     public static void main(String[] args) throws Exception {  
  21.         HandleTask task = new HandleTask();  
  22.         long beginTime = System.currentTimeMillis();  
  23.         for (int i = 0; i < executeTimes; i++) {  
  24.             System.out.println(”Round: ” + (i + 1));  
  25.             latch = new CountDownLatch(threadCount);  
  26.             for (int j = 0; j < threadCount; j++) {  
  27.                 new Thread(task).start();  
  28.             }  
  29.             latch.await();  
  30.         }  
  31.         long endTime = System.currentTimeMillis();  
  32.         System.out.println(”Execute summary: Round( ” + executeTimes + “ ) Thread Per Round( ” + threadCount   
  33.                 + ” ) Execute Time ( ” + (endTime - beginTime) + “ ) ms”);  
  34.     }  
  35.       
  36.     static class HandleTask implements Runnable {  
  37.         private final Random random = new Random();  
  38.         @Override  
  39.         public void run() {  
  40.             Handler.getInstance().handle(random.nextInt(10000));  
  41.             latch.countDown();  
  42.         }  
  43.           
  44.     }  
  45.       
  46.     static class Handler {  
  47.         private static final Handler self = new Handler();  
  48.         private final Random random = new Random();  
  49.         private int lockCount = 10;  
  50.         private Lock[] locks = new Lock[lockCount];  
  51.         private Handler() {  
  52.             for (int i = 0; i < lockCount; i++) {  
  53.                 locks[i] = new ReentrantLock();  
  54.             }  
  55.         }  
  56.         public static Handler getInstance() {  
  57.             return self;  
  58.         }  
  59.         public void handle(int id) {  
  60.             // execute sth don’t need lock  
  61.             try {  
  62.                 Thread.sleep(random.nextInt(5));  
  63.             } catch (Exception e) {  
  64.                 e.printStackTrace();  
  65.             }  
  66.               
  67.             int mod = id % lockCount;  
  68.             try {  
  69.                 locks[mod].lock();  
  70.                   
  71.                 // execute sth  
  72.                 try {  
  73.                     Thread.sleep(random.nextInt(5));  
  74.                 } catch (Exception e) {  
  75.                     e.printStackTrace();  
  76.                 }  
  77.             } finally {  
  78.                 locks[mod].unlock();  
  79.             }  
  80.         }  
  81.     }  
  82. }  
  83. /* 
  84. Round: 1 
  85. …… 
  86. Round: 10 
  87. Execute summary: Round( 10 ) Thread Per Round( 200 ) Execute Time ( 843 ) ms 
  88. */  
/** * */ package tune.program.concurrent; import java.util.Random; import java.util.concurrent.CountDownLatch; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; /** * 尽可能少用锁:尽可能只对需要控制的资源做加锁操作 * 拆分锁:独占锁拆分为多把锁(读写锁拆分、类似ConcurrentHashMap中默认拆分为16把锁) * * @author yangwm Aug 24, 2010 11:59:35 PM */ public class SplitReduceLockHotDemo { private static int executeTimes = 10; private static int threadCount = Runtime.getRuntime().availableProcessors() * 100; private static CountDownLatch latch = null; public static void main(String[] args) throws Exception { HandleTask task = new HandleTask(); long beginTime = System.currentTimeMillis(); for (int i = 0; i < executeTimes; i++) { System.out.println(“Round: ” + (i + 1)); latch = new CountDownLatch(threadCount); for (int j = 0; j < threadCount; j++) { new Thread(task).start(); } latch.await(); } long endTime = System.currentTimeMillis(); System.out.println(“Execute summary: Round( ” + executeTimes + ” ) Thread Per Round( ” + threadCount + ” ) Execute Time ( ” + (endTime - beginTime) + ” ) ms”); } static class HandleTask implements Runnable { private final Random random = new Random(); @Override public void run() { Handler.getInstance().handle(random.nextInt(10000)); latch.countDown(); } } static class Handler { private static final Handler self = new Handler(); private final Random random = new Random(); private int lockCount = 10; private Lock[] locks = new Lock[lockCount]; private Handler() { for (int i = 0; i < lockCount; i++) { locks[i] = new ReentrantLock(); } } public static Handler getInstance() { return self; } public void handle(int id) { // execute sth don’t need lock try { Thread.sleep(random.nextInt(5)); } catch (Exception e) { e.printStackTrace(); } int mod = id % lockCount; try { locks[mod].lock(); // execute sth try { Thread.sleep(random.nextInt(5)); } catch (Exception e) { e.printStackTrace(); } } finally { locks[mod].unlock(); } } } } /* Round: 1 …… Round: 10 Execute summary: Round( 10 ) Thread Per Round( 200 ) Execute Time ( 843 ) ms */

 

 

ConcurrentStack和StackBenchmark

  1. /** 
  2.  *  
  3.  */  
  4. package tune.program.concurrent;  
  5. import java.util.concurrent.atomic.AtomicReference;  
  6. /** 
  7.  * 使用Treiber算法实现Stack:基于CAS以及AtomicReference。 
  8.  *  
  9.  * @author yangwm Aug 25, 2010 10:50:17 AM 
  10.  */  
  11. public class ConcurrentStack<E> {  
  12.       
  13.     AtomicReference<Node<E>> head = new AtomicReference<Node<E>>();  
  14.     public void push(E item) {  
  15.         Node<E> newHead = new Node<E>(item);  
  16.         Node<E> oldHead;  
  17.         do {  
  18.             oldHead = head.get();  
  19.             newHead.next = oldHead;  
  20.         } while (!head.compareAndSet(oldHead, newHead));  
  21.     }  
  22.       
  23.     public E pop() {  
  24.         Node<E> oldHead;  
  25.         Node<E> newHead;  
  26.         do {  
  27.             oldHead = head.get();  
  28.             if (oldHead == null) {  
  29.                 return null;  
  30.             }  
  31.             newHead = oldHead.next;  
  32.         } while (!head.compareAndSet(oldHead, newHead));  
  33.         return oldHead.item;  
  34.     }  
  35.       
  36.     static class Node<E> {  
  37.         final E item;  
  38.         Node<E> next;  
  39.         public Node(E item) {  
  40.             this.item = item;  
  41.         }  
  42.     }  
  43. }  
  44. /** 
  45.  *  
  46.  */  
  47. package tune.program.concurrent;  
  48. import java.util.Stack;  
  49. import java.util.concurrent.CountDownLatch;  
  50. import java.util.concurrent.CyclicBarrier;  
  51. /** 
  52.  * 基准测试:Treiber算法实现Stack、同步实现的Stack  
  53.  *  
  54.  * @author yangwm Aug 25, 2010 11:36:14 AM 
  55.  */  
  56. public class StackBenchmark {  
  57.     public static void main(String[] args) throws Exception {  
  58.         StackBenchmark stackBenchmark = new StackBenchmark();  
  59.         stackBenchmark.run();  
  60.     }  
  61.       
  62.       
  63.     private Stack<String> stack = new Stack<String>();  
  64.     private ConcurrentStack<String> concurrentStack = new ConcurrentStack<String>();  
  65.     private static final int THREAD_COUNT = 300;  
  66.     private CountDownLatch latch = new CountDownLatch(THREAD_COUNT);  
  67.     private CyclicBarrier barrier = new CyclicBarrier(THREAD_COUNT);  
  68.       
  69.     public void run() throws Exception {  
  70.         StackTask stackTask = new StackTask();  
  71.         long beginTime = System.currentTimeMillis();  
  72.         for (int i = 0; i < THREAD_COUNT; i++) {  
  73.             new Thread(stackTask).start();  
  74.         }  
  75.         latch.await();  
  76.         long endTime = System.currentTimeMillis();  
  77.         System.out.println(”Stack consume Time:  ” + (endTime - beginTime) + “ ms”);  
  78.           
  79.         latch = new CountDownLatch(THREAD_COUNT);  
  80.         barrier = new CyclicBarrier(THREAD_COUNT);  
  81.         ConcurrentStackTask concurrentStackTask = new ConcurrentStackTask();  
  82.         beginTime = System.currentTimeMillis();  
  83.         for (int i = 0; i < THREAD_COUNT; i++) {  
  84.             new Thread(concurrentStackTask).start();  
  85.         }  
  86.         latch.await();  
  87.         endTime = System.currentTimeMillis();  
  88.         System.out.println(”ConcurrentStack consume Time:  ” + (endTime - beginTime) + “ ms”);  
  89.     }  
  90.       
  91.     class StackTask implements Runnable {  
  92.           
  93.         @Override  
  94.         public void run() {  
  95.             try {  
  96.                 barrier.await();  
  97.             } catch (Exception e) {  
  98.                 e.printStackTrace();  
  99.             }  
  100.             for (int i = 0; i < 10; i++) {  
  101.                 stack.push(Thread.currentThread().getName());  
  102.                 stack.pop();  
  103.             }  
  104.             latch.countDown();  
  105.         }  
  106.           
  107.     }  
  108.     class ConcurrentStackTask implements Runnable {  
  109.           
  110.         @Override  
  111.         public void run() {  
  112.             try {  
  113.                 barrier.await();  
  114.             } catch (Exception e) {  
  115.                 e.printStackTrace();  
  116.             }  
  117.             for (int i = 0; i < 10; i++) {  
  118.                 concurrentStack.push(Thread.currentThread().getName());  
  119.                 concurrentStack.pop();  
  120.             }  
  121.             latch.countDown();  
  122.         }  
  123.           
  124.     }  
  125.       
  126. }  
  127. /* 
  128. Stack consume Time:  94 ms 
  129. ConcurrentStack consume Time:  63 ms 
  130. Stack consume Time:  78 ms 
  131. ConcurrentStack consume Time:  62 ms 
  132. */  
/** * */ package tune.program.concurrent; import java.util.concurrent.atomic.AtomicReference; /** * 使用Treiber算法实现Stack:基于CAS以及AtomicReference。 * * @author yangwm Aug 25, 2010 10:50:17 AM */ public class ConcurrentStack<E> { AtomicReference<Node<E>> head = new AtomicReference<Node<E>>(); public void push(E item) { Node<E> newHead = new Node<E>(item); Node<E> oldHead; do { oldHead = head.get(); newHead.next = oldHead; } while (!head.compareAndSet(oldHead, newHead)); } public E pop() { Node<E> oldHead; Node<E> newHead; do { oldHead = head.get(); if (oldHead == null) { return null; } newHead = oldHead.next; } while (!head.compareAndSet(oldHead, newHead)); return oldHead.item; } static class Node<E> { final E item; Node<E> next; public Node(E item) { this.item = item; } } } /** * */ package tune.program.concurrent; import java.util.Stack; import java.util.concurrent.CountDownLatch; import java.util.concurrent.CyclicBarrier; /** * 基准测试:Treiber算法实现Stack、同步实现的Stack * * @author yangwm Aug 25, 2010 11:36:14 AM */ public class StackBenchmark { public static void main(String[] args) throws Exception { StackBenchmark stackBenchmark = new StackBenchmark(); stackBenchmark.run(); } private Stack<String> stack = new Stack<String>(); private ConcurrentStack<String> concurrentStack = new ConcurrentStack<String>(); private static final int THREAD_COUNT = 300; private CountDownLatch latch = new CountDownLatch(THREAD_COUNT); private CyclicBarrier barrier = new CyclicBarrier(THREAD_COUNT); public void run() throws Exception { StackTask stackTask = new StackTask(); long beginTime = System.currentTimeMillis(); for (int i = 0; i < THREAD_COUNT; i++) { new Thread(stackTask).start(); } latch.await(); long endTime = System.currentTimeMillis(); System.out.println(“Stack consume Time: ” + (endTime - beginTime) + ” ms”); latch = new CountDownLatch(THREAD_COUNT); barrier = new CyclicBarrier(THREAD_COUNT); ConcurrentStackTask concurrentStackTask = new ConcurrentStackTask(); beginTime = System.currentTimeMillis(); for (int i = 0; i < THREAD_COUNT; i++) { new Thread(concurrentStackTask).start(); } latch.await(); endTime = System.currentTimeMillis(); System.out.println(“ConcurrentStack consume Time: ” + (endTime - beginTime) + ” ms”); } class StackTask implements Runnable { @Override public void run() { try { barrier.await(); } catch (Exception e) { e.printStackTrace(); } for (int i = 0; i < 10; i++) { stack.push(Thread.currentThread().getName()); stack.pop(); } latch.countDown(); } } class ConcurrentStackTask implements Runnable { @Override public void run() { try { barrier.await(); } catch (Exception e) { e.printStackTrace(); } for (int i = 0; i < 10; i++) { concurrentStack.push(Thread.currentThread().getName()); concurrentStack.pop(); } latch.countDown(); } } } /* Stack consume Time: 94 ms ConcurrentStack consume Time: 63 ms Stack consume Time: 78 ms ConcurrentStack consume Time: 62 ms */

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值