List of usage examples for java.util.concurrent Semaphore Semaphore
public Semaphore(int permits)
From source file:org.commoncrawl.service.directory.BlockingClient.java
public static byte[] loadDataFromPath(InetAddress directoryServiceServer, String itemPath) throws IOException { final BlockingClient client = new BlockingClient(); try {/* www. j av a 2 s . c o m*/ client.connect(directoryServiceServer); DirectoryServiceQuery query = new DirectoryServiceQuery(); query.setItemPath(itemPath); client._blockingCallSemaphore = new Semaphore(0); client._serviceStub.query(query, new AsyncRequest.Callback<DirectoryServiceQuery, DirectoryServiceItemList>() { @Override public void requestComplete( AsyncRequest<DirectoryServiceQuery, DirectoryServiceItemList> request) { if (request.getStatus() == AsyncRequest.Status.Success) { DirectoryServiceItem item = request.getOutput().getItems().get(0); client._buffer = new Buffer(item.getItemData().getReadOnlyBytes()); } else { LOG.error("Request:" + request.getInput().getItemPath() + " returned NULL result."); } if (client._blockingCallSemaphore != null) { client._blockingCallSemaphore.release(); } } }); client._blockingCallSemaphore.acquireUninterruptibly(); client._blockingCallSemaphore = null; if (client._buffer.getCount() == 0) { throw new IOException("Failed to retrieve item at path:" + itemPath); } else { return client._buffer.get(); } } catch (IOException e) { throw e; } }
From source file:org.minig.imap.sieve.SieveClientSupport.java
public SieveClientSupport(String login, String password) { this.lock = new Semaphore(1); this.lastResponses = new LinkedList<SieveResponse>(); this.authenticate = new SieveAuthenticate(login, password); }
From source file:org.wso2.carbon.databridge.core.internal.queue.EventBlockingQueue.java
public EventBlockingQueue(int maxQueueSize, int maxSizeCapacity) { super(maxQueueSize); this.currentSize = new AtomicInteger(0); this.maxSize = maxSizeCapacity; this.semaphore = new Semaphore(1); }
From source file:org.phoenicis.multithreading.ControlledThreadPoolExecutorService.java
public ControlledThreadPoolExecutorService(String name, int numberOfThread, int queueSize) { super(numberOfThread, numberOfThread, 0, TimeUnit.SECONDS, new LinkedBlockingDeque<>(queueSize)); this.semaphore = new Semaphore(queueSize); this.name = name; this.numberOfThreads = numberOfThread; }
From source file:biospectra.utils.BlockingExecutor.java
/** * Creates a BlockingExecutor which will block and prevent further * submission to the pool when the specified queue size has been reached. * * @param poolSize the number of the threads in the pool * @param queueSize the size of the queue *///from w w w . j av a 2s. com public BlockingExecutor(final int poolSize, final int queueSize) { super(poolSize, poolSize, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); // the semaphore is bounding both the number of tasks currently executing // and those queued up semaphore = new Semaphore(poolSize + queueSize); }
From source file:com.microsoft.tfs.core.clients.versioncontrol.internal.concurrent.BoundedExecutor.java
/** * Constructs a {@link BoundedExecutor} that throttles the task submission * rate to the given executor by blocking. * * @param executor//from w ww. j a v a2 s . c om * the executor to hand tasks to (must not be <code>null</code>) This * should be an unbounded pool, because bounding it doesn't make * sense (this class does the throttling). * @param bound * the maximum number of tasks to be executing via the given * executor. Must be > 0. */ public BoundedExecutor(final Executor executor, final int bound) { Check.notNull(executor, "executor"); //$NON-NLS-1$ Check.isTrue(bound > 0, "bound > 0"); //$NON-NLS-1$ this.executor = executor; semaphore = new Semaphore(bound); maxPermits = bound; log.trace(MessageFormat.format("constructed with bounds {0}", Integer.toString(bound))); //$NON-NLS-1$ }
From source file:org.splandroid.tr.commons.KillableProcess.java
public KillableProcess(String execCmd, File workingDir, ExecuteStreamHandler streamHandler) { // Build a process status object for this process procStatus = new ProcessStatus(); // A semaphore to notify when the thread has finished finishSema = new Semaphore(1); // An empty command line cmdLine = new ArrayList<String>(); this.addCommandLine(execCmd); // Environment variables to add to the default environment envVars = new ArrayList<ImmutablePair<String, String>>(); // Working directory this.workingDir = workingDir; // Stream handler this.streamHandler = streamHandler; }
From source file:com.netflix.curator.framework.recipes.leader.TestLeaderSelectorCluster.java
@Test public void testRestart() throws Exception { final Timing timing = new Timing(); CuratorFramework client = null;/*from w w w .ja v a2 s . c o m*/ TestingCluster cluster = new TestingCluster(3); cluster.start(); try { client = CuratorFrameworkFactory.newClient(cluster.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1)); client.start(); final Semaphore semaphore = new Semaphore(0); LeaderSelectorListener listener = new LeaderSelectorListener() { @Override public void takeLeadership(CuratorFramework client) throws Exception { List<String> names = client.getChildren().forPath("/leader"); Assert.assertTrue(names.size() > 0); semaphore.release(); } @Override public void stateChanged(CuratorFramework client, ConnectionState newState) { } }; LeaderSelector selector = new LeaderSelector(client, "/leader", listener); selector.autoRequeue(); selector.start(); Assert.assertTrue(timing.acquireSemaphore(semaphore)); InstanceSpec connectionInstance = cluster .findConnectionInstance(client.getZookeeperClient().getZooKeeper()); cluster.killServer(connectionInstance); Assert.assertTrue(timing.multiple(4).acquireSemaphore(semaphore)); } finally { IOUtils.closeQuietly(client); IOUtils.closeQuietly(cluster); } }
From source file:edu.iu.harp.schstatic.TaskMonitor.java
TaskMonitor(int taskID, T task, Submitter<I> submitter, int numTasks, Semaphore barrier1) { this.taskObject = task; this.taskObject.setTaskID(taskID); this.taskObject.setNumTasks(numTasks); this.taskObject.setSubmitter(submitter); this.inputQueue = new LinkedBlockingQueue<>(); this.outputQueue = new LinkedBlockingQueue<>(); this.inputCount = 0; this.outputCount = 0; this.errorCount = 0; this.barrier1 = barrier1; this.barrier2 = new Semaphore(0); }
From source file:edu.iu.harp.schdynamic.DynamicScheduler.java
public DynamicScheduler(List<T> tasks) { inputQueue = new LinkedBlockingDeque<>(); outputQueue = new LinkedBlockingQueue<>(); threads = null;/*from w w w . j a v a2s .co m*/ inputCount = 0; outputCount = 0; errorCount = 0; isRunning = false; isPausing = false; barrier1 = new Semaphore(0); numTaskMonitors = tasks.size(); this.tasks = tasks; taskMonitors = new ArrayList<>(); for (T task : tasks) { taskMonitors.add(new TaskMonitor<>(inputQueue, outputQueue, task, barrier1)); } }