1 10 11 package net.sf.cache4j.perfomance.test; 12 13 import net.sf.cache4j.perfomance.ITest; 14 import net.sf.cache4j.perfomance.ICache; 15 16 import java.util.Random ; 17 18 24 25 public class GetPutRemoveThread implements ITest { 26 private ICache _cache; 29 private int _threadCount; 30 private static final String NAME = "GetPutRemoveT"; 32 33 private static Random RND = new Random (System.currentTimeMillis()); 34 private static int COUNT = 2000; 35 private static int[] KEYS = new int[COUNT]; 36 static{ 37 for (int i = 0; i <COUNT; i++) { 38 KEYS[i] = RND.nextInt(COUNT); 39 } 40 } 41 42 45 50 public void init(ICache cache) throws Exception { 51 _cache = cache; 52 for (int i = 0; i <KEYS.length; i++) { 53 Object key = String.valueOf(KEYS[i]); 54 _cache.put(key, key); 55 } 56 } 57 58 62 public void test() throws Exception { 63 int tcount = 10; 64 int count = 100000; 65 for (int i = 0; i <tcount; i++) { 66 new TThread(_cache, count).start(); 67 } 68 69 while(_threadCount!=0){ 70 Thread.currentThread().sleep(1); 71 } 72 } 73 74 77 public void destroy() { 78 _cache = null; 79 } 80 81 84 public String getTestName() { 85 return NAME; 86 } 87 88 private synchronized void incThreadCount(){ 92 _threadCount++; 93 } 94 private synchronized void decThreadCount(){ 95 _threadCount--; 96 } 97 private class TThread extends Thread { 99 private ICache _cache; 100 private long _count; 101 private Random _rnd = new Random (this.hashCode()); 102 public TThread(ICache cache, long count) { 103 incThreadCount(); 104 _cache = cache; 105 _count = count; 106 } 107 108 public void run() { 109 try { 110 111 long count = 0; 112 while(count<_count){ 113 count++; 114 115 Object key = String.valueOf(_rnd.nextInt(COUNT)); 116 Object obj = _cache.get(key); 117 if(obj==null){ 118 _cache.put(key, key); 119 } else { 120 _cache.remove(key); 121 } 122 123 } 124 } catch (Exception e){ 125 throw new RuntimeException (e); 126 } finally { 127 decThreadCount(); 128 } 129 } 130 } 131 } | Popular Tags |