Java Code Examples for java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock#lock()
The following examples show how to use
java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock#lock() .
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: StreamProcessor.java From incubator-retired-blur with Apache License 2.0 | 6 votes |
private <T> StreamWriter<T> getWriter(final ObjectOutputStream objectOutputStream) { final WriteLock writeLock = new ReentrantReadWriteLock(true).writeLock(); return new StreamWriter<T>() { @Override public void write(T obj) throws IOException { writeLock.lock(); try { objectOutputStream.writeObject(obj); } finally { writeLock.unlock(); } } @Override public void write(Iterable<T> it) throws IOException { writeLock.lock(); try { for (T t : it) { objectOutputStream.writeObject(t); } } finally { writeLock.unlock(); } } }; }
Example 2
Source File: EndpointService.java From hawkular-agent with Apache License 2.0 | 6 votes |
/** * Remove resources matching the given {@code location} and all their direct and indirect descendant resources. * * Note that this method may block if a discovery scan is currently in progress. The removal will occur when * the discovery scan finishes - only then will this method return. * * @param location a location that can contain wildcards */ public void removeResources(L location) { status.assertRunning(getClass(), "removeResources()"); try (S session = openSession()) { // we must not alter the resource manager while a discovery scan is in progress WriteLock lock = EndpointService.this.discoveryScanRWLock.writeLock(); lock.lock(); try { List<Resource<L>> removed = getResourceManager().removeResources(location, session.getLocationResolver()); inventoryListenerSupport.fireResourcesRemoved(removed); } finally { lock.unlock(); } } catch (Exception e) { LOG.errorCouldNotAccess(this, e); } }
Example 3
Source File: MapWithLock.java From t-io with Apache License 2.0 | 6 votes |
/** * 如果key值已经存在,则不会把新value put进去 * 如果key值不存在,此方法同put(key, value) * @param key * @param value * @return * @author tanyaowu */ public V putIfAbsent(K key, V value) { WriteLock writeLock = this.writeLock(); writeLock.lock(); try { Map<K, V> map = this.getObj(); V oldValue = map.putIfAbsent(key, value); if (oldValue == null) { return value; } else { return oldValue; } } catch (Throwable e) { log.error(e.getMessage(), e); } finally { writeLock.unlock(); } return null; }
Example 4
Source File: MapWithLock.java From t-io with Apache License 2.0 | 5 votes |
/** * * @param key * @param value * @return * @author tanyaowu */ public V put(K key, V value) { WriteLock writeLock = this.writeLock(); writeLock.lock(); try { Map<K, V> map = this.getObj(); return map.put(key, value); } catch (Throwable e) { log.error(e.getMessage(), e); } finally { writeLock.unlock(); } return null; }
Example 5
Source File: PoolUtils.java From commons-pool with Apache License 2.0 | 5 votes |
/** * {@inheritDoc} */ @Override public void returnObject(final T obj) { final WriteLock writeLock = readWriteLock.writeLock(); writeLock.lock(); try { pool.returnObject(obj); } catch (final Exception e) { // swallowed as of Pool 2 } finally { writeLock.unlock(); } }
Example 6
Source File: PoolUtils.java From commons-pool with Apache License 2.0 | 5 votes |
/** * {@inheritDoc} */ @Override public void addObject(final K key) throws Exception, IllegalStateException, UnsupportedOperationException { final WriteLock writeLock = readWriteLock.writeLock(); writeLock.lock(); try { keyedPool.addObject(key); } finally { writeLock.unlock(); } }
Example 7
Source File: ConcurrentCountingMap.java From BUbiNG with Apache License 2.0 | 5 votes |
/** Adds a value to the counter associated with a given key. * * @param array a byte array. * @param offset the first valid byte in {@code array}. * @param length the number of valid elements in {@code array}. * @param delta a value to be added to the counter associated with the specified key. * @return the previous value of the counter associated with the specified key. */ public int addTo(final byte[] array, final int offset, final int length, final int delta) { final long hash = MurmurHash3.hash(array, offset, length); final WriteLock writeLock = lock[(int)(hash >>> shift)].writeLock(); try { writeLock.lock(); return stripe[(int)(hash >>> shift)].addTo(array, offset, length, hash, delta); } finally { writeLock.unlock(); } }
Example 8
Source File: MapWithLock.java From t-io with Apache License 2.0 | 5 votes |
/** * * @param key * @return * @author tanyaowu */ public V remove(K key) { WriteLock writeLock = this.writeLock(); writeLock.lock(); try { Map<K, V> map = this.getObj(); return map.remove(key); } catch (Throwable e) { log.error(e.getMessage(), e); } finally { writeLock.unlock(); } return null; }
Example 9
Source File: ListWithLock.java From t-io with Apache License 2.0 | 5 votes |
/** * * * @author tanyaowu */ public void clear() { WriteLock writeLock = this.writeLock(); writeLock.lock(); try { List<T> list = this.getObj(); list.clear(); } catch (Throwable e) { log.error(e.getMessage(), e); } finally { writeLock.unlock(); } }
Example 10
Source File: PoolUtils.java From commons-pool with Apache License 2.0 | 5 votes |
/** * {@inheritDoc} */ @Override public void invalidateObject(final K key, final V obj) { final WriteLock writeLock = readWriteLock.writeLock(); writeLock.lock(); try { keyedPool.invalidateObject(key, obj); } catch (final Exception e) { // swallowed as of Pool 2 } finally { writeLock.unlock(); } }
Example 11
Source File: PoolUtils.java From Tomcat8-Source-Read with MIT License | 5 votes |
/** * {@inheritDoc} */ @Override public void addObject(final K key) throws Exception, IllegalStateException, UnsupportedOperationException { final WriteLock writeLock = readWriteLock.writeLock(); writeLock.lock(); try { keyedPool.addObject(key); } finally { writeLock.unlock(); } }
Example 12
Source File: ObjWithLock.java From t-io with Apache License 2.0 | 5 votes |
/** * 操作obj时,带上写锁 * @param writeLockHandler */ public void handle(WriteLockHandler<T> writeLockHandler) { WriteLock writeLock = lock.writeLock(); writeLock.lock(); try { writeLockHandler.handler(obj); } catch (Throwable e) { log.error(e.getMessage(), e); } finally { writeLock.unlock(); } }
Example 13
Source File: PoolUtils.java From Tomcat8-Source-Read with MIT License | 5 votes |
/** * {@inheritDoc} */ @Override public V borrowObject(final K key) throws Exception, NoSuchElementException, IllegalStateException { final WriteLock writeLock = readWriteLock.writeLock(); writeLock.lock(); try { return keyedPool.borrowObject(key); } finally { writeLock.unlock(); } }
Example 14
Source File: ADProcessInstanceController.java From metasfresh-webui-api-legacy with GNU General Public License v3.0 | 5 votes |
IAutoCloseable lockForWriting() { final WriteLock writeLock = readwriteLock.writeLock(); logger.debug("Acquiring write lock for {}: {}", this, writeLock); writeLock.lock(); logger.debug("Acquired write lock for {}: {}", this, writeLock); return () -> { writeLock.unlock(); logger.debug("Released write lock for {}: {}", this, writeLock); }; }
Example 15
Source File: PoolUtils.java From commons-pool with Apache License 2.0 | 5 votes |
/** * {@inheritDoc} */ @Override public void clear() throws Exception, UnsupportedOperationException { final WriteLock writeLock = readWriteLock.writeLock(); writeLock.lock(); try { pool.clear(); } finally { writeLock.unlock(); } }
Example 16
Source File: PoolUtils.java From Tomcat8-Source-Read with MIT License | 5 votes |
/** * {@inheritDoc} */ @Override public void returnObject(final T obj) { final WriteLock writeLock = readWriteLock.writeLock(); writeLock.lock(); try { pool.returnObject(obj); } catch (final Exception e) { // swallowed as of Pool 2 } finally { writeLock.unlock(); } }
Example 17
Source File: InMemoryCacheStatistics.java From alfresco-repository with GNU Lesser General Public License v3.0 | 4 votes |
@Override public void add(String cacheName, TransactionStats txStats) { boolean registerCacheStats = false; WriteLock writeLock = getWriteLock(cacheName); writeLock.lock(); try { // Are we adding new stats for a previously unseen cache? registerCacheStats = !cacheToStatsMap.containsKey(cacheName); if (registerCacheStats) { // There are no statistics yet for this cache. cacheToStatsMap.put(cacheName, new HashMap<OpType, OperationStats>()); } Map<OpType, OperationStats> cacheStats = cacheToStatsMap.get(cacheName); for (OpType opType : OpType.values()) { SummaryStatistics txOpSummary = txStats.getTimings(opType); long count = txOpSummary.getN(); double totalTime = txOpSummary.getSum(); OperationStats oldStats = cacheStats.get(opType); OperationStats newStats; if (oldStats == null) { newStats = new OperationStats(totalTime, count); } else { newStats = new OperationStats(oldStats, totalTime, count); } cacheStats.put(opType, newStats); } } finally { writeLock.unlock(); } if (registerCacheStats) { // We've added stats for a previously unseen cache, raise an event // so that an MBean for the cache may be registered, for example. applicationContext.publishEvent(new CacheStatisticsCreated(this, cacheName)); } }
Example 18
Source File: AprEndpoint.java From Tomcat8-Source-Read with MIT License | 4 votes |
@Override protected void doWrite(boolean block, ByteBuffer from) throws IOException { if (closed) { throw new IOException(sm.getString("socket.apr.closed", getSocket())); } Lock readLock = getBlockingStatusReadLock(); WriteLock writeLock = getBlockingStatusWriteLock(); readLock.lock(); try { if (getBlockingStatus() == block) { if (block) { Socket.timeoutSet(getSocket().longValue(), getWriteTimeout() * 1000); } doWriteInternal(from); return; } } finally { readLock.unlock(); } writeLock.lock(); try { // Set the current settings for this socket setBlockingStatus(block); if (block) { Socket.timeoutSet(getSocket().longValue(), getWriteTimeout() * 1000); } else { Socket.timeoutSet(getSocket().longValue(), 0); } // Downgrade the lock readLock.lock(); try { writeLock.unlock(); doWriteInternal(from); } finally { readLock.unlock(); } } finally { // Should have been released above but may not have been on some // exception paths if (writeLock.isHeldByCurrentThread()) { writeLock.unlock(); } } }
Example 19
Source File: AprServletOutputStream.java From Tomcat7.0.67 with Apache License 2.0 | 4 votes |
@Override protected int doWrite(boolean block, byte[] b, int off, int len) throws IOException { if (closed) { throw new IOException(sm.getString("apr.closed", Long.valueOf(socket))); } Lock readLock = wrapper.getBlockingStatusReadLock(); WriteLock writeLock = wrapper.getBlockingStatusWriteLock(); try { readLock.lock(); if (wrapper.getBlockingStatus() == block) { return doWriteInternal(b, off, len); } } finally { readLock.unlock(); } try { writeLock.lock(); // Set the current settings for this socket wrapper.setBlockingStatus(block); if (block) { Socket.timeoutSet(socket, endpoint.getSoTimeout() * 1000); } else { Socket.timeoutSet(socket, 0); } // Downgrade the lock try { readLock.lock(); writeLock.unlock(); return doWriteInternal(b, off, len); } finally { readLock.unlock(); } } finally { // Should have been released above but may not have been on some // exception paths if (writeLock.isHeldByCurrentThread()) { writeLock.unlock(); } } }
Example 20
Source File: AprServletInputStream.java From tomcatsrc with Apache License 2.0 | 4 votes |
@Override protected int doRead(boolean block, byte[] b, int off, int len) throws IOException { if (closed) { throw new IOException(sm.getString("apr.closed", Long.valueOf(socket))); } Lock readLock = wrapper.getBlockingStatusReadLock(); WriteLock writeLock = wrapper.getBlockingStatusWriteLock(); boolean readDone = false; int result = 0; try { readLock.lock(); if (wrapper.getBlockingStatus() == block) { result = Socket.recv(socket, b, off, len); readDone = true; } } finally { readLock.unlock(); } if (!readDone) { try { writeLock.lock(); wrapper.setBlockingStatus(block); // Set the current settings for this socket Socket.optSet(socket, Socket.APR_SO_NONBLOCK, (block ? 0 : 1)); // Downgrade the lock try { readLock.lock(); writeLock.unlock(); result = Socket.recv(socket, b, off, len); } finally { readLock.unlock(); } } finally { // Should have been released above but may not have been on some // exception paths if (writeLock.isHeldByCurrentThread()) { writeLock.unlock(); } } } if (result > 0) { eagain = false; return result; } else if (-result == Status.EAGAIN) { eagain = true; return 0; } else if (-result == Status.APR_EGENERAL && wrapper.isSecure()) { // Not entirely sure why this is necessary. Testing to date has not // identified any issues with this but log it so it can be tracked // if it is suspected of causing issues in the future. if (log.isDebugEnabled()) { log.debug(sm.getString("apr.read.sslGeneralError", Long.valueOf(socket), wrapper)); } eagain = true; return 0; } else if (-result == Status.APR_EOF) { throw new EOFException(sm.getString("apr.clientAbort")); } else if ((OS.IS_WIN32 || OS.IS_WIN64) && (-result == Status.APR_OS_START_SYSERR + 10053)) { // 10053 on Windows is connection aborted throw new EOFException(sm.getString("apr.clientAbort")); } else { throw new IOException(sm.getString("apr.read.error", Integer.valueOf(-result), Long.valueOf(socket), wrapper)); } }