Commits

Anonymous committed 237071b

Synchronize list access

Issue number: CACHE-246 NoSuchElementException during putInCache
Submitted by: Lars Torunski

  • Participants
  • Parent commits 6093067

Comments (0)

Files changed (1)

File src/java/com/opensymphony/oscache/base/algorithm/LRUCache.java

      * @return The key of whichever item was removed.
      */
     protected Object removeItem() {
+        Object toRemove = null;
+
         removeInProgress = true;
-
-        Object toRemove;
-
         try {
-            toRemove = removeFirst();
-        } catch (Exception e) {
-            // List is empty.
-            // this is theorically possible if we have more than the size concurrent
-            // thread in getItem. Remove completed but add not done yet.
-            // We simply wait for add to complete.
-            do {
+        	while (toRemove == null) {
                 try {
-                    Thread.sleep(5);
-                } catch (InterruptedException ie) {
+                    toRemove = removeFirst();
+                } catch (Exception e) {
+                    // List is empty.
+                    // this is theorically possible if we have more than the size concurrent
+                    // thread in getItem. Remove completed but add not done yet.
+                    // We simply wait for add to complete.
+                    do {
+                        try {
+                            Thread.sleep(5);
+                        } catch (InterruptedException ie) {
+                        }
+                    } while (list.isEmpty());
                 }
-            } while (list.size() == 0);
-
-            toRemove = removeFirst();
+        	}
+        } finally {
+            removeInProgress = false;
         }
 
-        removeInProgress = false;
-
         return toRemove;
     }
 
      * @return the object that was removed
      */
     private Object removeFirst() {
-        Iterator it = list.iterator();
-        Object toRemove = it.next();
-        it.remove();
+    	Object toRemove = null;
+    	
+    	synchronized (list) { // A further fix for CACHE-44 and CACHE-246
+        	Iterator it = list.iterator();
+        	toRemove = it.next();
+        	it.remove();
+    	}
 
         return toRemove;
     }