Last active
August 29, 2015 14:21
-
-
Save vstorm83/5ff5d668d56331f18993 to your computer and use it in GitHub Desktop.
HashMap concurrent issue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.exoplatform.web.security; | |
import java.util.LinkedList; | |
import java.util.List; | |
import java.util.concurrent.CountDownLatch; | |
import java.util.concurrent.atomic.AtomicInteger; | |
import junit.framework.TestCase; | |
import org.exoplatform.services.security.ConversationState; | |
public class MyTest extends TestCase { | |
public void testHashMap() throws Exception { | |
final ConversationState state = new ConversationState(null); | |
final AtomicInteger count = new AtomicInteger(0); | |
List<Thread> threads = new LinkedList<Thread>(); | |
final CountDownLatch latch = new CountDownLatch(1); | |
for (int i = 0; i < 50; i++) { | |
final int idx = i; | |
Thread t = new Thread(new Runnable() { | |
@Override | |
public void run() { | |
try { | |
latch.await(); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
for (int j = 0; j < 100; j++) { | |
state.setAttribute("test" + idx + ":" + j, "value" + j); | |
} | |
for (int j = 0; j < 90; j++) { | |
state.removeAttribute("test" + idx + ":" + j); | |
} | |
count.addAndGet(1); | |
} | |
}); | |
threads.add(t); | |
} | |
for (Thread t : threads) { | |
t.start(); | |
} | |
latch.countDown(); | |
for (Thread t : threads) { | |
t.join(); | |
} | |
// | |
while (count.get() < 50) { | |
Thread.sleep(1000); | |
} | |
// | |
for (int i = 0; i < 50; i++) { | |
for (int j = 90; j < 100; j++) { | |
assertEquals("value" + i + ":" + j, state.getAttribute("test" + i + ":" + j)); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment