/** * Constructs an empty BehaviorSubject. * @since 2.0 */ @SuppressWarnings("unchecked") BehaviorSubject() { this.lock = new ReentrantReadWriteLock(); this.readLock = lock.readLock(); this.writeLock = lock.writeLock(); this.subscribers = new AtomicReference<BehaviorDisposable<T>[]>(EMPTY); this.value = new AtomicReference<Object>(); this.terminalEvent = new AtomicReference<Throwable>(); }
public FileArchiverNotifierImpl( Connection conn, Configuration conf, FileSystem fs, TableName tn) { this.conn = conn; this.conf = conf; this.fs = fs; this.tn = tn; ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); readLock = lock.readLock(); writeLock = lock.writeLock(); }
private ReentrantReadWriteLock getDagLock(QueryIdentifier queryIdentifier) { lock.lock(); try { ReentrantReadWriteLock dagLock = dagSpecificLocks.get(queryIdentifier); if (dagLock == null) { dagLock = new ReentrantReadWriteLock(); dagSpecificLocks.put(queryIdentifier, dagLock); } return dagLock; } finally { lock.unlock(); } }
import java.util.*; import java.util.concurrent.locks.*; public class UpgradeTest { public static void main(String[] args) { System.out.println("read to write test"); ReadWriteLock lock = new ReentrantReadWriteLock(); lock.readLock().lock(); // get our own read lock lock.writeLock().lock(); // upgrade to write lock System.out.println("passed"); } }
int foo() { ReadWriteLock myLock = new ReentrantReadWriteLock(); myLock.writeLock().lock(); int a = 6; int b = 9; int wrongAnswer = a * b; int rightAnswer = 42; myLock.writeLock().unlock(); return wrongAnswer + rightAnswer; }
public final class MyCustomAppenderImpl extends AbstractAppender { private final ReadWriteLock rwLock = new ReentrantReadWriteLock(); private final Lock readLock = rwLock.readLock(); readLock.lock(); try { final byte[] bytes = getLayout().toByteArray(event);
class MyData { private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock(); private final Lock r = rwl.readLock(); private final Lock w = rwl.writeLock(); public Data ReadSomething(int id) { r.lock(); try { Cursor c = readableDatabase.query(...); return c.getString(0); } finally { r.unlock(); } } public void ChangeSomething(int id, int value) { w.lock(); try { writeableDatabase.update(...); } finally { w.unlock(); } } }
// For example, here is a class using a TreeMap that is expected to be // large and concurrently accessed. class RWDictionary { private final Map<String, Data> m = new TreeMap<String, Data>(); private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock(); private final Lock w = rwl.writeLock(); // other code left out for brevity public void clear() { w.lock(); // write lock try { m.clear(); } // clear the TreeMap finally { w.unlock(); } } }
/** * */ @Test public void testNotOwnedLockRelease() { ReadWriteLock lock = new ReentrantReadWriteLock(); lock.readLock().unlock(); }
ReentrantReadWriteLock.WriteLock lock = new ReentrantReadWriteLock(FAIR).writeLock();
class Store { private ReadWriteLock rwlock = new ReentrantReadWriteLock(); public void write() { rwlock.writeLock().lock(); try { write to store; } finally { rwlock.writeLock().unlock(); } } public String read() { rwlock.readLock().lock(); try { read from store; } finally { rwlock.readLock().unlock(); } } }
/** * Constructs an empty BehaviorProcessor. * @since 2.0 */ @SuppressWarnings("unchecked") BehaviorProcessor() { this.value = new AtomicReference<Object>(); this.lock = new ReentrantReadWriteLock(); this.readLock = lock.readLock(); this.writeLock = lock.writeLock(); this.subscribers = new AtomicReference<BehaviorSubscription<T>[]>(EMPTY); this.terminalEvent = new AtomicReference<Throwable>(); }
public RealtimeInvertedIndexReader() { ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock(); _readLock = readWriteLock.readLock(); _writeLock = readWriteLock.writeLock(); }
@Ignore("too slow to run every time") @Test public void testSpellCheckerFailure() throws Exception { String sampleText = createSampleText(); Language language = createLanguage(); int threadCount = Runtime.getRuntime().availableProcessors() * 10; int testRuns = 100; ReadWriteLock testWaitLock = new ReentrantReadWriteLock(); Lock testWriteLock = testWaitLock.writeLock(); testWriteLock.lock(); failedTests = 0; List<Thread> threads = new ArrayList<>(); for (int i = 0; i < threadCount; i++) { Thread t = new Thread(new TestRunner(testWaitLock, language, testRuns, sampleText)); t.start(); threads.add(t); } // Release the lock and allow all TestRunner threads to do their work. testWriteLock.unlock(); for (Thread t : threads) { t.join(); } Assert.assertEquals(0, failedTests); }
@Test public void testTryWriteLock() throws Exception { final ReadWriteLock lock = new ReentrantReadWriteLock(); lock.readLock().lock(); lock.readLock().unlock();
private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock(); private final Lock r = rwl.readLock(); private final Lock w = rwl.writeLock(); r.lock(); try { c = getReadableDatabase().query(...); w.lock(); try { getWritableDatabase().update(...); @Override public void onBegin() { w.lock();
@Override protected void lock(MailboxSession session, MailboxPath path, boolean writeLock) throws MailboxException { ReadWriteLock lock = paths.get(path); if (lock == null) { lock = new ReentrantReadWriteLock(); ReadWriteLock storedLock = paths.putIfAbsent(path, lock); if (storedLock != null) { lock = storedLock; } } getLock(lock, writeLock).lock(); }
@Override protected void setInternalRegionArguments(InternalRegionArguments ira) { // PR specific PartitionedRegion pr = mock(PartitionedRegion.class); BucketAdvisor ba = mock(BucketAdvisor.class); ReadWriteLock primaryMoveLock = new ReentrantReadWriteLock(); Lock primaryMoveReadLock = primaryMoveLock.readLock(); when(ba.getPrimaryMoveReadLock()).thenReturn(primaryMoveReadLock); when(ba.getProxyBucketRegion()).thenReturn(mock(ProxyBucketRegion.class)); when(ba.isPrimary()).thenReturn(true); ira.setPartitionedRegion(pr).setPartitionedRegionBucketRedundancy(1).setBucketAdvisor(ba); }
/** * @throws Exception If failed. */ @SuppressWarnings({"LockAcquiredButNotSafelyReleased"}) @Test public void testReadThenWriteLockAcquire() throws Exception { ReadWriteLock lock = new ReentrantReadWriteLock(); lock.readLock().lock(); lock.writeLock().lock(); }
public Memoizer() { this.map = new LinkedHashMap<K, V>(); ReadWriteLock lock = new ReentrantReadWriteLock(); this.readLock = lock.readLock(); this.writeLock = lock.writeLock(); }