List of usage examples for java.util.concurrent Semaphore Semaphore
public Semaphore(int permits)
From source file:org.thiesen.jiffs.jobs.clusterer.Clusterer.java
private Map<String, Long> findClusters() { final Iterable<StoryDBO> unprocessed = _storyDAO.findForClustering(); final StopWatch watch = new StopWatch(); watch.start();//from ww w. j a v a2 s . co m final Map<String, Long> foundClusters = Maps.newConcurrentMap(); final Semaphore maxEnqueedTasks = new Semaphore(100000); final List<ClusterItem> clusterItems = Lists.newLinkedList(transform(unprocessed)); final Iterator<ClusterItem> firstIterator = clusterItems.iterator(); while (firstIterator.hasNext()) { final ClusterItem firstItem = firstIterator.next(); for (final ClusterItem secondItem : clusterItems) { if (firstItem == secondItem) { continue; } EXECUTOR.submit(new ClusterFinder(maxEnqueedTasks, foundClusters, firstItem, secondItem)); maxEnqueedTasks.acquireUninterruptibly(); } firstIterator.remove(); } EXECUTOR.shutdown(); try { EXECUTOR.awaitTermination(1, TimeUnit.DAYS); } catch (InterruptedException e) { Thread.interrupted(); } watch.stop(); System.out.println("Clustering took " + watch); return foundClusters; }
From source file:org.usergrid.benchmark.commands.EventServiceTestBase.java
@Override public void runTool(CommandLine line) throws Exception { // set the hazelcast cluster property BEFORE starting spring System.setProperty("HZ_MEMBERS", line.getOptionValue("hazelcast")); startSpring();//from w w w. j a v a 2 s .c o m int workers = Integer.parseInt(line.getOptionValue("workers")); String hostName = InetAddress.getLocalHost().getHostName(); int count = Integer.parseInt(line.getOptionValue("count")); logger.info("Starting workers"); doWork(line, hostName, workers, count); //wait until the user pressed CTRL +C if (line.hasOption("wait")) { new Semaphore(0).acquire(); } }
From source file:org.apache.asterix.experiment.client.SpatialQueryGenerator.java
public void start() throws Exception { final Semaphore sem = new Semaphore(0); threadPool.submit(new QueryGenerator(sem, restHost, restPort, orchHost, orchPort, partitionRangeStart, duration, openStreetMapFilePath, isIndexOnlyPlan)); sem.acquire();//from w w w .jav a 2 s. c om }
From source file:com.pinterest.rocksplicator.controller.DispatcherTest.java
@Test public void testSingleTaskLifeCycle() throws Exception { // Assuming there is only one task in the queue PowerMockito.when(taskQueue.dequeueTask(anyString())).thenReturn(getSleepIncrementTaskFromQueue()) .thenReturn(null);//ww w . j a va 2s .co m Semaphore idleWorkersSemaphore = new Semaphore(1); ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 1, 0, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(1)); WorkerPool workerPool = new WorkerPool(threadPoolExecutor, idleWorkersSemaphore, taskQueue); TaskDispatcher dispatcher = new TaskDispatcher(2, idleWorkersSemaphore, workerPool, taskQueue); dispatcher.start(); // Wait for first task to be done synchronized (SleepIncrementTask.notifyObject) { SleepIncrementTask.notifyObject.wait(); } verify(taskQueue, atLeastOnce()).dequeueTask(anyString()); Assert.assertEquals(1, SleepIncrementTask.executionCounter.intValue()); Assert.assertEquals(1, idleWorkersSemaphore.availablePermits()); dispatcher.stop(); }
From source file:com.flipkart.phantom.http.impl.HttpConnectionPool.java
/** * Initialize the connection pool/* www . jav a 2 s . c om*/ */ public void initConnectionPool() { // max concurrent requests = max connections + request queue size this.processQueue = new Semaphore(requestQueueSize + maxConnections); // create scheme SchemeRegistry schemeRegistry = new SchemeRegistry(); // registry both http and https schemes schemeRegistry.register(new Scheme("http", port, PlainSocketFactory.getSocketFactory())); schemeRegistry.register(new Scheme("https", port, PlainSocketFactory.getSocketFactory())); PoolingClientConnectionManager cm; // create connection manager if (getTimeToLiveInSecs() > 0) { cm = new PoolingClientConnectionManager(schemeRegistry, getTimeToLiveInSecs(), TimeUnit.SECONDS); } else { cm = new PoolingClientConnectionManager(schemeRegistry); } // Max pool size cm.setMaxTotal(maxConnections); // Increase default max connection per route to 20 cm.setDefaultMaxPerRoute(maxConnections); // Increase max connections for host:port HttpHost httpHost = new HttpHost(host, port); cm.setMaxPerRoute(new HttpRoute(httpHost), maxConnections); // set timeouts HttpParams httpParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParams, connectionTimeout); HttpConnectionParams.setSoTimeout(httpParams, operationTimeout); // create client pool this.client = new DefaultHttpClient(cm, httpParams); }
From source file:com.netflix.curator.framework.recipes.leader.TestLeaderSelector.java
@Test public void testServerDying() throws Exception { Timing timing = new Timing(); LeaderSelector selector = null;/*from www . j a v a 2s .c o m*/ CuratorFramework client = CuratorFrameworkFactory.builder().connectionTimeoutMs(timing.connection()) .connectString(server.getConnectString()).retryPolicy(new RetryOneTime(1)) .sessionTimeoutMs(timing.session()).build(); client.start(); try { final Semaphore semaphore = new Semaphore(0); LeaderSelectorListener listener = new LeaderSelectorListener() { @Override public void takeLeadership(CuratorFramework client) throws Exception { semaphore.release(); Thread.sleep(Integer.MAX_VALUE); } @Override public void stateChanged(CuratorFramework client, ConnectionState newState) { if (newState == ConnectionState.LOST) { semaphore.release(); } } }; selector = new LeaderSelector(client, "/leader", listener); selector.start(); timing.acquireSemaphore(semaphore); server.close(); timing.acquireSemaphore(semaphore); } finally { IOUtils.closeQuietly(selector); IOUtils.closeQuietly(client); } }
From source file:com.yahala.ui.GroupCreateFinalActivity.java
@SuppressWarnings("unchecked") @Override// w w w . ja va 2s .c o m public boolean onFragmentCreate() { NotificationCenter.getInstance().addObserver(this, MessagesController.updateInterfaces); NotificationCenter.getInstance().addObserver(this, MessagesController.chatDidCreated); NotificationCenter.getInstance().addObserver(this, MessagesController.chatDidFailCreate); avatarUpdater.parentFragment = this; avatarUpdater.delegate = this; selectedContacts = getArguments().getStringArrayList("result"); final ArrayList<String> usersToLoad = new ArrayList<String>(); for (String jid : selectedContacts) { TLRPC.User user = ContactsController.getInstance().friendsDict.get(jid); if (user == null) { usersToLoad.add(jid); } } if (!usersToLoad.isEmpty()) { final Semaphore semaphore = new Semaphore(0); final ArrayList<TLRPC.User> users = new ArrayList<TLRPC.User>(); final boolean[] error = new boolean[1]; MessagesStorage.getInstance().storageQueue.postRunnable(new Runnable() { @Override public void run() { users.addAll(MessagesStorage.getInstance().getUsers(usersToLoad, error)); semaphore.release(); } }); try { semaphore.acquire(); } catch (Exception e) { FileLog.e("tmessages", e); } //if (error[0]) { // return false; // } if (!users.isEmpty()) { for (TLRPC.User user : users) { ContactsController.getInstance().friendsDict.putIfAbsent(user.jid, user); } } else { return false; } } super.onFragmentCreate(); return true; }
From source file:org.apache.solr.schema.TestSchemalessBufferedUpdates.java
@Test public void test() throws Exception { DirectUpdateHandler2.commitOnClose = false; final Semaphore logReplay = new Semaphore(0); final Semaphore logReplayFinish = new Semaphore(0); UpdateLog.testing_logReplayHook = () -> { try {/*from w ww . j av a2s . co m*/ assertTrue(logReplay.tryAcquire(TIMEOUT, TimeUnit.SECONDS)); } catch (Exception e) { throw new RuntimeException(e); } }; UpdateLog.testing_logReplayFinishHook = logReplayFinish::release; SolrQueryRequest req = req(); UpdateHandler uhandler = req.getCore().getUpdateHandler(); UpdateLog ulog = uhandler.getUpdateLog(); try { assertEquals(UpdateLog.State.ACTIVE, ulog.getState()); // Invalid date will be normalized by ParseDateField URP updateJ(jsonAdd(processAdd(sdoc("id", "1", "f_dt", "2017-01-04"))), params(DISTRIB_UPDATE_PARAM, FROM_LEADER)); assertU(commit()); assertJQ(req("q", "*:*"), "/response/numFound==1"); ulog.bufferUpdates(); assertEquals(UpdateLog.State.BUFFERING, ulog.getState()); // If the ParseDateField URP isn't ahead of the DUP, then the date won't be normalized in the buffered tlog entry, // and the doc won't be indexed on the replaying replica - a warning is logged as follows: // WARN [...] o.a.s.u.UpdateLog REYPLAY_ERR: IOException reading log // org.apache.solr.common.SolrException: Invalid Date String:'2017-01-05' // at org.apache.solr.util.DateMathParser.parseMath(DateMathParser.java:234) // at org.apache.solr.schema.TrieField.createField(TrieField.java:725) [...] updateJ(jsonAdd(processAdd(sdoc("id", "2", "f_dt", "2017-01-05"))), params(DISTRIB_UPDATE_PARAM, FROM_LEADER)); Future<UpdateLog.RecoveryInfo> rinfoFuture = ulog.applyBufferedUpdates(); assertTrue(rinfoFuture != null); assertEquals(UpdateLog.State.APPLYING_BUFFERED, ulog.getState()); logReplay.release(1000); UpdateLog.RecoveryInfo rinfo = rinfoFuture.get(); assertEquals(UpdateLog.State.ACTIVE, ulog.getState()); assertU(commit()); assertJQ(req("q", "*:*"), "/response/numFound==2"); } finally { DirectUpdateHandler2.commitOnClose = true; UpdateLog.testing_logReplayHook = null; UpdateLog.testing_logReplayFinishHook = null; req().close(); } }
From source file:com.music.Generator.java
@PostConstruct public void init() throws MidiUnavailableException, IOException, InvalidMidiDataException { //TODO http://marsyas.info/ when input signal processing is needed semaphore = new Semaphore(maxConcurrentGenerations); manipulators.add(new MetreConfigurer()); manipulators.add(new PartConfigurer()); manipulators.add(new ScaleConfigurer()); manipulators.add(new MainPartGenerator()); manipulators.add(new AccompanimentPartGenerator()); manipulators.add(new Arpeggiator()); manipulators.add(new PercussionGenerator()); manipulators.add(new SimpleBeatGenerator()); manipulators.add(new BassPartGenerator()); manipulators.add(new DroneGenerator()); manipulators.add(new EffectsGenerator()); manipulators.add(new PadsGenerator()); manipulators.add(new TimpaniPartGenerator()); manipulators.add(new TitleGenerator()); try {/*from www .j a v a 2 s .c o m*/ Collection<File> files = FileUtils.listFiles(new File(configLocation + "/soundbanks/"), new String[] { "sf2" }, false); for (File file : files) { InputStream is = new BufferedInputStream(new FileInputStream(file)); soundbanks.add(MidiSystem.getSoundbank(is)); } } catch (IOException ex) { logger.warn("Problem loading soundbank: " + ex.getMessage()); // ignore } //initJMusicSynthesizer(); }
From source file:org.paxle.core.threading.AWorkerTest.java
public void testWorkerTriggeredAndReturnToPool() throws InterruptedException { final ICommand command = mock(ICommand.class); final DummyWorker worker = new DummyWorker(); final Semaphore waitforReturnToPool = new Semaphore(0); checking(new Expectations() { {//from w w w.ja v a 2s.c o m // allow enqueuing and dequeueing of exactly one command one(inQueue).dequeue(); will(returnValue(command)); one(outQueue).enqueue(with(same(command))); // pool is not closed one(pool).closed(); will(returnValue(false)); // worker must return itself into pool one(pool).returnWorker(with(same(worker))); will(new Action() { public void describeTo(Description arg0) { } public Object invoke(Invocation invocation) throws Throwable { waitforReturnToPool.release(); return null; } }); } }); // init worker worker.setInQueue(this.inQueue); worker.setOutQueue(this.outQueue); worker.setPool(this.pool); // trigger worker to dequeue and process new command worker.trigger(); // wait until worker has returned itself into pool assertTrue(waitforReturnToPool.tryAcquire(5, TimeUnit.SECONDS)); // terminate worker worker.terminate(); assertTrue(worker.commandProcessed); }