// test FullCache.clearCache():
Thread clearThread = new Thread() {
public void run() {
for(;;) {
cache.clearCache();
try {
Thread.sleep(DATA_OPERATION * 2);
}
catch(InterruptedException e) {
break;
}
}
}
};
// start all threads:
clearThread.start();
for(Thread t : threads) {
t.start();
}
// wait for all threads:
for(Thread t : threads) {
try {
t.join();
}
catch(InterruptedException e) {}
}
clearThread.interrupt();
try {
clearThread.join();
}
catch(InterruptedException e) {}
// statistics:
hit.debug();
}
private static void doSleep(long n) {
try {
Thread.sleep(n);
}
catch(InterruptedException e) {}
}
}
反復運行JUnit測試,均未報錯。統計結果如下:
Total get: 10000000
Not hit: 7
Hits: 99%
執行時間3.9秒。如果用synchronized取代ReadWriteLock,執行時間為204秒,可見性能差異巨大。
總結:
接口和實現的分離是必要的,否則難以實現Proxy模式。
Facade模式和DAO模式都是必要的,否則,一旦數據訪問分散在各個Servlet或JSP中,將難以控制緩存讀寫。
下載完整的源代碼
文章來源于領測軟件測試網 http://www.kjueaiud.com/