Example usage for java.util.concurrent Semaphore Semaphore

List of usage examples for java.util.concurrent Semaphore Semaphore

Introduction

In this page you can find the example usage for java.util.concurrent Semaphore Semaphore.

Prototype

public Semaphore(int permits) 

Source Link

Document

Creates a Semaphore with the given number of permits and nonfair fairness setting.

Usage

From source file:org.mahasen.node.MahasenNodeManager.java

/**
 * Insert past object into DHT//from   w w w.  ja  va  2 s.  c o m
 * @param resourceId
 * @param resource
 * @throws InterruptedException
 * @throws PastException
 */
public void insertIntoDHT(final Id resourceId, MahasenResource resource, boolean isToUpdate)
        throws InterruptedException, PastException {
    MahasenPastContent myContent = new MahasenPastContent(resourceId, resource);
    myContent.setIsToUpdate(isToUpdate);
    //  String result = this.lookupDHT(resourceId);
    final Semaphore control = new Semaphore(0);

    System.out.println(" storing key for" + resource.getId() + "  :" + myContent.getId());

    {

        mahasenPastApp.insert(myContent, new Continuation<Boolean[], Exception>() {
            // the result is an Array of Booleans for each insert
            public void receiveResult(Boolean[] results) {

                int numSuccessfulStores = 0;

                for (int ctr = 0; ctr < results.length; ctr++) {
                    if (results[ctr].booleanValue())
                        numSuccessfulStores++;
                }
                control.release();
                System.out.println(" successfully stored at " + +numSuccessfulStores + " locations.");
            }

            public void receiveException(Exception result) {

                System.out.println("Error storing ");
                result.printStackTrace();
                control.release();

            }
        });

    }

}

From source file:org.ut.biolab.medsavant.client.plugin.AppController.java

/**
 * Try to load all JAR files in the given directory.
 *//*  ww  w  .j a  v  a2  s.c o  m*/
public void loadPlugins(File pluginsDir) {
    LOG.info("Loading plugins in " + pluginsDir.getAbsolutePath());
    File[] files = pluginsDir.listFiles(new FilenameFilter() {
        @Override
        public boolean accept(File dir, String name) {
            return name.toLowerCase().endsWith(".jar");
        }
    });
    for (File f : files) {
        try {
            addPlugin(f);
        } catch (PluginVersionException x) {
            LOG.warn(String.format("No compatible plugins found in %s.", f));
        }
    }

    // Check to see if we have any outdated plugins.
    if (pluginErrors.size() > 0) {
        List<String> updated = new ArrayList<String>();
        for (String s : pluginErrors.keySet()) {
            // Plugin is invalid, and we don't have a newer version.
            if (checkForPluginUpdate(s)) {
                updated.add(s);
            }
        }
        if (updated.size() > 0) {
            DialogUtils.displayMessage("Plugins Updated", String.format(
                    "<html>The following plugins were updated to be compatible with MedSavant %s:<br><br><i>%s</i></html>",
                    VersionSettings.getVersionString(), ClientMiscUtils.join(updated, ", ")));
            for (String s : updated) {
                pluginErrors.remove(s);
            }
        }
        if (pluginErrors.size() > 0) {
            StringBuilder errorStr = null;
            for (String s : pluginErrors.keySet()) {
                if (errorStr == null) {
                    errorStr = new StringBuilder();
                } else {
                    errorStr.append("<br>");
                }
                errorStr.append(s);
                errorStr.append("  ");
                errorStr.append(pluginErrors.get(s));
            }
            if (errorStr != null) {
                // The following dialog will only report plugins which we can tell are faulty before calling loadPlugin(), typically
                // by checking the version in plugin.xml.
                //                  System.out.println("Showing dialog");
                //                    JOptionPane.showMessageDialog(null, String.format("<html>The following plugins could not be loaded:<br><br><i>%s</i><br><br>They will not be available to MedSavant.</html>", errorStr),"Plugins Not Loaded", JOptionPane.ERROR_MESSAGE);
                DialogUtils.displayMessage("Apps Not Loaded", String.format(
                        "<html>The following Apps could not be loaded:<br><br><i>%s</i><br><br>They will not be available to MedSavant.</html>",
                        errorStr));
            }
        }
    }

    Set<URL> jarURLs = new HashSet<URL>();
    for (AppDescriptor desc : knownPlugins.values()) {
        try {
            if (!pluginErrors.containsKey(desc.getID())) {
                jarURLs.add(desc.getFile().toURI().toURL());
            }
        } catch (MalformedURLException ignored) {
        }
    }
    if (jarURLs.size() > 0) {
        pluginLoader = new PluginLoader(jarURLs.toArray(new URL[0]), getClass().getClassLoader());

        final Semaphore waitSem = new Semaphore(-knownPlugins.size() + 1);
        for (final AppDescriptor desc : knownPlugins.values()) {
            if (!pluginErrors.containsKey(desc.getID())) {
                new Thread("PluginLoader-" + desc) {
                    @Override
                    public void run() {
                        try {
                            loadPlugin(desc);
                            waitSem.release();
                        } catch (Throwable x) {
                            LOG.error(String.format("Unable to load %s.", desc.getName()), x);
                            pluginErrors.put(desc.getID(), x.getClass().getName());
                            fireEvent(new PluginEvent(PluginEvent.Type.ERROR, desc.getID()));
                        }
                    }
                }.start();
            } else {
                waitSem.release();
            }
        }
        LOG.info("Waiting for Apps to load...");
        try {
            waitSem.acquire();
        } catch (InterruptedException ie) {
            LOG.error("Interrupted while waiting for apps to load");
        }
        LOG.info("All Apps loaded.");
        waitSem.release();
    }
}

