/** * removeJobTasks: Cleanup mapTasks and reduceTasks for this job * * @param jobID */ private void removeJobTasks(int jobID) { System.out.println( communicator.getLocalHostName() + " proceeding to remove map tasks for job " + jobID); mapLock.lock(); Iterator<MapTask> itr = mapTasks.iterator(); while (itr.hasNext()) { MapTask task = itr.next(); if (task.getJobConf().getJobID() == jobID) { itr.remove(); } } mapLock.unlock(); System.out.println( communicator.getLocalHostName() + " proceeding to remove reduce tasks for job " + jobID); reduceLock.lock(); Iterator<ReduceTask> itr1 = reduceTasks.iterator(); while (itr1.hasNext()) { ReduceTask task = itr1.next(); if (task.getJobConf().getJobID() == jobID) { itr1.remove(); } } reduceLock.unlock(); }
public final V get(K key) { if (key == null) { throw new NullPointerException("key == null"); } // check to see if we already have a value readLock.lock(); try { V value = map.get(key); if (value != null) { return value; } } finally { readLock.unlock(); } // create a new value. this may race and we might create more than one instance, but that's ok V newValue = create(key); if (newValue == null) { throw new NullPointerException("create returned null"); } // write the new value and return it writeLock.lock(); try { map.put(key, newValue); return newValue; } finally { writeLock.unlock(); } }
/** {@inheritDoc } */ public List<Delta> getDeltas() { readLock.lock(); try { List<Delta> cp = readCopy; if (cp != null) { return cp; } } finally { readLock.unlock(); } /* * Double-check: was a deprecated practice before Java 5. * Is okay since Java 5 provided that the readCopy field * is protected by the readlock. */ writeLock.lock(); try { List<Delta> cp = readCopy; if (cp == null) { cp = UnmodifiableArrayList.wrap(deltas.toArray(new Delta[deltas.size()])); readCopy = cp; } return cp; } finally { writeLock.unlock(); } }
/** * Method that prints the Job. The printing is divided in two phase two show how the fairness * attribute affects the election of the thread who has the control of the lock * * @param document The document to print */ public void printJob(Object document) { queueLock.lock(); try { Long duration = (long) (Math.random() * 10000); System.out.printf( "%s: PrintQueue: Printing a Job during %d seconds\n", Thread.currentThread().getName(), (duration / 1000)); Thread.sleep(duration); } catch (InterruptedException e) { e.printStackTrace(); } finally { queueLock.unlock(); } queueLock.lock(); try { Long duration = (long) (Math.random() * 10000); System.out.printf( "%s: PrintQueue: Printing a Job during %d seconds\n", Thread.currentThread().getName(), (duration / 1000)); Thread.sleep(duration); } catch (InterruptedException e) { e.printStackTrace(); } finally { queueLock.unlock(); } }
public List<State> getBlue(Graph graph, State state) { List<State> result = graph.post(state); try { blueLock.lock(); if (!blueMap.containsKey(state) || blueMap.get(state).size() == 0) { ArrayList<State> succesors = (ArrayList<State>) graph.post(state); ArrayList<ArrayList<State>> permutations = new ArrayList<ArrayList<State>>(); if (succesors.size() > 1) { permute(succesors, 0, succesors.size() - 1, permutations); } else { permutations.add(succesors); } blueMap.put(state, permutations); } } finally { blueLock.unlock(); } try { blueLock.lock(); result = (List<State>) blueMap.get(state).get(blueMap.get(state).size() - 1); } finally { blueLock.unlock(); } return result; }
@Override public boolean containsAll(Collection<?> arg0) { if (containsCounter.incrementAndGet() >= maxContains && !copyDone.get()) { try { writeLock.lock(); // many calls to contains, inefficient if the delegate // is not a set // copyDone is doublechecked, but here it's protected by // the write // lock as in all other instances in which its value is // changed if (!(delegate instanceof Set)) { if (!copyDone.getAndSet(true)) { delegate = new SyncSet<T>(delegate); } } // skip the second portion of the method: no need to // reacquire // the lock, it's already a write lock return delegate.containsAll(arg0); } finally { writeLock.unlock(); } } try { readLock.lock(); return delegate.containsAll(arg0); } finally { readLock.unlock(); } }
/** * Calls shutdown on current state and waits until a finished state is reached. * * @throws InterruptedException if the waiting for a finished state is interrupted. */ public void shutdown() throws InterruptedException { stateLock.lock(); try { isShutdown = true; // interrupts any take operations takeCallLock.lock(); try { takeCallCondition.signalAll(); } finally { takeCallLock.unlock(); } currentServerState.shutdown(); while (true) { if (currentServerState.getCurrentState() == ServerStates.Finished) { break; } if (currentServerState.getCurrentState() == ServerStates.Error) { break; } stateChangeCondition.await(); } callMapCleanupTimer.cancel(); callMapCleanupTask.cancel(); stateTimeoutTimer.cancel(); } catch (InterruptedException e) { throw e; } finally { stateLock.unlock(); } }
public boolean set(int option, int optval) { if (option == ZMQ.ZMQ_MAX_SOCKETS && optval >= 1) { optSync.lock(); try { maxSockets = optval; } finally { optSync.unlock(); } } else if (option == ZMQ.ZMQ_IO_THREADS && optval >= 0) { optSync.lock(); try { ioThreadCount = optval; } finally { optSync.unlock(); } } else if (option == ZMQ.ZMQ_BLOCKY && optval >= 0) { optSync.lock(); try { blocky = (optval != 0); } finally { optSync.unlock(); } } else { return false; } return true; }
/* * Inicia a realização de uma Tarefa. * Retorna o ID se foi iniciada com sucesso -1 caso contrário */ public int beginTask(String type) { Map<String, Integer> objectsToConsume; lock.lock(); try { if (!this.tasks.containsKey(type)) return -1; else { objectsToConsume = this.tasks.get(type).getObjects(); } } finally { lock.unlock(); } try { warehouse.consume(objectsToConsume); } catch (Exception e) { return -1; } lock.lock(); try { countID++; this.tasksRunning.put(countID, type); return countID; // Retornar o ID da tarefa } finally { lock.unlock(); } }
/* * Termina a realização da Tarefa. * Retorna true se foi terminada com sucesso false caso contrário */ public boolean endTask(String id) throws InterruptedException { Map<String, Integer> objectsToSupply; String type; lock.lock(); try { if (!this.tasksRunning.containsKey(Integer.valueOf(id))) { return false; } else { type = this.tasksRunning.get(Integer.valueOf(id)); objectsToSupply = this.tasks.get(type).getObjects(); } } finally { lock.unlock(); } // Supply de todos os objetos for (Map.Entry<String, Integer> entry : objectsToSupply.entrySet()) { warehouse.supply(entry.getKey(), entry.getValue()); } lock.lock(); try { this.tasksRunning.remove(Integer.valueOf(id)); this.tasks.get(type).signalP(); } finally { lock.unlock(); } return true; }
/** * Aborts a temp block. * * @param sessionId the id of session * @param blockId the id of block * @throws BlockDoesNotExistException if block id can not be found in temporary blocks * @throws BlockAlreadyExistsException if block id already exists in committed blocks * @throws InvalidWorkerStateException if block id is not owned by session id * @throws IOException if I/O errors occur when deleting the block file */ private void abortBlockInternal(long sessionId, long blockId) throws BlockDoesNotExistException, BlockAlreadyExistsException, InvalidWorkerStateException, IOException { long lockId = mLockManager.lockBlock(sessionId, blockId, BlockLockType.WRITE); try { String path; TempBlockMeta tempBlockMeta; mMetadataReadLock.lock(); try { checkTempBlockOwnedBySession(sessionId, blockId); tempBlockMeta = mMetaManager.getTempBlockMeta(blockId); path = tempBlockMeta.getPath(); } finally { mMetadataReadLock.unlock(); } // Heavy IO is guarded by block lock but not metadata lock. This may throw IOException. Files.delete(Paths.get(path)); mMetadataWriteLock.lock(); try { mMetaManager.abortTempBlockMeta(tempBlockMeta); } catch (BlockDoesNotExistException e) { throw Throwables.propagate(e); // We shall never reach here } finally { mMetadataWriteLock.unlock(); } } finally { mLockManager.unlockBlock(lockId); } }
@Test(timeout = 1500l) public void testReentrancy() throws Exception { final CountDownLatch latch = new CountDownLatch(1); Lock firstLock = new ReentrantZkLock(baseLockPath, zkSessionManager); firstLock.lock(); try { testService.submit( new Runnable() { @Override public void run() { Lock secondLock = new ReentrantZkLock(baseLockPath, zkSessionManager); secondLock.lock(); try { latch.countDown(); } finally { secondLock.unlock(); } } }); boolean nowAcquired = latch.await(500, TimeUnit.MILLISECONDS); assertTrue("The Second lock was acquired before the first lock was released!", !nowAcquired); // this should be fine firstLock.lock(); System.out.println("Lock acquired twice!"); firstLock.unlock(); // should still be locked nowAcquired = latch.await(500, TimeUnit.MILLISECONDS); assertTrue( "The Second lock was acquired before the first lock was released twice!", !nowAcquired); } finally { firstLock.unlock(); } }
@Override public void handleEvents(com.sun.jdi.event.EventSet eventSet) throws DebuggerException { boolean resume = true; try { for (com.sun.jdi.event.Event event : eventSet) { LOG.debug("New event: {}", event); if (event instanceof com.sun.jdi.event.BreakpointEvent) { lock.lock(); try { resume = processBreakPointEvent((com.sun.jdi.event.BreakpointEvent) event); } finally { lock.unlock(); } } else if (event instanceof com.sun.jdi.event.StepEvent) { lock.lock(); try { resume = processStepEvent((com.sun.jdi.event.StepEvent) event); } finally { lock.unlock(); } } else if (event instanceof com.sun.jdi.event.VMDisconnectEvent) { resume = processDisconnectEvent(); } else if (event instanceof com.sun.jdi.event.ClassPrepareEvent) { resume = processClassPrepareEvent((com.sun.jdi.event.ClassPrepareEvent) event); } } } finally { if (resume) { eventSet.resume(); } } }
public void finish() { runningLock.lock(); running = false; runningLock.unlock(); waitingLock.lock(); waitingCondition.signal(); waitingLock.unlock(); }
public static List<Server> getServers(int num) { writelock.lock(); Collections.shuffle(servers, r); writelock.unlock(); readlock.lock(); List<Server> ss = new ArrayList<Server>(); ss.addAll(servers.subList(0, num)); readlock.unlock(); return ss; }
public void parse(File scriptName, InputStream inputStream) { mStreamLock.lock(); mNext = inputStream; mStreamLock.unlock(); mStatusLock.lock(); mScriptName = scriptName; mParse = true; mAbort = true; mStatusLock.unlock(); }
@Override // 刷新Solr集群状态的Scheduled public void run() { JsonArray stateArray = SolrTools.getClusterState(_solrServerUrls, _coreName, _connectTimeout, _readTimeout); if (stateArray == null) { _logger.log( Level.WARNING, "can not connect Solr Cloud:" + "coreName:" + _coreName + ",URLS:" + _solrServerUrls); return; } if (_stateArray.encode().equals(stateArray.encode())) { return; } _stateArray = stateArray; java.util.List<String> newUrlGets = new java.util.ArrayList<String>(stateArray.size()); java.util.List<String> newUrlUpdates = new java.util.ArrayList<String>(stateArray.size()); java.util.List<String> newUrlSelects = new java.util.ArrayList<String>(stateArray.size()); for (int i = 0; i < stateArray.size(); i++) { JsonObject jj = stateArray.<JsonObject>get(i); if (jj.getString("state").equalsIgnoreCase("active") || jj.getString("state").equalsIgnoreCase("recovering")) { newUrlGets.add(jj.getString("base_url") + "/" + _coreName + "/get?id="); newUrlUpdates.add(jj.getString("base_url") + "/" + _coreName + "/update"); newUrlSelects.add(jj.getString("base_url") + "/" + _coreName + "/select"); } } _lockGet.lock(); try { this._urlGets.clear(); this._urlGets = newUrlGets; } finally { _lockGet.unlock(); } _lockPost.lock(); try { this._urlUpdates.clear(); this._urlUpdates = newUrlUpdates; } finally { _lockPost.unlock(); } _lockSelect.lock(); try { this._urlSelects.clear(); this._urlSelects = newUrlSelects; } finally { _lockSelect.unlock(); } }
/** @throws Exception If test fails. */ public void testMultiNodeLock() throws Exception { IgniteCache<Integer, String> cache1 = ignite1.cache(null); IgniteCache<Integer, String> cache2 = ignite2.cache(null); Lock lock1_1 = cache1.lock(1); Lock lock2_1 = cache2.lock(1); lock1_1.lock(); try { assert cache1.isLocalLocked(1, false) : entries(1); assert cache1.isLocalLocked(1, true); assert cache2.isLocalLocked(1, false) : entries(1); assert !cache2.isLocalLocked(1, true); assert !lock2_1.tryLock(); assert cache2.isLocalLocked(1, false) : entries(1); assert !cache2.isLocalLocked(1, true); } finally { lock1_1.unlock(); checkUnlocked(cache1, 1); } CountDownLatch latch = new CountDownLatch(1); lock2_1.lock(); try { assert cache2.isLocalLocked(1, false) : entries(1); assert cache2.isLocalLocked(1, true); assert cache1.isLocalLocked(1, false) : entries(1); assert !cache1.isLocalLocked(1, true); addListener(ignite1, new UnlockListener(latch, 1)); assert !lock1_1.tryLock(); assert cache1.isLocalLocked(1, false) : entries(1); assert !cache1.isLocalLocked(1, true); } finally { lock2_1.unlock(); } latch.await(); checkUnlocked(cache1, 1); checkUnlocked(cache2, 1); }
@Override public JDialog call() throws IBControllerException, InterruptedException { final JFrame mainForm = MainWindowManager.mainWindowManager().getMainWindow(); if (isGateway) { /* * For the gateway, the main form is loaded right at the start, and long before * the menu items become responsive: any attempt to access the Configure > Settings * menu item (even after it has been enabled) results in an exception being logged * by TWS. * * It's not obvious how long we need to wait before the menu becomes responsive. However the splash * frame that appears in front of the gateway main window during initialisation disappears when everything * is ready, and it's close can be detected as a frame entitled 'Starting application...' and a Closed event. * * So we wait for the handler for that frame to call setSplashScreenClosed(). * */ lock.lock(); try { while (!mGatewayInitialised) { gatewayInitialised.await(); } } finally { lock.unlock(); } } if (isGateway) { if (!Utils.invokeMenuItem(mainForm, new String[] {"Configure", "Settings"})) throw new IBControllerException("'Configure > Settings' menu item"); } else if (Utils.invokeMenuItem( mainForm, new String[] {"Edit", "Global Configuration..."})) /* TWS's Classic layout */ { } else if (Utils.invokeMenuItem( mainForm, new String[] {"File", "Global Configuration..."})) /* TWS's Mosaic layout */ { } else { throw new IBControllerException( "'Edit > Global Configuration' or 'File > Global Configuration' menu items"); } lock.lock(); try { while (mConfigDialog == null) { gotConfigDialog.await(); } } finally { lock.unlock(); } return mConfigDialog; }
public void secondThread() throws InterruptedException { Random random = new Random(); for (int i = 0; i < 10_000; i++) { lock1.lock(); lock2.lock(); try { Account.transfer(acc2, acc1, random.nextInt(100)); } finally { lock1.unlock(); lock2.unlock(); } } }
void doRemoveAddress(Address deadAddress, boolean destroyConnection) { if (!ensureMemberIsRemovable(deadAddress)) { return; } lock.lock(); try { if (deadAddress.equals(node.getMasterAddress())) { assignNewMaster(); } if (node.isMaster()) { clusterJoinManager.removeJoin(new MemberInfo(deadAddress)); } Connection conn = node.connectionManager.getConnection(deadAddress); if (destroyConnection && conn != null) { node.connectionManager.destroyConnection(conn); } MemberImpl deadMember = getMember(deadAddress); if (deadMember != null) { removeMember(deadMember); logger.info(membersString()); } } finally { lock.unlock(); } }
/** * This method calculates the minimum view ID known by the current node. This method is only used * in a clustered cache, and only invoked when either a view change is detected, or a transaction * whose view ID is not the same as the current view ID. * * <p>This method is guarded by minViewRecalculationLock to prevent concurrent updates to the * minimum view ID field. * * @param idOfRemovedTransaction the view ID associated with the transaction that triggered this * recalculation, or -1 if triggered by a view change event. */ @GuardedBy("minViewRecalculationLock") private void calculateMinViewId(int idOfRemovedTransaction) { minViewRecalculationLock.lock(); try { // We should only need to re-calculate the minimum view ID if the transaction being completed // has the same ID as the smallest known transaction ID, to check what the new smallest is. // We do this check // again here, since this is now within a synchronized method. if (idOfRemovedTransaction == -1 || (idOfRemovedTransaction == minTxViewId && idOfRemovedTransaction < currentViewId)) { int minViewIdFound = currentViewId; for (CacheTransaction ct : localTransactions.values()) { int viewId = ct.getViewId(); if (viewId < minViewIdFound) minViewIdFound = viewId; } for (CacheTransaction ct : remoteTransactions.values()) { int viewId = ct.getViewId(); if (viewId < minViewIdFound) minViewIdFound = viewId; } if (minViewIdFound > minTxViewId) { log.tracef("Changing minimum view ID from %s to %s", minTxViewId, minViewIdFound); minTxViewId = minViewIdFound; } else { log.tracef("Minimum view ID still is %s; nothing to change", minViewIdFound); } } } finally { minViewRecalculationLock.unlock(); } }
protected void forwardToCoord(long seqno, Message msg) { if (is_coord) { forward(msg, seqno, false); return; } if (!running || flushing) { forward_table.put(seqno, msg); return; } if (!ack_mode) { forward_table.put(seqno, msg); forward(msg, seqno, false); return; } send_lock.lock(); try { forward_table.put(seqno, msg); while (running && !flushing) { ack_promise.reset(); forward(msg, seqno, true); if (!ack_mode || !running || flushing) break; Long ack = ack_promise.getResult(500); if ((Objects.equals(ack, seqno)) || !forward_table.containsKey(seqno)) break; } } finally { send_lock.unlock(); } }
public static List<Server> getServers() { readlock.lock(); @SuppressWarnings("unchecked") ArrayList<Server> ss = (ArrayList<Server>) ((ArrayList<Server>) servers).clone(); readlock.unlock(); return ss; }
private void onFinishCommand() { lock.lock(); try { String execution = currentCommandExecution; LOGGER.debug("onFinishCommand() called while execution = {}", execution); currentCommandExecution = null; onDisconnect = null; updateActivityTimestamp(); switch (state) { case Running: try { onFinishCommand.run(); condition.signalAll(); } catch (Throwable throwable) { setState(State.Broken); throw UncheckedException.throwAsUncheckedException(throwable); } break; case StopRequested: stop(); break; case Stopped: break; default: throw new IllegalStateException("Daemon is in unexpected state: " + state); } } finally { lock.unlock(); } }
private void onStartCommand(String commandDisplayName, Runnable onDisconnect) { lock.lock(); try { switch (state) { case Broken: throw new DaemonUnavailableException("This daemon is in a broken state and will stop."); case StopRequested: throw new DaemonUnavailableException("This daemon is currently stopping."); case Stopped: throw new DaemonUnavailableException("This daemon has stopped."); } if (currentCommandExecution != null) { throw new DaemonUnavailableException( String.format("This daemon is currently executing: %s", currentCommandExecution)); } LOGGER.debug( "onStartCommand({}) called after {} minutes of idle", commandDisplayName, getIdleMinutes()); try { onStartCommand.run(); currentCommandExecution = commandDisplayName; this.onDisconnect = onDisconnect; updateActivityTimestamp(); condition.signalAll(); } catch (Throwable throwable) { setState(State.Broken); throw UncheckedException.throwAsUncheckedException(throwable); } } finally { lock.unlock(); } }
/** * getReduceLoad: send current number of reduce tasks running on the node * * @return */ public int getReduceLoad() { int size; reduceLock.lock(); size = reduceTasks.size(); reduceLock.unlock(); return (size); }
/** * getMapLoad: Send current number of map tasks running on the node * * @return */ public int getMapLoad() { int size; mapLock.lock(); size = mapTasks.size(); mapLock.unlock(); return (size); }
public <K, V> MultiProcessSafePersistentIndexedCache<K, V> newCache( final PersistentIndexedCacheParameters<K, V> parameters) { Factory<BTreePersistentIndexedCache<K, V>> indexedCacheFactory = new Factory<BTreePersistentIndexedCache<K, V>>() { public BTreePersistentIndexedCache<K, V> create() { return doCreateCache( parameters.getCacheFile(), parameters.getKeySerializer(), parameters.getValueSerializer()); } }; MultiProcessSafePersistentIndexedCache<K, V> indexedCache = parameters.decorate( new DefaultMultiProcessSafePersistentIndexedCache<K, V>( indexedCacheFactory, fileAccess)); lock.lock(); try { caches.add(indexedCache); if (fileLock != null) { indexedCache.onStartWork(operations.getDescription(), stateAtOpen); } } finally { lock.unlock(); } return indexedCache; }
public <T> T useCache(String operationDisplayName, Factory<? extends T> factory) { if (lockOptions != null && lockOptions.getMode() == FileLockManager.LockMode.Shared) { throw new UnsupportedOperationException("Not implemented yet."); } takeOwnership(operationDisplayName); boolean wasStarted = false; try { wasStarted = onStartWork(); return factory.create(); } finally { lock.lock(); try { try { if (wasStarted) { onEndWork(); } } finally { releaseOwnership(); } } finally { lock.unlock(); } } }