[ajax4jsf-svn-commits] JBoss Ajax4JSF SVN: r147 - in trunk/framework/src: test/java/org/ajax4jsf/cache and 1 other directory.

ajax4jsf-svn-commits at lists.jboss.org ajax4jsf-svn-commits at lists.jboss.org
Tue May 1 21:26:50 EDT 2007


Author: nbelaevski
Date: 2007-05-01 21:26:49 -0400 (Tue, 01 May 2007)
New Revision: 147

Modified:
   trunk/framework/src/main/java/org/ajax4jsf/cache/LRUMapCache.java
   trunk/framework/src/test/java/org/ajax4jsf/cache/LRUMapCacheThreadedTest.java
Log:
LRUMapCache.java modified for better strategy

Modified: trunk/framework/src/main/java/org/ajax4jsf/cache/LRUMapCache.java
===================================================================
--- trunk/framework/src/main/java/org/ajax4jsf/cache/LRUMapCache.java	2007-05-02 01:14:48 UTC (rev 146)
+++ trunk/framework/src/main/java/org/ajax4jsf/cache/LRUMapCache.java	2007-05-02 01:26:49 UTC (rev 147)
@@ -43,13 +43,13 @@
 
 	private Map futures = new HashMap();
 
-	public Object get(Object key, Object context) throws CacheException {
+	public synchronized Object get(Object key, Object context) throws CacheException {
 
 		try {
 			LRUMapFuture activeFuture = null;
 			LRUMapFuture future = null;
 
-			synchronized (this) {
+			synchronized (futures) {
 				future = (LRUMapFuture) futures.get(key);
 				if (future == null) {
 					activeFuture = new LRUMapFuture();
@@ -64,19 +64,20 @@
 					return future.getResult();
 				}
 			} else {
-				synchronized (this) {
-					synchronized (activeFuture) {
-						try {
-							if (!containsKey(key)) {
-								load(key, context);
-							}
+				try {
+					if (!containsKey(key)) {
+						load(key, context);
+					}
 
-							Object result = peek(key);
-							activeFuture.setResult(result);
+					Object result = peek(key);
+					activeFuture.setResult(result);
 
-							return result;
-						} finally {
-							this.futures.remove(key);
+					return result;
+				} finally {
+					synchronized (futures) {
+						this.futures.remove(key);
+
+						synchronized (activeFuture) {
 							activeFuture.notifyAll();
 						}
 					}

Modified: trunk/framework/src/test/java/org/ajax4jsf/cache/LRUMapCacheThreadedTest.java
===================================================================
--- trunk/framework/src/test/java/org/ajax4jsf/cache/LRUMapCacheThreadedTest.java	2007-05-02 01:14:48 UTC (rev 146)
+++ trunk/framework/src/test/java/org/ajax4jsf/cache/LRUMapCacheThreadedTest.java	2007-05-02 01:26:49 UTC (rev 147)
@@ -27,24 +27,31 @@
 	};
 	
 	private static final int COUNT = 2000;
+	private static final int PASS_COUNT = 200;
 	
-	private final Cache cache = new LRUMapCache(cacheLoader, COUNT);
 
 	public void testCache() throws Exception {
-		Thread[] threads = new Thread[COUNT];
+		long millis = System.currentTimeMillis();
 		
-		try {
+		for (int k = 0; k < PASS_COUNT; k++) {
+			Cache cache = new LRUMapCache(cacheLoader, COUNT);
+			Thread[] threads = new Thread[COUNT];
+			
+			try {
+				for (int i = 0; i < COUNT; i++) {
+					threads[i] = new TestThread(cache, new Integer(new Random().nextInt(10)));
+					threads[i].start();
+				}
+			} catch (Exception e) {
+				e.printStackTrace();
+			}
+		
 			for (int i = 0; i < COUNT; i++) {
-				threads[i] = new TestThread(cache, new Integer(new Random().nextInt(10)));
-				threads[i].start();
+				threads[i].join();
 			}
-		} catch (Exception e) {
-			e.printStackTrace();
 		}
-	
-		for (int i = 0; i < COUNT; i++) {
-			threads[i].join();
-		}
+
+		System.out.println((double) (System.currentTimeMillis() - millis) / PASS_COUNT);
 	}
 }
 




More information about the ajax4jsf-svn-commits mailing list