From source file:org.apache.bookkeeper.bookie.InterleavedLedgerStorageTest.java

@Test
public void testConsistencyCheckConcurrentGC() throws Exception {
    final long signalDone = -1;
    final List<Exception> asyncErrors = new ArrayList<>();
    final LinkedBlockingQueue<Long> toCompact = new LinkedBlockingQueue<>();
    final Semaphore awaitingCompaction = new Semaphore(0);

    interleavedStorage.flush();//  w  w w .  ja v  a2 s  . c  o m
    final long lastLogId = entryLogger.getLeastUnflushedLogId();

    final MutableInt counter = new MutableInt(0);
    entryLogger.setCheckEntryTestPoint((ledgerId, entryId, entryLogId, pos) -> {
        if (entryLogId < lastLogId) {
            if (counter.intValue() % 100 == 0) {
                try {
                    toCompact.put(entryLogId);
                    awaitingCompaction.acquire();
                } catch (InterruptedException e) {
                    asyncErrors.add(e);
                }
            }
            counter.increment();
        }
    });

    Thread mutator = new Thread(() -> {
        EntryLogCompactor compactor = new EntryLogCompactor(conf, entryLogger, interleavedStorage,
                entryLogger::removeEntryLog);
        while (true) {
            Long next = null;
            try {
                next = toCompact.take();
                if (next == null || next == signalDone) {
                    break;
                }
                compactor.compact(entryLogger.getEntryLogMetadata(next));
            } catch (BufferedChannelBase.BufferedChannelClosedException e) {
                // next was already removed, ignore
            } catch (Exception e) {
                asyncErrors.add(e);
                break;
            } finally {
                if (next != null) {
                    awaitingCompaction.release();
                }
            }
        }
    });
    mutator.start();

    List<LedgerStorage.DetectedInconsistency> inconsistencies = interleavedStorage
            .localConsistencyCheck(Optional.empty());
    for (LedgerStorage.DetectedInconsistency e : inconsistencies) {
        LOG.error("Found: {}", e);
    }
    Assert.assertEquals(0, inconsistencies.size());

    toCompact.offer(signalDone);
    mutator.join();
    for (Exception e : asyncErrors) {
        throw e;
    }

    if (!conf.isEntryLogPerLedgerEnabled()) {
        Assert.assertNotEquals(0,
                statsProvider.getCounter(BOOKIE_SCOPE + "." + STORAGE_SCRUB_PAGE_RETRIES).get().longValue());
    }
}

From source file:org.knime.al.util.noveltydetection.kernel.KernelCalculator.java

