BAEL-3855

This commit is contained in:
Unknown 2020-03-11 19:56:30 +01:00
parent 407861afac
commit e09d87a604
2 changed files with 30 additions and 26 deletions

View File

@ -14,30 +14,30 @@ public class SingleLock extends ConcurrentAccessExperiment {
protected synchronized Supplier<?> putSupplier(Map<String,String> map, int key) { protected synchronized Supplier<?> putSupplier(Map<String,String> map, int key) {
return (()-> { return (()-> {
boolean done = false;
try { try {
while(!done) { lock.lock();
done = lock.tryLock();
}
map.put("key" + key, "value" + key); map.put("key" + key, "value" + key);
} catch (Exception e) {
this.putSupplier(map, key);
} finally { } finally {
lock.unlock(); try {
} lock.unlock();
} catch (Exception e) {} }
return null; return null;
}); });
} }
protected synchronized Supplier<?> getSupplier(Map<String,String> map, int key) { protected synchronized Supplier<?> getSupplier(Map<String,String> map, int key) {
return (()-> { return (()-> {
boolean done = false;
try { try {
while(!done) { lock.lock();
done = lock.tryLock();
}
map.get("key" + key); map.get("key" + key);
} catch (Exception e) {
this.getSupplier(map, key);
} finally { } finally {
lock.unlock(); try {
} lock.unlock();
} catch (Exception e) {} }
return null; return null;
}); });
} }

View File

@ -7,23 +7,25 @@ import com.google.common.base.Supplier;
import com.google.common.util.concurrent.Striped; import com.google.common.util.concurrent.Striped;
public class StripedLock extends ConcurrentAccessExperiment { public class StripedLock extends ConcurrentAccessExperiment {
Striped<Lock> lock; Striped<Lock> stripedLock;
public StripedLock(int buckets) { public StripedLock(int buckets) {
lock = Striped.lock(buckets); stripedLock = Striped.lock(buckets);
} }
protected synchronized Supplier<?> putSupplier(Map<String,String> map, int key) { protected synchronized Supplier<?> putSupplier(Map<String,String> map, int key) {
return (()-> { return (()-> {
Lock currentLock = lock.get("key" + key); int bucket = key % stripedLock.size();
boolean done = false; Lock lock = stripedLock.get(bucket);
try { try {
while(!done) { lock.tryLock();
done = currentLock.tryLock();
}
map.put("key" + key, "value" + key); map.put("key" + key, "value" + key);
} catch (Exception e) {
this.putSupplier(map, key);
} finally { } finally {
currentLock.unlock(); try {
lock.unlock();
} catch (Exception e) {}
} }
return null; return null;
}); });
@ -31,15 +33,17 @@ public class StripedLock extends ConcurrentAccessExperiment {
protected synchronized Supplier<?> getSupplier(Map<String,String> map, int key) { protected synchronized Supplier<?> getSupplier(Map<String,String> map, int key) {
return (()-> { return (()-> {
Lock currentLock = lock.get("key" + key); int bucket = key % stripedLock.size();
boolean done = false; Lock lock = stripedLock.get(bucket);
try { try {
while(!done) { lock.tryLock();
done = currentLock.tryLock();
}
map.get("key" + key); map.get("key" + key);
} catch (Exception e) {
this.getSupplier(map, key);
} finally { } finally {
currentLock.unlock(); try {
lock.unlock();
} catch (Exception e) {}
} }
return null; return null;
}); });