use of java.util.concurrent.locks.StampedLock in project LanternServer by LanternPowered.
the class ConcurrentObjectArray method work.
public <T> T work(int index, Function<O, T> function, boolean write, boolean forceReadLock) {
final StampedLock lock = this.locks[index];
if (write) {
final long stamp = lock.writeLock();
try {
return function.apply(this.objects[index]);
} finally {
lock.unlockWrite(stamp);
}
} else {
long stamp;
if (!forceReadLock) {
stamp = lock.tryOptimisticRead();
final T result = stamp == 0L ? null : function.apply(this.objects[index]);
if (lock.validate(stamp)) {
// noinspection ConstantConditions
return result;
}
}
stamp = lock.readLock();
try {
return function.apply(this.objects[index]);
} finally {
lock.unlock(stamp);
}
}
}
use of java.util.concurrent.locks.StampedLock in project LanternServer by LanternPowered.
the class ConcurrentObjectArray method set.
/**
* Sets the chunk section at the index.
*
* @param index The index in the column
* @param object The object
*/
public void set(int index, O object) {
final StampedLock lock = this.locks[index];
final long stamp = lock.writeLock();
try {
this.objects[index] = object;
} finally {
lock.unlockWrite(stamp);
}
}
use of java.util.concurrent.locks.StampedLock in project LanternServer by LanternPowered.
the class ConcurrentObjectArray method work.
/**
* Performs work on the object.
*
* @param index The index of the object
* @param consumer The consumer that will be executed
* @param write Whether you will perform write functions
* @param forceReadLock whether you want to use the lock without trying without first,
* this should be used when you are almost sure that the normal
* read will fail anyway
*/
public void work(int index, Consumer<O> consumer, boolean write, boolean forceReadLock) {
final StampedLock lock = this.locks[index];
if (write) {
final long stamp = lock.writeLock();
try {
consumer.accept(this.objects[index]);
} finally {
lock.unlockWrite(stamp);
}
} else {
long stamp;
if (!forceReadLock) {
stamp = lock.tryOptimisticRead();
if (stamp != 0L) {
consumer.accept(this.objects[index]);
}
if (lock.validate(stamp)) {
return;
}
}
stamp = lock.readLock();
try {
consumer.accept(this.objects[index]);
} finally {
lock.unlock(stamp);
}
}
}
use of java.util.concurrent.locks.StampedLock in project LanternServer by LanternPowered.
the class ConcurrentObjectArray method work.
/**
* Allows us to perform some work on the section, the function may return
* a null, this can happen if the section was empty (all air), and may return
* a new section to be set at the index.
*
* @param index the index
* @param function the section function
*/
public void work(int index, Function<O, O> function) {
final StampedLock lock = this.locks[index];
final long stamp = lock.writeLock();
try {
this.objects[index] = function.apply(this.objects[index]);
} finally {
lock.unlockWrite(stamp);
}
}
use of java.util.concurrent.locks.StampedLock in project java8-tutorial by winterbe.
the class Lock4 method main.
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(2);
Map<String, String> map = new HashMap<>();
StampedLock lock = new StampedLock();
executor.submit(() -> {
long stamp = lock.writeLock();
try {
ConcurrentUtils.sleep(1);
map.put("foo", "bar");
} finally {
lock.unlockWrite(stamp);
}
});
Runnable readTask = () -> {
long stamp = lock.readLock();
try {
System.out.println(map.get("foo"));
ConcurrentUtils.sleep(1);
} finally {
lock.unlockRead(stamp);
}
};
executor.submit(readTask);
executor.submit(readTask);
ConcurrentUtils.stop(executor);
}
Aggregations