public RealMatrix calculateKernelMatrix(final double[][] training, final double[][] test,
        final ExecutionMonitor progMon) throws Exception {

    final ThreadPool pool = KNIMEConstants.GLOBAL_THREAD_POOL;
    final int procCount = (int) (Runtime.getRuntime().availableProcessors() * (2.0 / 3));
    final Semaphore semaphore = new Semaphore(procCount);
    RealMatrix result = null;/* w  ww .j  a  v a 2s . c  o  m*/
    try {
        result = pool.runInvisible(new Callable<RealMatrix>() {

            @Override
            public RealMatrix call() throws Exception {
                final double[][] resultArrayMatrix = new double[training.length][test.length];
                final CalculateKernelValuesRunnable[] kct = new CalculateKernelValuesRunnable[test.length];
                final int numberOfRunnables = kct.length;
                for (int i = 0; i < numberOfRunnables; i++) {
                    kct[i] = new CalculateKernelValuesRunnable(0, training.length, i, i + 1, training, test,
                            resultArrayMatrix, m_kernelFunction, semaphore);
                }
                final Future<?>[] threads = new Future<?>[numberOfRunnables];
                double progCounter = 0;
                for (int i = 0; i < numberOfRunnables; i++) {
                    try {
                        progMon.checkCanceled();
                    } catch (final Exception e) {
                        for (int j = 0; j < i; j++) {
                            if (threads[j] != null) {
                                threads[j].cancel(true);
                            }
                        }
                        throw e;
                    }
                    semaphore.acquire();
                    threads[i] = pool.enqueue(kct[i]);
                    progMon.setProgress(progCounter / (2 * numberOfRunnables),
                            "Kernel calculation started (" + i + "/" + numberOfRunnables + ")");
                    progCounter += 1;
                }
                for (int i = 0; i < numberOfRunnables; i++) {
                    try {
                        progMon.checkCanceled();
                    } catch (final Exception e) {
                        for (int j = 0; j < numberOfRunnables; j++) {
                            if (threads[j] != null) {
                                threads[j].cancel(true);
                            }
                        }
                        throw e;
                    }
                    semaphore.acquire();
                    threads[i].get();
                    semaphore.release();
                    progMon.setProgress(progCounter / (2 * numberOfRunnables),
                            "Kernel calculation finished (" + i + "/" + numberOfRunnables + ")");
                    progCounter += 1;
                }
                return MatrixUtils.createRealMatrix(resultArrayMatrix);
            }

        });
    } catch (final Exception e) {
        throw e;
    }

    return result;
}

From source file:org.apache.pulsar.compaction.TwoPhaseCompactor.java

private CompletableFuture<Long> phaseTwoSeekThenLoop(RawReader reader, MessageId from, MessageId to,
        MessageId lastReadId, Map<String, MessageId> latestForKey, BookKeeper bk, LedgerHandle ledger) {
    CompletableFuture<Long> promise = new CompletableFuture<>();

    reader.seekAsync(from).thenCompose((v) -> {
        Semaphore outstanding = new Semaphore(MAX_OUTSTANDING);
        CompletableFuture<Void> loopPromise = new CompletableFuture<Void>();
        phaseTwoLoop(reader, to, latestForKey, ledger, outstanding, loopPromise);
        return loopPromise;
    }).thenCompose((v) -> closeLedger(ledger))
            .thenCompose((v) -> reader.acknowledgeCumulativeAsync(lastReadId,
                    ImmutableMap.of(COMPACTED_TOPIC_LEDGER_PROPERTY, ledger.getId())))
            .whenComplete((res, exception) -> {
                if (exception != null) {
                    deleteLedger(bk, ledger).whenComplete((res2, exception2) -> {
                        if (exception2 != null) {
                            log.warn("Cleanup of ledger {} for failed", ledger, exception2);
                        }//from   w ww .jav a 2  s.  c o  m
                        // complete with original exception
                        promise.completeExceptionally(exception);
                    });
                } else {
                    promise.complete(ledger.getId());
                }
            });
    return promise;
}

From source file:com.alibaba.napoli.client.benchmark.NapoliNormalQueueTest.java

