KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > cache4j > perfomance > test > GetPutRemoveThread


1 /* =========================================================================
2  * File: GetPutRemoveThread.java$
3  *
4  * Copyright 2006 by Yuriy Stepovoy.
5  * email: stepovoy@gmail.com
6  * All rights reserved.
7  *
8  * =========================================================================
9  */

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 JavaDoc;
17
18 /**
19  * GetPutRemoveThread
20  *
21  * @version $Revision: 1.0 $ $Date: 04/03/2003 11:00:00 $
22  * @author Yuriy Stepovoy. <a HREF="mailto:stepovoy@gmail.com">stepovoy@gmail.com</a>
23  **/

24
25 public class GetPutRemoveThread implements ITest {
26 // ----------------------------------------------------------------------------- Константы
27
// ----------------------------------------------------------------------------- Атрибуты класса
28
private ICache _cache;
29     private int _threadCount;
30 // ----------------------------------------------------------------------------- Статические переменные
31
private static final String JavaDoc NAME = "GetPutRemoveT";
32
33     private static Random JavaDoc RND = new Random JavaDoc(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 // ----------------------------------------------------------------------------- Конструкторы
43
// ----------------------------------------------------------------------------- Public методы
44

45     /**
46      * Инициализирует тест
47      * @param cache тестируемый кеш
48      * @throws Exception
49      */

50     public void init(ICache cache) throws Exception JavaDoc {
51         _cache = cache;
52         for (int i = 0; i <KEYS.length; i++) {
53             Object JavaDoc key = String.valueOf(KEYS[i]);
54             _cache.put(key, key);
55         }
56     }
57
58     /**
59      * Этот метод выполняет тестирование
60      * @throws Exception
61      */

62     public void test() throws Exception JavaDoc {
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     /**
75      * Деинициализирует тест
76      */

77     public void destroy() {
78         _cache = null;
79     }
80
81     /**
82      * Возвращает название теста
83      */

84     public String JavaDoc getTestName() {
85         return NAME;
86     }
87
88 // ----------------------------------------------------------------------------- Package scope методы
89
// ----------------------------------------------------------------------------- Protected методы
90
// ----------------------------------------------------------------------------- Private методы
91
private synchronized void incThreadCount(){
92         _threadCount++;
93     }
94     private synchronized void decThreadCount(){
95         _threadCount--;
96     }
97 // ----------------------------------------------------------------------------- Inner классы
98
private class TThread extends Thread JavaDoc {
99         private ICache _cache;
100         private long _count;
101         private Random JavaDoc _rnd = new Random JavaDoc(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 JavaDoc key = String.valueOf(_rnd.nextInt(COUNT));
116                     Object JavaDoc 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 JavaDoc e){
125                 throw new RuntimeException JavaDoc(e);
126             } finally {
127                 decThreadCount();
128             }
129         }
130     }
131 }
Popular Tags