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) {
return (()-> {
boolean done = false;
try {
while(!done) {
done = lock.tryLock();
}
lock.lock();
map.put("key" + key, "value" + key);
} catch (Exception e) {
this.putSupplier(map, key);
} finally {
lock.unlock();
}
try {
lock.unlock();
} catch (Exception e) {} }
return null;
});
}
protected synchronized Supplier<?> getSupplier(Map<String,String> map, int key) {
return (()-> {
boolean done = false;
try {
while(!done) {
done = lock.tryLock();
}
lock.lock();
map.get("key" + key);
} catch (Exception e) {
this.getSupplier(map, key);
} finally {
lock.unlock();
}
try {
lock.unlock();
} catch (Exception e) {} }
return null;
});
}

View File

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