Search in sources :

Example 11 with StampedLock

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);
        }
    }
}
Also used : StampedLock(java.util.concurrent.locks.StampedLock)

Example 12 with StampedLock

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);
    }
}
Also used : StampedLock(java.util.concurrent.locks.StampedLock)

Example 13 with StampedLock

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);
        }
    }
}
Also used : StampedLock(java.util.concurrent.locks.StampedLock)

Example 14 with StampedLock

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);
    }
}
Also used : StampedLock(java.util.concurrent.locks.StampedLock)

Example 15 with StampedLock

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);
}
Also used : HashMap(java.util.HashMap) ExecutorService(java.util.concurrent.ExecutorService) StampedLock(java.util.concurrent.locks.StampedLock)

Aggregations

StampedLock (java.util.concurrent.locks.StampedLock)16 Lock (java.util.concurrent.locks.Lock)5 ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)5 ExecutorService (java.util.concurrent.ExecutorService)4 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 ClockProPolicy (herddb.core.ClockProPolicy)1 HttpServerExchange (io.undertow.server.HttpServerExchange)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 Future (java.util.concurrent.Future)1 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1 Phaser (java.util.concurrent.Phaser)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Consumer (java.util.function.Consumer)1 LongConsumer (java.util.function.LongConsumer)1 Test (org.junit.Test)1 Session (org.wildfly.clustering.web.session.Session)1 OOBSession (org.wildfly.clustering.web.session.oob.OOBSession)1