场景:一个类使用Jdk1.7Get()和put()是唯一被调用的java.util.HashMap方法。am试图避免get()方法上的同步。以前同步的方法ClassloaderHashMap.get()会在必须加载新类时阻塞我的所有线程数秒。类加载的本质是对象被添加到HashMap中,并且永远不会被删除。我的应用程序使用了400个线程和30'000个类。我不能使用ConcurrentHashMap。
/**
* Class to simulate lock free reads from HashMap in WebClassLoader.
*/
public static class ClassloaderHashMap {
private final HashMap<String, String> testHashMap = new HashMap<String, String>();
public String get(String key) {
if (testHashMap.containsKey(key)) {
String result = testHashMap.get(key);
if (result != null) {
return result;
}
}
// call synchronized method
return writeAndGet(key);
}
private synchronized String writeAndGet(String key) {
// find and load class by key, for the test scenario simply use value=key
testHashMap.put(key, key);
return testHashMap.get(key);
}
}问:这个解决方案有没有潜在的危险?
我用下面的代码成功地测试了一个多线程场景:
package alex;
import java.util.HashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
public class PerfTestLockFreeReadHashMap {
private static final ExecutorService EXECUTOR = Executors.newCachedThreadPool();
private static final int KEY_COUNT = 30179; // same number of loaded classes
// as in my app
private static int NUM_WRITERS = 20;
private static int NUM_READERS = 400;
private static long TEST_DURATION_MS = 1000;
private static final String[] keysArray = new String[KEY_COUNT];
static {
for (int i = 0; i < keysArray.length; i++) {
keysArray[i] = "com.company.SomeClass-" + i;
}
}
/**
* Class to simulate lock free reads from HashMap in WebClassLoader.
*/
public static class ClassloaderHashMap {
private final HashMap<String, String> testHashMap = new HashMap<String, String>();
private AtomicLong reads = new AtomicLong();
private AtomicLong nullentries = new AtomicLong();
private AtomicLong writes = new AtomicLong();
public String get(String key) {
if (testHashMap.containsKey(key)) {
reads.incrementAndGet();
String result = testHashMap.get(key);
if (result != null) {
return result;
} else {
nullentries.incrementAndGet();
}
}
// call synchronized method
return writeAndGet(key);
}
public synchronized String writeAndGet(String key) {
writes.incrementAndGet();
testHashMap.put(key, key);
return testHashMap.get(key);
}
@Override
public String toString() {
return "ClassloaderHashMap [Lock-free reads=" + reads + ", Null entries=" + nullentries + ", writes=" + writes + "]";
}
}
public static void main(final String[] args) throws Exception {
for (int i = 0; i < 10; i++) {
ClassloaderHashMap classloaderHashMap = new ClassloaderHashMap();
System.out.println("*** Run - " + i);
perfRun(classloaderHashMap);
System.out.println(classloaderHashMap);
}
EXECUTOR.shutdown();
}
public static void perfRun(final ClassloaderHashMap classloaderHashMap) throws Exception {
final CyclicBarrier startBarrier = new CyclicBarrier(NUM_READERS + NUM_WRITERS + 1);
final CountDownLatch finishLatch = new CountDownLatch(NUM_READERS + NUM_WRITERS);
final AtomicBoolean runningFlag = new AtomicBoolean(true);
for (int i = 0; i < NUM_WRITERS; i++) {
EXECUTOR.execute(new WriterRunner(classloaderHashMap, i, runningFlag, startBarrier, finishLatch));
}
for (int i = 0; i < NUM_READERS; i++) {
EXECUTOR.execute(new ReaderRunner(classloaderHashMap, i, runningFlag, startBarrier, finishLatch));
}
awaitBarrier(startBarrier);
Thread.sleep(TEST_DURATION_MS);
runningFlag.set(false);
finishLatch.await();
System.out.format("%d readers %d writers \n", NUM_READERS, NUM_WRITERS);
}
public static void awaitBarrier(final CyclicBarrier barrier) {
try {
barrier.await();
} catch (final Exception ex) {
throw new RuntimeException(ex);
}
}
public static class WriterRunner implements Runnable {
private final int id;
private final AtomicBoolean runningFlag;
private final CyclicBarrier barrier;
private final CountDownLatch latch;
private final ClassloaderHashMap classloaderHashMap;
public WriterRunner(final ClassloaderHashMap classloaderHashMap, final int id, final AtomicBoolean runningFlag, final CyclicBarrier barrier,
final CountDownLatch latch) {
this.id = id;
this.runningFlag = runningFlag;
this.barrier = barrier;
this.latch = latch;
this.classloaderHashMap = classloaderHashMap;
}
@Override
public void run() {
awaitBarrier(barrier);
int writeCounter = 0;
while (runningFlag.get()) {
String key = writeCounter + keysArray[writeCounter % KEY_COUNT] + id;
String result = classloaderHashMap.get(key);
if (result == null) {
result = classloaderHashMap.writeAndGet(key);
}
if (!key.equals(result)) {
throw new RuntimeException(String.format("Got %s instead of %s.\n", result, key));
}
++writeCounter;
}
latch.countDown();
}
}
public static class ReaderRunner implements Runnable {
private final int id;
private final AtomicBoolean runningFlag;
private final CyclicBarrier barrier;
private final CountDownLatch latch;
private final ClassloaderHashMap classloaderHashMap;
public ReaderRunner(final ClassloaderHashMap classloaderHashMap, final int id, final AtomicBoolean runningFlag, final CyclicBarrier barrier,
final CountDownLatch latch) {
this.id = id;
this.runningFlag = runningFlag;
this.barrier = barrier;
this.latch = latch;
this.classloaderHashMap = classloaderHashMap;
}
@Override
public void run() {
awaitBarrier(barrier);
int readCounter = 0;
while (runningFlag.get()) {
String key = keysArray[readCounter % keysArray.length] + "-" + id;
String result = classloaderHashMap.get(key);
if (result == null) {
result = classloaderHashMap.writeAndGet(key);
}
if (!key.equals(result)) {
throw new RuntimeException(String.format("Got %s instead of %s.\n", result, key));
}
++readCounter;
}
latch.countDown();
}
}
}示例输出-可以出现空条目,但不会导致错误,在这种情况下将调用synchronized方法:
*** Run - 0
400 readers 20 writers
ClassloaderHashMap [Lock-free reads=4288664, Null entries=0, writes=589699]
*** Run - 1
400 readers 20 writers
ClassloaderHashMap [Lock-free reads=4177513, Null entries=0, writes=965519]
*** Run - 2
400 readers 20 writers
ClassloaderHashMap [Lock-free reads=4701346, Null entries=0, writes=971986]
*** Run - 3
400 readers 20 writers
ClassloaderHashMap [Lock-free reads=8181871, Null entries=1, writes=2076311]
*** Run - 4
400 readers 20 writers
ClassloaderHashMap [Lock-free reads=3225071, Null entries=0, writes=616041]
*** Run - 5
400 readers 20 writers
ClassloaderHashMap [Lock-free reads=2923419, Null entries=0, writes=1762663]
*** Run - 6
400 readers 20 writers
ClassloaderHashMap [Lock-free reads=5514584, Null entries=0, writes=1090732]
*** Run - 7
400 readers 20 writers
ClassloaderHashMap [Lock-free reads=4037333, Null entries=0, writes=948106]
*** Run - 8
400 readers 20 writers
ClassloaderHashMap [Lock-free reads=6604630, Null entries=0, writes=750456]
*** Run - 9
400 readers 20 writers
ClassloaderHashMap [Lock-free reads=5263678, Null entries=0, writes=894637]发布于 2015-03-24 03:05:50
不,HashMap不是线程安全的。如果有一个线程正在向map写入数据,而另一个线程正在从该map中读取数据,那么读取线程可能会看到处于不一致状态的map。当然,这可能会在很长一段时间内正确运行,但随后会产生一个很难重现和找到的bug。
使用同步的get()方法,问题是对映射的所有访问都是同步的。因此,当两个线程同时尝试从map中读取数据时,其中一个线程必须等待另一个线程(尽管同时读取不是问题)。对于400个线程,这确实可能会导致考虑到的延迟。
您的问题的解决方案是使用java.util.concurrent.locks.ReadWriteLock。(Java为这个接口提供了java.util.concurrent.locks.ReentrantReadWriteLock实现。)使用这个锁,您可以确保任意数量的线程可以同时拥有对一个对象的读访问权限,但只有一个线程可以向map写入(如果一个线程正在写入,则不能有其他线程在读取)。查看Java API文档以了解如何使用这样的锁。
发布于 2015-03-24 03:02:55
是的,这种解决方案存在潜在的危险。它通过不保证“在此之前发生”原则来造成内存不一致。
即使put()方法是同步的,您的get()方法也可以返回null或old and incorrect value,或者覆盖刚刚由另一个线程通过put()放入的值(我也不知道为什么要从get()调用put()。让get()返回null)。
如果您不关心数据的准确性,那么您可以实现这一点,但这绝对不是推荐的解决方案。
发布于 2015-03-24 03:05:45
是的有。
因为你在没有同步的情况下阅读,所以你可以看到损坏的状态。实际上,您正在尝试构建“双重检查锁定”,这将不起作用,请参阅http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html
https://stackoverflow.com/questions/29217437
复制相似问题