Java Code Examples for java.util.concurrent.locks.Lock#lockInterruptibly()
The following examples show how to use
java.util.concurrent.locks.Lock#lockInterruptibly() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Basic.java From dragonwell8_jdk with GNU General Public License v2.0 | 8 votes |
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(); } }}; }
Example 2
Source File: Basic.java From openjdk-jdk8u with GNU General Public License v2.0 | 6 votes |
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(); } }}; }
Example 3
Source File: Basic.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
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(); } }}; }
Example 4
Source File: Basic.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
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(); } }}; }
Example 5
Source File: Basic.java From jdk8u-jdk with GNU General Public License v2.0 | 6 votes |
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(); } }}; }
Example 6
Source File: Basic.java From hottub with GNU General Public License v2.0 | 6 votes |
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(); } }}; }
Example 7
Source File: Basic.java From hottub with GNU General Public License v2.0 | 6 votes |
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(); } }}; }
Example 8
Source File: DefaultJobManager.java From onedev with MIT License | 6 votes |
@Transactional @Override public Build submit(Project project, ObjectId commitId, String jobName, Map<String, List<String>> paramMap, SubmitReason reason) { Lock lock = LockUtils.getLock("job-manager: " + project.getId() + "-" + commitId.name()); transactionManager.mustRunAfterTransaction(new Runnable() { @Override public void run() { lock.unlock(); } }); // Lock to guarantee uniqueness of build (by project, commit, job and parameters) try { lock.lockInterruptibly(); validate(project, commitId); return submit(project, commitId, jobName, paramMap, reason, new LinkedHashSet<>()); } catch (Throwable e) { throw ExceptionUtils.unchecked(e); } }
Example 9
Source File: LockInterrupt.java From clearpool with GNU General Public License v3.0 | 6 votes |
@Test public void testLockInterrupt() { final Lock lock = new ReentrantLock(); Thread t = new Thread(new Runnable() { @Override public void run() { while (LockInterrupt.this.sign) { } try { lock.lockInterruptibly(); } catch (InterruptedException e) { System.out.println("interrupt it"); } LockInterrupt.this.sign = true; } }); t.start(); t.interrupt(); this.sign = false; while (!LockInterrupt.this.sign) { } lock.lock(); lock.unlock(); System.out.println("lock success"); }
Example 10
Source File: Basic.java From native-obfuscator with GNU General Public License v3.0 | 6 votes |
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(); } }}; }
Example 11
Source File: Basic.java From openjdk-8 with GNU General Public License v2.0 | 6 votes |
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(); } }}; }
Example 12
Source File: Basic.java From openjdk-8-source with GNU General Public License v2.0 | 6 votes |
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(); } }}; }
Example 13
Source File: Basic.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 6 votes |
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(); } }}; }
Example 14
Source File: GridH2Table.java From ignite with Apache License 2.0 | 5 votes |
/** * Acquire table lock. * * @param exclusive Exclusive flag. * @param interruptibly Acquires interruptibly lock or not interruplible lock flag. */ @SuppressWarnings({"LockAcquiredButNotSafelyReleased", "CallToThreadYield"}) private void lock(boolean exclusive, boolean interruptibly) { Lock l = exclusive ? lock.writeLock() : lock.readLock(); try { if (!exclusive) { if (interruptibly) l.lockInterruptibly(); else l.lock(); } else { for (;;) { if (l.tryLock(200, TimeUnit.MILLISECONDS)) break; else Thread.yield(); } ver.incrementAndGet(); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new IgniteInterruptedException("Thread got interrupted while trying to acquire table lock.", e); } }
Example 15
Source File: LockInterrupt.java From clearpool with GNU General Public License v3.0 | 5 votes |
private void printLockInterruptiblyTime(Lock lock) { try { long begin = System.currentTimeMillis(); for (int i = 0; i < this.count; i++) { lock.lockInterruptibly(); lock.unlock(); } System.out.println("LockInterruptiblyTime: " + (System.currentTimeMillis() - begin)); } catch (InterruptedException e) { throw new RuntimeException(e); } }
Example 16
Source File: AbstractPool.java From lams with GNU General Public License v2.0 | 4 votes |
/** * Gets connection listener instance associated with transaction. * This method is package protected beacause it is intended only for test case use. * Please don't use it in your production code. * @param trackByTransaction transaction instance * @param mcp the managed connection pool associated with the desired connection listener * @return connection listener instance * @throws ResourceException Thrown if an error occurs */ ConnectionListener getTransactionOldConnection(Transaction trackByTransaction, ManagedConnectionPool mcp) throws ResourceException { TransactionSynchronizationRegistry tsr = getTransactionSynchronizationRegistry(); Lock lock = getTSRLock(); if (lock == null) throw new ResourceException(bundle.unableObtainLock()); try { lock.lockInterruptibly(); } catch (InterruptedException ie) { Thread.interrupted(); throw new ResourceException(bundle.unableObtainLock(), ie); } try { // Already got one ConnectionListener cl = (ConnectionListener)tsr.getResource(mcp); if (cl != null) { log.tracef("Previous connection tracked by transaction=%s tx=%s", cl, trackByTransaction); return cl; } return null; } catch (Throwable t) { throw new ResourceException(bundle.unableGetConnectionListener(), t); } finally { lock.unlock(); } }
Example 17
Source File: TxConnectionListener.java From lams with GNU General Public License v2.0 | 4 votes |
/** * {@inheritDoc} */ @Override public void dissociate() throws ResourceException { log.tracef("dissociate: %s", this); try { TransactionManager tm = getConnectionManager().getTransactionIntegration().getTransactionManager(); int status = tm.getStatus(); log.tracef("dissociate: status=%s", TxUtils.getStatusAsString(status)); if (status != Status.STATUS_NO_TRANSACTION) { if (isEnlisted()) { if (doDelistResource) { Transaction tx = tm.getTransaction(); boolean delistResult = tx.delistResource(getXAResource(), XAResource.TMSUCCESS); log.tracef("dissociate: delistResult=%s", delistResult); } } else { log.tracef("dissociate: not enlisted (%s)", this); } if (isTrackByTx()) { ManagedConnectionPool mcp = getManagedConnectionPool(); TransactionSynchronizationRegistry tsr = getConnectionManager().getTransactionIntegration().getTransactionSynchronizationRegistry(); Lock lock = (Lock)tsr.getResource(LockKey.INSTANCE); if (lock != null) { try { lock.lockInterruptibly(); } catch (InterruptedException ie) { Thread.interrupted(); throw new ResourceException(bundle.unableObtainLock(), ie); } try { tsr.putResource(mcp, null); } finally { lock.unlock(); } } } } localTransaction.set(false); setTrackByTx(false); if (transactionSynchronization != null) { transactionSynchronization.cancel(); transactionSynchronization = null; } setEnlisted(false); } catch (Throwable t) { throw new ResourceException(bundle.errorInDissociate(), t); } }
Example 18
Source File: WriteCache.java From database with GNU General Public License v2.0 | 4 votes |
/** * Low-level routine copies the data from the caller's buffer into this * buffer. * * @param bin * The caller's buffer. * * @throws InterruptedException */ void copyRawBuffer(final ByteBuffer bin) throws InterruptedException { final Lock writeLock = lock.writeLock(); writeLock.lockInterruptibly(); try { final ByteBuffer buf = this.buf.get().buffer(); /* * Copy the data from the caller's buffer into our own. * * Note: We receive the buffer with pos=0, limit=#ofbyteswritten. As * a post-condition, pos will be advanced to the limit. */ buf.limit(bin.limit()); buf.position(0); buf.put(bin); // copy the caller's buffer. /* * Rebuild the record map * * Note: rebuild reads from position to limit, advancing the * position. * * Note: flush() expects pos=limit, so we are good to go after we * rebuild the record map. */ buf.position(0); // reset the position. resetRecordMapFromBuffer(buf, recordMap); buf.position(buf.limit()); } finally { writeLock.unlock(); } }
Example 19
Source File: WriteCache.java From database with GNU General Public License v2.0 | 3 votes |
/** * Return the backing {@link ByteBuffer}. The caller may read or write on * the buffer, but MUST NOT have a side effect on the * {@link ByteBuffer#position()} without first synchronizing on the * {@link ByteBuffer}. Once they are done, the caller MUST call * {@link #release()}. * <p> * Note: This uses the read lock to allow concurrent read/write operations * on the backing buffer. * <p> * Note: <strong>At most one write operation may execute concurrently in * order to avoid side effects on the buffers position when copying data * onto the buffer. This constraint must be imposed by the caller using a * <code>synchronized(buf){}</code> block during the critical sections where * the buffer position will be updated by a write. </strong> * * @return The {@link ByteBuffer}. * * @throws InterruptedException * @throws IllegalStateException * if the {@link WriteCache} is closed. */ private ByteBuffer acquire() throws InterruptedException, IllegalStateException { final Lock readLock = lock.readLock(); readLock.lockInterruptibly(); try { // latch.inc(); final IBufferAccess tmp = buf.get(); if (tmp == null) { // latch.dec(); throw new IllegalStateException(); } // Note: The ReadLock is still held! return tmp.buffer(); } catch (Throwable t) { // Release the lock only on the error path. readLock.unlock(); if (t instanceof InterruptedException) throw (InterruptedException) t; if (t instanceof IllegalStateException) throw (IllegalStateException) t; throw new RuntimeException(t); } }
Example 20
Source File: WriteCache.java From database with GNU General Public License v2.0 | 2 votes |
/** * Permanently take the {@link WriteCache} instance out of service. If the * buffer was allocated by the {@link WriteCache} then it is released back * to the {@link DirectBufferPool}. After this method is called, records can * no longer be read from nor written onto the {@link WriteCache}. It is * safe to invoke this method more than once. * <p> * Concurrent {@link #read(long, int)} requests will be serviced if the * already hold the the read lock but requests will fail once the * * @throws InterruptedException */ public void close() throws InterruptedException { final Lock writeLock = lock.writeLock(); writeLock.lockInterruptibly(); try { // // wait until there are no readers using the buffer. // latch.await(); /* * Note: This method is thread safe. Only one thread will manage to * clear the AtomicReference and it will do the rest of the work as * well. */ // position := 0; limit := capacity. final IBufferAccess tmp = buf.get(); if (tmp == null) { // Already closed. return; } if (buf.compareAndSet(tmp/* expected */, null/* update */)) { try { _resetState(tmp.buffer()); } finally { if (releaseBuffer) { tmp.release(); } } } } finally { writeLock.unlock(); } }