Search in sources :

Example 16 with StampedLock

use of java.util.concurrent.locks.StampedLock in project java8-tutorial by winterbe.

the class Lock6 method main.

public static void main(String[] args) {
    ExecutorService executor = Executors.newFixedThreadPool(2);
    StampedLock lock = new StampedLock();
    executor.submit(() -> {
        long stamp = lock.readLock();
        try {
            if (count == 0) {
                stamp = lock.tryConvertToWriteLock(stamp);
                if (stamp == 0L) {
                    System.out.println("Could not convert to write lock");
                    stamp = lock.writeLock();
                }
                count = 23;
            }
            System.out.println(count);
        } finally {
            lock.unlock(stamp);
        }
    });
    ConcurrentUtils.stop(executor);
}
Also used : 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