@Test
public void sendMessageWithSenderStoreEnableTest() throws Exception {

    log.info("start to execute sendMessageWithSenderStoreEnableTest");
    long beginQueueSize = JmxUtil.getQueueSize(sendConnector.getAddress(), queueName);

    qSender = new DefaultAsyncSender();
    qSender.setConnector(sendConnector);
    qSender.setName(queueName);/*  w w  w .  j a  v  a2  s  . c  o  m*/

    qSender.setStoreEnable(true);
    qSender.setReprocessInterval(10000 * 1000 * 1000);

    qSender.init();

    int tc = 10;

    log.info("yanny requestcount = " + System.getProperty("requestCount") + ", begin queue size is "
            + beginQueueSize);
    final int tp = Integer.parseInt(System.getProperty("requestCount", "20"));
    final Semaphore semaphore = new Semaphore(tc);
    final AtomicInteger sumCount = new AtomicInteger();

    final AtomicInteger requestCount = new AtomicInteger();
    long startTime = System.currentTimeMillis();
    log.info("Yanny start send request " + startTime);

    for (int i = 0; i < tc; i++) {
        Thread t = new Thread("thread--" + i) {
            public void run() {
                try {
                    //?tringap??Serializable
                    semaphore.acquire();
                    Person person = new Person();

                    person.setLoginName("superman");
                    person.setEmail("sm@1.com");
                    person.setPenName("pname");
                    person.setStatus(PersonStatus.ENABLED);

                    for (int j = 0; j < tp; j++) {
                        //      log.info("hello");
                        int id = requestCount.incrementAndGet();
                        person.setPersonId("" + id);

                        //?? ??true???alse                        
                        boolean result = qSender.send(person);
                        if (!result) {
                            log.info("----------------send to queue " + "result is false. personid=" + j);
                        } else {
                            sumCount.incrementAndGet();
                        }
                    }
                } catch (Throwable t) {
                    t.printStackTrace();
                } finally {
                    semaphore.release();
                }
            }
        };
        t.start();
    }

    while (semaphore.availablePermits() != tc) {
        Thread.sleep(100);
    }

    int totalRequest = tc * tp;

    long endTime = System.currentTimeMillis();
    log.info("yanny: send " + totalRequest + " message, take " + (endTime - startTime) + " milseconds");

    JmxUtil.waitTillQueueSizeAsTarget(sendConnector.getAddress(), queueName, beginQueueSize);

    endTime = System.currentTimeMillis();

    String errorMessage = "";

    long qBdbCount = NapoliTestUtil.getStoreSize(sendConnector.getSenderKVStore(qSender.getName()));

    log.info("yanny totalRequest " + totalRequest + " send queue success " + sumCount + " local store count:"
            + qBdbCount + " queue received " + qWorker.getAccessNum() + " take " + (endTime - startTime)
            + " milseconds");

    log.info(initConsumeMessage);

    log.info("NapoliNormalQueueTest's success=" + qWorker.getAccessNum() + " bdb's size=" + qBdbCount);

    //with store enabled, all send should succeed.
    if (qSender.getStoreEnable()) {
        if (sumCount.get() != totalRequest) {
            errorMessage += ";with store enabled, all send should return success, but not equal now. send succeed "
                    + sumCount.get() + "; total request:" + totalRequest;
        }
    } else {
        if (sumCount.get() < totalRequest * 0.95) {
            errorMessage += ";with store disabled, expected more than 95% message send succeed, total request:"
                    + totalRequest + "; send succeed " + sumCount.get();
        }
    }

    if (sumCount.get() < qWorker.getAccessNum()) {
        errorMessage += ";queue should not have success messages more than send succeed" + sumCount.get()
                + " (success " + qWorker.getAccessNum() + ")";
    }

    if ((sumCount.get() - qBdbCount) > qWorker.getAccessNum()) {
        errorMessage += ";queue received message (" + qWorker.getAccessNum()
                + ") less than send succeed - local stored message, message lost "
                + (sumCount.get() - qBdbCount);
    }

    int allowedDiff = (int) Math.round(sumCount.get() * 0.001);

    if (((qWorker.getAccessNum() + qBdbCount) - sumCount.get()) > allowedDiff) {
        errorMessage += "queue received message should not have more than send succeed + " + allowedDiff
                + " than allowed (0.1%), gap " + ((qWorker.getAccessNum() + qBdbCount) - sumCount.get());
    }

    assertTrue(errorMessage, errorMessage.equals(""));

    verify(napoliSenderStat, atMost(qWorker.getAccessNum())).sendSuccess(anyLong(), anyLong());
    verify(napoliSenderStat, atLeast((int) (sumCount.get() - qBdbCount))).sendSuccess(anyLong(), anyLong());
    verify(napoliSenderStat, times((int) qBdbCount)).sendFailure(anyLong(), anyLong());

    verify(napoliReceiverStat, times((int) qWorker.getAccessNum())).receiveSuccess(anyLong(), anyLong());
}

