本文整理汇总了Java中java.util.concurrent.locks.StampedLock类的典型用法代码示例。如果您正苦于以下问题:Java StampedLock类的具体用法?Java StampedLock怎么用?Java StampedLock使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
StampedLock类属于java.util.concurrent.locks包,在下文中一共展示了StampedLock类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import java.util.concurrent.locks.StampedLock; //导入依赖的package包/类
public static void main(String[] args) {
Position position = new Position();
StampedLock lock = new StampedLock();
Thread threadWriter = new Thread(new Writer(position, lock));
Thread threadReader = new Thread(new Reader(position, lock));
Thread threadOptReader = new Thread(new OptimisticReader(position, lock));
threadWriter.start();
threadReader.start();
threadOptReader.start();
try {
threadWriter.join();
threadReader.join();
threadOptReader.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
示例2: main
import java.util.concurrent.locks.StampedLock; //导入依赖的package包/类
public static void main(String[] args) {
Position position=new Position();
StampedLock lock=new StampedLock();
Thread threadWriter=new Thread(new Writer(position,lock));
Thread threadReader=new Thread(new Reader(position, lock));
Thread threadOptReader=new Thread(new OptimisticReader(position, lock));
threadWriter.start();
threadReader.start();
threadOptReader.start();
try {
threadWriter.join();
threadReader.join();
threadOptReader.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
示例3: interruptibleReader
import java.util.concurrent.locks.StampedLock; //导入依赖的package包/类
static Reader interruptibleReader(final StampedLock sl,
final long timeout,
final TimeUnit unit,
final Phaser gate) {
return new Reader("InterruptibleReader") { public void run() {
if (gate != null ) toTheStartingGate(gate);
try {
if (timeout < 0)
stamp(sl.readLockInterruptibly());
else
stamp(sl.tryReadLock(timeout, unit));
check(sl.validate(stamp()));
check(sl.isReadLocked());
check(!sl.isWriteLocked());
} catch (Throwable x) { thrown(x);
} finally { if (stamp() != 0L) sl.unlockRead(stamp()); } }};
}
示例4: interruptibleReaderView
import java.util.concurrent.locks.StampedLock; //导入依赖的package包/类
static Reader interruptibleReaderView(final StampedLock sl,
final long timeout,
final TimeUnit unit,
final Phaser gate) {
return new Reader("InterruptibleReaderView") { public void run() {
if (gate != null ) toTheStartingGate(gate);
final Lock rl = sl.asReadLock();
try {
if (timeout < 0)
rl.lockInterruptibly();
else
rl.tryLock(timeout, unit);
stamp(1L); // got the lock
check(sl.isReadLocked());
check(!sl.isWriteLocked());
} catch (Throwable x) { thrown(x);
} finally { if (stamp() != 0L) rl.unlock(); } }};
}
示例5: interruptibleWriter
import java.util.concurrent.locks.StampedLock; //导入依赖的package包/类
static Writer interruptibleWriter(final StampedLock sl,
final long timeout,
final TimeUnit unit,
final Phaser gate) {
return new Writer("InterruptibleWriter") { public void run() {
if (gate != null ) toTheStartingGate(gate);
try {
if (timeout < 0)
stamp(sl.writeLockInterruptibly());
else
stamp(sl.tryWriteLock(timeout, unit));
check(sl.validate(stamp()));
check(!sl.isReadLocked());
check(sl.isWriteLocked());
} catch (Throwable x) { thrown(x);
} finally { if (stamp() != 0L) sl.unlockWrite(stamp()); } }};
}
示例6: interruptibleWriterView
import java.util.concurrent.locks.StampedLock; //导入依赖的package包/类
static Writer interruptibleWriterView(final StampedLock sl,
final long timeout,
final TimeUnit unit,
final Phaser gate) {
return new Writer("InterruptibleWriterView") { public void run() {
if (gate != null ) toTheStartingGate(gate);
Lock wl = sl.asWriteLock();
try {
if (timeout < 0)
wl.lockInterruptibly();
else
wl.tryLock(timeout, unit);
stamp(1L); // got the lock
check(!sl.isReadLocked());
check(sl.isWriteLocked());
} catch (Throwable x) { thrown(x);
} finally { if (stamp() != 0L) wl.unlock(); } }};
}
示例7: readerIterator
import java.util.concurrent.locks.StampedLock; //导入依赖的package包/类
static Iterator<Reader> readerIterator(final StampedLock sl,
final Phaser gate) {
return new Iterator<Reader>() {
int i = 0;
boolean view = false;
public boolean hasNext() { return true; }
public Reader next() {
switch ((i++)&7) {
case 1: case 4: case 7:
return reader(sl, gate, view ^= true);
case 2: case 5:
return interruptibleReader(sl, -1, SECONDS, gate, view ^= true);
default:
return interruptibleReader(sl, 30, SECONDS, gate, view ^= true); }}
public void remove() {throw new UnsupportedOperationException();}};
}
示例8: writerIterator
import java.util.concurrent.locks.StampedLock; //导入依赖的package包/类
static Iterator<Writer> writerIterator(final StampedLock sl,
final Phaser gate) {
return new Iterator<Writer>() {
int i = 0;
boolean view = false;
public boolean hasNext() { return true; }
public Writer next() {
switch ((i++)&7) {
case 1: case 4: case 7:
return writer(sl, gate, view ^= true);
case 2: case 5:
return interruptibleWriter(sl, -1, SECONDS, gate, view ^= true);
default:
return interruptibleWriter(sl, 30, SECONDS, gate, view ^= true); }}
public void remove() {throw new UnsupportedOperationException();}};
}
示例9: testWriteLock_lockUnlock
import java.util.concurrent.locks.StampedLock; //导入依赖的package包/类
/**
* write-locking, then unlocking, an unlocked lock succeed
*/
public void testWriteLock_lockUnlock() {
StampedLock lock = new StampedLock();
for (Function<StampedLock, Long> writeLocker : writeLockers())
for (BiConsumer<StampedLock, Long> writeUnlocker : writeUnlockers()) {
assertFalse(lock.isWriteLocked());
assertFalse(lock.isReadLocked());
assertEquals(0, lock.getReadLockCount());
long s = writeLocker.apply(lock);
assertValid(lock, s);
assertTrue(lock.isWriteLocked());
assertFalse(lock.isReadLocked());
assertEquals(0, lock.getReadLockCount());
writeUnlocker.accept(lock, s);
assertUnlocked(lock);
}
}
示例10: testReadLock_lockUnlock
import java.util.concurrent.locks.StampedLock; //导入依赖的package包/类
/**
* read-locking, then unlocking, an unlocked lock succeed
*/
public void testReadLock_lockUnlock() {
StampedLock lock = new StampedLock();
for (Function<StampedLock, Long> readLocker : readLockers())
for (BiConsumer<StampedLock, Long> readUnlocker : readUnlockers()) {
long s = 42;
for (int i = 0; i < 2; i++) {
s = assertValid(lock, readLocker.apply(lock));
assertFalse(lock.isWriteLocked());
assertTrue(lock.isReadLocked());
assertEquals(i + 1, lock.getReadLockCount());
}
for (int i = 0; i < 2; i++) {
assertFalse(lock.isWriteLocked());
assertTrue(lock.isReadLocked());
assertEquals(2 - i, lock.getReadLockCount());
readUnlocker.accept(lock, s);
}
assertUnlocked(lock);
}
}
示例11: testInterruptibleOperationsThrowInterruptedExceptionWriteLockedInterrupted
import java.util.concurrent.locks.StampedLock; //导入依赖的package包/类
/**
* interruptible operations throw InterruptedException when write locked and interrupted
*/
public void testInterruptibleOperationsThrowInterruptedExceptionWriteLockedInterrupted() {
final StampedLock lock = new StampedLock();
long s = lock.writeLock();
Action[] interruptibleLockBlockingActions = {
() -> lock.writeLockInterruptibly(),
() -> lock.tryWriteLock(Long.MAX_VALUE, DAYS),
() -> lock.readLockInterruptibly(),
() -> lock.tryReadLock(Long.MAX_VALUE, DAYS),
() -> lock.asWriteLock().lockInterruptibly(),
() -> lock.asWriteLock().tryLock(Long.MAX_VALUE, DAYS),
() -> lock.asReadLock().lockInterruptibly(),
() -> lock.asReadLock().tryLock(Long.MAX_VALUE, DAYS),
};
shuffle(interruptibleLockBlockingActions);
assertThrowInterruptedExceptionWhenInterrupted(interruptibleLockBlockingActions);
}
示例12: testMultipleReadLocks
import java.util.concurrent.locks.StampedLock; //导入依赖的package包/类
/**
* Multiple threads can hold a read lock when not write-locked
*/
public void testMultipleReadLocks() {
final StampedLock lock = new StampedLock();
final long s = lock.readLock();
Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
long s2 = lock.tryReadLock();
assertValid(lock, s2);
lock.unlockRead(s2);
long s3 = lock.tryReadLock(LONG_DELAY_MS, MILLISECONDS);
assertValid(lock, s3);
lock.unlockRead(s3);
long s4 = lock.readLock();
assertValid(lock, s4);
lock.unlockRead(s4);
lock.asReadLock().lock();
lock.asReadLock().unlock();
lock.asReadLock().lockInterruptibly();
lock.asReadLock().unlock();
lock.asReadLock().tryLock(Long.MIN_VALUE, DAYS);
lock.asReadLock().unlock();
}});
awaitTermination(t);
lock.unlockRead(s);
}
示例13: testWriteAfterReadLock
import java.util.concurrent.locks.StampedLock; //导入依赖的package包/类
/**
* writeLock() succeeds only after a reading thread unlocks
*/
public void testWriteAfterReadLock() throws InterruptedException {
final CountDownLatch aboutToLock = new CountDownLatch(1);
final StampedLock lock = new StampedLock();
long rs = lock.readLock();
Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() {
aboutToLock.countDown();
long s = lock.writeLock();
assertTrue(lock.isWriteLocked());
assertFalse(lock.isReadLocked());
lock.unlockWrite(s);
}});
await(aboutToLock);
assertThreadBlocks(t, Thread.State.WAITING);
assertFalse(lock.isWriteLocked());
assertTrue(lock.isReadLocked());
lock.unlockRead(rs);
awaitTermination(t);
assertUnlocked(lock);
}
示例14: testWriteAfterMultipleReadLocks
import java.util.concurrent.locks.StampedLock; //导入依赖的package包/类
/**
* writeLock() succeeds only after reading threads unlock
*/
public void testWriteAfterMultipleReadLocks() {
final StampedLock lock = new StampedLock();
long s = lock.readLock();
Thread t1 = newStartedThread(new CheckedRunnable() {
public void realRun() {
long rs = lock.readLock();
lock.unlockRead(rs);
}});
awaitTermination(t1);
Thread t2 = newStartedThread(new CheckedRunnable() {
public void realRun() {
long ws = lock.writeLock();
lock.unlockWrite(ws);
}});
assertTrue(lock.isReadLocked());
assertFalse(lock.isWriteLocked());
lock.unlockRead(s);
awaitTermination(t2);
assertUnlocked(lock);
}
示例15: testReadAfterWriteLock
import java.util.concurrent.locks.StampedLock; //导入依赖的package包/类
/**
* readLock() succeed only after a writing thread unlocks
*/
public void testReadAfterWriteLock() {
final StampedLock lock = new StampedLock();
final CountDownLatch threadsStarted = new CountDownLatch(2);
final long s = lock.writeLock();
final Runnable acquireReleaseReadLock = new CheckedRunnable() {
public void realRun() {
threadsStarted.countDown();
long rs = lock.readLock();
assertTrue(lock.isReadLocked());
assertFalse(lock.isWriteLocked());
lock.unlockRead(rs);
}};
Thread t1 = newStartedThread(acquireReleaseReadLock);
Thread t2 = newStartedThread(acquireReleaseReadLock);
await(threadsStarted);
assertThreadBlocks(t1, Thread.State.WAITING);
assertThreadBlocks(t2, Thread.State.WAITING);
assertTrue(lock.isWriteLocked());
assertFalse(lock.isReadLocked());
releaseWriteLock(lock, s);
awaitTermination(t1);
awaitTermination(t2);
assertUnlocked(lock);
}