本文整理匯總了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);
}