List of usage examples for java.util.concurrent.locks ReentrantLock ReentrantLock
public ReentrantLock(boolean fair)
From source file:Main.java
public static void main(String[] args) { int[] runningThreads = { 0 }; int[] taskcount = { 10 }; Lock myLock = new ReentrantLock(true); int maxThreadQty = 3; while ((taskcount[0] > 0) && (runningThreads[0] < maxThreadQty)) { myLock.lock();//from www. j a va2 s . co m runningThreads[0]++; myLock.unlock(); new Thread("T") { public void run() { myLock.lock(); taskcount[0]--; runningThreads[0]--; myLock.unlock(); } }.start(); } }
From source file:eu.riscoss.server.DBConnector.java
private static void lock(String domain) { ReentrantLock lock = locks.get(domain); if (lock == null) { lock = new ReentrantLock(true); locks.put(domain, lock);//from ww w. j av a 2 s .c o m } System.out.println("'" + domain + "' locked by " + Thread.currentThread()); // Thread.dumpStack(); lock.lock(); }
From source file:cherry.foundation.numbering.SimpleNumberingStore.java
@Override public synchronized long loadAndLock(String numberName) { Lock lock = lockMap.get(numberName); if (lock == null) { lock = new ReentrantLock(true); lockMap.put(numberName, lock);//from www . j a v a 2s . c om } lock.lock(); return currentValueMap.get(numberName).longValue(); }
From source file:org.jenkinsci.plugins.mesos.MesosRetentionStrategy.java
private void readResolve() { computerCheckLock = new ReentrantLock(false); }
From source file:org.springframework.integration.store.MessageGroupQueue.java
public MessageGroupQueue(MessageGroupStore messageGroupStore, Object groupId) { this(messageGroupStore, groupId, DEFAULT_CAPACITY, new ReentrantLock(true)); }
From source file:org.springframework.integration.store.MessageGroupQueue.java
public MessageGroupQueue(MessageGroupStore messageGroupStore, Object groupId, int capacity) { this(messageGroupStore, groupId, capacity, new ReentrantLock(true)); }
From source file:massbank.svn.SVNOperation.java
public SVNOperation(String repoDirName, String wcName) throws SVNException { repoDirName = SVNUtils.escapeDirName(repoDirName); String repoURL = SVNServiceManager.SVN_BASE_URL; if (wcName.equals(WC_OPEN_DATA)) { repoURL += "OpenData/record/" + repoDirName; } else {//w w w. j ava 2 s. c o m repoURL += "BackupData/" + repoDirName; } this.workCopyPath = SVNUtils.getWorkCopyPath(repoDirName, wcName); if (!rlockList.containsKey(this.workCopyPath)) { this.rlock = new ReentrantLock(true); this.rlockList.put(this.workCopyPath, this.rlock); } else { this.rlock = this.rlockList.get(this.workCopyPath); } // logger.severe("** LOCK:" + this.workCopyPath); this.rlock.lock(); this.svnClient = new SVNClientWrapper(repoURL, this.workCopyPath); if (wcName.equals(WC_BACKUP_UPLOAD) || wcName.equals(WC_OPEN_DATA)) { checkout(); } this.ipAddress = SVNUtils.getLocalIPAddress(); }
From source file:com.github.horrorho.liquiddonkey.cloud.Authenticator.java
Authenticator(String id, String password, Auth auth) {
this(id, password, new ReentrantLock(false), Token.from(auth), null);
}
From source file:io.fabric8.maven.core.service.PortForwardService.java
/** * Forwards a port to the newest pod matching the given selector. * If another pod is created, it forwards connections to the new pod once it's ready. *//*w w w .j av a 2s .c o m*/ public Closeable forwardPortAsync(final Logger externalProcessLogger, final LabelSelector podSelector, final int remotePort, final int localPort) throws Fabric8ServiceException { final Lock monitor = new ReentrantLock(true); final Condition podChanged = monitor.newCondition(); final Pod[] nextForwardedPod = new Pod[1]; final Thread forwarderThread = new Thread() { @Override public void run() { Pod currentPod = null; Closeable currentPortForward = null; try { monitor.lock(); while (true) { if (podEquals(currentPod, nextForwardedPod[0])) { podChanged.await(); } else { Pod nextPod = nextForwardedPod[0]; // may be null try { monitor.unlock(); // out of critical section if (currentPortForward != null) { log.info("Closing port-forward from pod %s", KubernetesHelper.getName(currentPod)); currentPortForward.close(); currentPortForward = null; } if (nextPod != null) { log.info("Starting port-forward to pod %s", KubernetesHelper.getName(nextPod)); currentPortForward = forwardPortAsync(externalProcessLogger, KubernetesHelper.getName(nextPod), remotePort, localPort); } else { log.info("Waiting for a pod to become ready before starting port-forward"); } currentPod = nextPod; } finally { monitor.lock(); } } } } catch (InterruptedException e) { log.debug("Port-forwarding thread interrupted", e); Thread.currentThread().interrupt(); } catch (Exception e) { log.warn("Error while port-forwarding to pod", e); } finally { monitor.unlock(); if (currentPortForward != null) { try { currentPortForward.close(); } catch (Exception e) { } } } } }; // Switching forward to the current pod if present Pod newPod = getNewestPod(podSelector); nextForwardedPod[0] = newPod; final Watch watch = KubernetesClientUtil.withSelector(kubernetes.pods(), podSelector, log) .watch(new Watcher<Pod>() { @Override public void eventReceived(Action action, Pod pod) { monitor.lock(); try { List<Pod> candidatePods; if (nextForwardedPod[0] != null) { candidatePods = new LinkedList<>(); candidatePods.add(nextForwardedPod[0]); candidatePods.add(pod); } else { candidatePods = Collections.singletonList(pod); } Pod newPod = getNewestPod(candidatePods); // may be null if (!podEquals(nextForwardedPod[0], newPod)) { nextForwardedPod[0] = newPod; podChanged.signal(); } } finally { monitor.unlock(); } } @Override public void onClose(KubernetesClientException e) { // don't care } }); forwarderThread.start(); final Closeable handle = new Closeable() { @Override public void close() throws IOException { try { watch.close(); } catch (Exception e) { } try { forwarderThread.interrupt(); forwarderThread.join(15000); } catch (Exception e) { } } }; Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { try { handle.close(); } catch (Exception e) { // suppress } } }); return handle; }
From source file:net.sf.jasperreports.web.servlets.AsyncJasperPrintAccessor.java
/** * Create a report accessor./*w w w . ja v a2 s . c om*/ * * @param fillHandle the asynchronous fill handle used by this accessor */ public AsyncJasperPrintAccessor(FillHandle fillHandle) { this.fillHandle = fillHandle; lock = new ReentrantLock(true); pageCondition = lock.newCondition(); fillHandle.addListener(this); fillHandle.addFillListener(this); }