From source file:com.delphix.session.test.ServiceTest.java

@BeforeMethod
public void initTest() {
    // Create a new semaphore to track the completion of each test
    doneSema = new Semaphore(0);

    // Register the services
    ServerConfig config = initServiceConfig(new HelloService(helloService));
    serverManager.register(config);/*from  w  ww . j  av  a  2  s .co m*/

    config = initServiceConfig(new HelloDelayService(delayService));
    serverManager.register(config);

    // Run in server mode
    if (serverMode) {
        setupThroughputServer();
        sleep(Long.MAX_VALUE);
    }
}

From source file:com.parse.ParseAnalyticsTest.java

@Test
public void testTrackAppOpenedInBackgroundNormalCallback() throws Exception {
    Intent intent = makeIntentWithParseData("test");
    final Semaphore done = new Semaphore(0);
    ParseAnalytics.trackAppOpenedInBackground(intent, new SaveCallback() {
        @Override/*  w  w w .j  a  v a2  s  .  c  o m*/
        public void done(ParseException e) {
            assertNull(e);
            done.release();
        }
    });

    // Make sure the callback is called
    assertTrue(done.tryAcquire(1, 10, TimeUnit.SECONDS));
    verify(controller, times(1)).trackAppOpenedInBackground(eq("test"), isNull(String.class));
}

From source file:org.apache.spark.network.RpcIntegrationSuite.java

private RpcResult sendRpcWithStream(String... streams) throws Exception {
    TransportClient client = clientFactory.createClient(TestUtils.getLocalHost(), server.getPort());
    final Semaphore sem = new Semaphore(0);
    RpcResult res = new RpcResult();
    res.successMessages = Collections.synchronizedSet(new HashSet<String>());
    res.errorMessages = Collections.synchronizedSet(new HashSet<String>());

    for (String stream : streams) {
        int idx = stream.lastIndexOf('/');
        ManagedBuffer meta = new NioManagedBuffer(JavaUtils.stringToBytes(stream));
        String streamName = (idx == -1) ? stream : stream.substring(idx + 1);
        ManagedBuffer data = testData.openStream(conf, streamName);
        client.uploadStream(meta, data, new RpcStreamCallback(stream, res, sem));
    }//www  . j  a va2 s. c  o m

    if (!sem.tryAcquire(streams.length, 5, TimeUnit.SECONDS)) {
        fail("Timeout getting response from the server");
    }
    streamCallbacks.values().forEach(streamCallback -> {
        try {
            streamCallback.verify();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    });
    client.close();
    return res;
}

From source file:com.zavakid.mushroom.impl.TestSinkQueue.java

private SinkQueue<Integer> newSleepingConsumerQueue(int capacity, int... values) {
    final SinkQueue<Integer> q = new SinkQueue<Integer>(capacity);
    final Semaphore semaphore = new Semaphore(0);
    for (int i : values) {
        q.enqueue(i);//  ww w .  jav  a2  s.c  om
    }
    Thread t = new Thread() {

        @Override
        public void run() {
            try {
                q.consume(new Consumer<Integer>() {

                    public void consume(Integer e) throws InterruptedException {
                        semaphore.release(1);
                        LOG.info("sleeping");
                        Thread.sleep(1000 * 86400); // a long time
                    }
                });
            } catch (InterruptedException ex) {
                LOG.warn("Interrupted", ex);
            }
        }
    };
    t.setName("Sleeping consumer");
    t.setDaemon(true); // so jvm can exit
    t.start();
    try {
        semaphore.acquire();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    LOG.debug("Returning new sleeping consumer queue");
    return q;
}