Example usage for java.util.concurrent Semaphore acquire

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

Introduction

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

Prototype

public void acquire() throws InterruptedException 

Source Link

Document

Acquires a permit from this semaphore, blocking until one is available, or the thread is Thread#interrupt interrupted .

Usage

From source file:org.commoncrawl.util.HDFSBlockTransferUtility.java

public static void main(String[] args) {
    final String transferFromDisk = args[0];
    final String transferToDisks[] = args[1].split(",");
    final LinkedBlockingQueue<String> queues[] = new LinkedBlockingQueue[transferToDisks.length];
    final Semaphore waitSemaphore = new Semaphore(-(transferToDisks.length - 1));
    for (int i = 0; i < transferToDisks.length; ++i) {
        queues[i] = new LinkedBlockingQueue<String>();
    }/* www.j a v  a  2s  .c o  m*/

    File transferSource = new File(transferFromDisk);
    for (File transferFile : transferSource.listFiles()) {
        if (transferFile.isDirectory()) {
            int partition = Math.abs(transferFile.getName().hashCode() % transferToDisks.length);
            try {
                queues[partition].put(transferFile.getAbsolutePath());
            } catch (InterruptedException e) {
            }
        } else {
            try {
                doCopyFile(transferFile, new File(transferToDisks[0], transferFile.getName()), true);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    Thread threads[] = new Thread[transferToDisks.length];
    for (int i = 0; i < transferToDisks.length; ++i) {

        final int threadIdx = i;

        try {
            queues[threadIdx].put("");
        } catch (InterruptedException e1) {
        }

        threads[i] = new Thread(new Runnable() {

            @Override
            public void run() {

                try {
                    File transferToDisk = new File(transferToDisks[threadIdx]);

                    LinkedBlockingQueue<String> queue = queues[threadIdx];

                    while (true) {
                        try {
                            String nextDir = queue.take();
                            if (nextDir.length() == 0) {
                                break;
                            } else {
                                File sourceDir = new File(nextDir);
                                File targetDir = new File(transferToDisk, sourceDir.getName());

                                try {
                                    copyFiles(sourceDir, targetDir, true);
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }

                            }
                        } catch (InterruptedException e) {
                        }
                    }
                } finally {
                    waitSemaphore.release();
                }
            }

        });
        threads[i].start();
    }

    System.out.println("Waiting for Worker Threads");
    try {
        waitSemaphore.acquire();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println("Worker Threads Dead");
}

From source file:org.apache.pulsar.testclient.ManagedLedgerWriter.java

public static void main(String[] args) throws Exception {

    final Arguments arguments = new Arguments();
    JCommander jc = new JCommander(arguments);
    jc.setProgramName("pulsar-perf-producer");

    try {//w w  w.  jav a2  s  .  com
        jc.parse(args);
    } catch (ParameterException e) {
        System.out.println(e.getMessage());
        jc.usage();
        System.exit(-1);
    }

    if (arguments.help) {
        jc.usage();
        System.exit(-1);
    }

    arguments.testTime = TimeUnit.SECONDS.toMillis(arguments.testTime);

    // Dump config variables
    ObjectMapper m = new ObjectMapper();
    ObjectWriter w = m.writerWithDefaultPrettyPrinter();
    log.info("Starting Pulsar managed-ledger perf writer with config: {}", w.writeValueAsString(arguments));

    byte[] payloadData = new byte[arguments.msgSize];
    ByteBuf payloadBuffer = PooledByteBufAllocator.DEFAULT.directBuffer(arguments.msgSize);
    payloadBuffer.writerIndex(arguments.msgSize);

    // Now processing command line arguments
    String managedLedgerPrefix = "test-" + DigestUtils.sha1Hex(UUID.randomUUID().toString()).substring(0, 5);

    ClientConfiguration bkConf = new ClientConfiguration();
    bkConf.setUseV2WireProtocol(true);
    bkConf.setAddEntryTimeout(30);
    bkConf.setReadEntryTimeout(30);
    bkConf.setThrottleValue(0);
    bkConf.setNumChannelsPerBookie(arguments.maxConnections);
    bkConf.setZkServers(arguments.zookeeperServers);

    ManagedLedgerFactoryConfig mlFactoryConf = new ManagedLedgerFactoryConfig();
    mlFactoryConf.setMaxCacheSize(0);
    ManagedLedgerFactory factory = new ManagedLedgerFactoryImpl(bkConf, mlFactoryConf);

    ManagedLedgerConfig mlConf = new ManagedLedgerConfig();
    mlConf.setEnsembleSize(arguments.ensembleSize);
    mlConf.setWriteQuorumSize(arguments.writeQuorum);
    mlConf.setAckQuorumSize(arguments.ackQuorum);
    mlConf.setMinimumRolloverTime(10, TimeUnit.MINUTES);
    mlConf.setMetadataEnsembleSize(arguments.ensembleSize);
    mlConf.setMetadataWriteQuorumSize(arguments.writeQuorum);
    mlConf.setMetadataAckQuorumSize(arguments.ackQuorum);
    mlConf.setDigestType(arguments.digestType);
    mlConf.setMaxSizePerLedgerMb(2048);

    List<CompletableFuture<ManagedLedger>> futures = new ArrayList<>();

    for (int i = 0; i < arguments.numManagedLedgers; i++) {
        String name = String.format("%s-%03d", managedLedgerPrefix, i);
        CompletableFuture<ManagedLedger> future = new CompletableFuture<>();
        futures.add(future);
        factory.asyncOpen(name, mlConf, new OpenLedgerCallback() {

            @Override
            public void openLedgerComplete(ManagedLedger ledger, Object ctx) {
                future.complete(ledger);
            }

            @Override
            public void openLedgerFailed(ManagedLedgerException exception, Object ctx) {
                future.completeExceptionally(exception);
            }
        }, null);
    }

    List<ManagedLedger> managedLedgers = futures.stream().map(CompletableFuture::join)
            .collect(Collectors.toList());

    log.info("Created {} managed ledgers", managedLedgers.size());

    Runtime.getRuntime().addShutdownHook(new Thread() {
        public void run() {
            printAggregatedStats();
        }
    });

    Collections.shuffle(managedLedgers);
    AtomicBoolean isDone = new AtomicBoolean();

    List<List<ManagedLedger>> managedLedgersPerThread = Lists.partition(managedLedgers,
            Math.max(1, managedLedgers.size() / arguments.numThreads));

    for (int i = 0; i < arguments.numThreads; i++) {
        List<ManagedLedger> managedLedgersForThisThread = managedLedgersPerThread.get(i);
        int nunManagedLedgersForThisThread = managedLedgersForThisThread.size();
        long numMessagesForThisThread = arguments.numMessages / arguments.numThreads;
        int maxOutstandingForThisThread = arguments.maxOutstanding;

        executor.submit(() -> {
            try {
                final double msgRate = arguments.msgRate / (double) arguments.numThreads;
                final RateLimiter rateLimiter = RateLimiter.create(msgRate);

                // Acquire 1 sec worth of messages to have a slower ramp-up
                rateLimiter.acquire((int) msgRate);
                final long startTime = System.currentTimeMillis();

                final Semaphore semaphore = new Semaphore(maxOutstandingForThisThread);

                final AddEntryCallback addEntryCallback = new AddEntryCallback() {
                    @Override
                    public void addComplete(Position position, Object ctx) {
                        long sendTime = (Long) (ctx);
                        messagesSent.increment();
                        bytesSent.add(payloadData.length);

                        long latencyMicros = NANOSECONDS.toMicros(System.nanoTime() - sendTime);
                        recorder.recordValue(latencyMicros);
                        cumulativeRecorder.recordValue(latencyMicros);

                        semaphore.release();
                    }

                    @Override
                    public void addFailed(ManagedLedgerException exception, Object ctx) {
                        log.warn("Write error on message", exception);
                        System.exit(-1);
                    }
                };

                // Send messages on all topics/producers
                long totalSent = 0;
                while (true) {
                    for (int j = 0; j < nunManagedLedgersForThisThread; j++) {
                        if (arguments.testTime > 0) {
                            if (System.currentTimeMillis() - startTime > arguments.testTime) {
                                log.info("------------------- DONE -----------------------");
                                printAggregatedStats();
                                isDone.set(true);
                                Thread.sleep(5000);
                                System.exit(0);
                            }
                        }

                        if (numMessagesForThisThread > 0) {
                            if (totalSent++ >= numMessagesForThisThread) {
                                log.info("------------------- DONE -----------------------");
                                printAggregatedStats();
                                isDone.set(true);
                                Thread.sleep(5000);
                                System.exit(0);
                            }
                        }

                        semaphore.acquire();
                        rateLimiter.acquire();

                        final long sendTime = System.nanoTime();
                        managedLedgersForThisThread.get(j).asyncAddEntry(payloadBuffer, addEntryCallback,
                                sendTime);
                    }
                }
            } catch (Throwable t) {
                log.error("Got error", t);
            }
        });
    }

    // Print report stats
    long oldTime = System.nanoTime();

    Histogram reportHistogram = null;

    while (true) {
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            break;
        }

        if (isDone.get()) {
            break;
        }

        long now = System.nanoTime();
        double elapsed = (now - oldTime) / 1e9;

        double rate = messagesSent.sumThenReset() / elapsed;
        double throughput = bytesSent.sumThenReset() / elapsed / 1024 / 1024 * 8;

        reportHistogram = recorder.getIntervalHistogram(reportHistogram);

        log.info(
                "Throughput produced: {}  msg/s --- {} Mbit/s --- Latency: mean: {} ms - med: {} - 95pct: {} - 99pct: {} - 99.9pct: {} - 99.99pct: {} - Max: {}",
                throughputFormat.format(rate), throughputFormat.format(throughput),
                dec.format(reportHistogram.getMean() / 1000.0),
                dec.format(reportHistogram.getValueAtPercentile(50) / 1000.0),
                dec.format(reportHistogram.getValueAtPercentile(95) / 1000.0),
                dec.format(reportHistogram.getValueAtPercentile(99) / 1000.0),
                dec.format(reportHistogram.getValueAtPercentile(99.9) / 1000.0),
                dec.format(reportHistogram.getValueAtPercentile(99.99) / 1000.0),
                dec.format(reportHistogram.getMaxValue() / 1000.0));

        reportHistogram.reset();

        oldTime = now;
    }

    factory.shutdown();
}

From source file:Main.java

public static void acquire(Semaphore semaphore) {
    try {//  ww w  .ja  va  2  s. c o m
        semaphore.acquire();
    } catch (InterruptedException iex) {
        // ignore
    }
}

From source file:Main.java

public static void waitForRelease(Semaphore semaphore) {
    try {//  ww w .  j a  va 2s  .c o  m
        semaphore.acquire();
    } catch (InterruptedException iex) {
        //ignore
    }
    semaphore.release();
}

From source file:edu.iu.harp.schdynamic.ComputeUtil.java

public static void acquire(Semaphore sem) {
    boolean isFailed = false;
    do {//from   w  w w  . j av  a2  s.co m
        try {
            sem.acquire();
            isFailed = false;
        } catch (Exception e) {
            isFailed = true;
            LOG.error("Error when acquiring semaphore", e);
        }
    } while (isFailed);
}

From source file:Main.java

/**
 * Locks a semaphore for handling multi threading with insert/delete/update methods.
 *
 * @param key key for inserting the locked semaphore in a hashmap.
 * @return the locked semaphore.//from w  w  w . j a  v a  2 s.  c om
 */
public static boolean lockChat(String key) {
    Semaphore semaphore = new Semaphore(1);
    Semaphore oldSemaphore = semaphoreMap.putIfAbsent(key, semaphore);

    if (oldSemaphore != null) {
        semaphore = oldSemaphore;
    }
    //semaphore.tryAcquire(30, TimeUnit.SECONDS);
    try {
        semaphore.acquire();
        return true;
    } catch (InterruptedException ignored) {
        Log.w(TAG, "Could not acquire chat: " + key);
        return false;
    }
}

From source file:org.marekasf.troughput.XYHistogramChart.java

public static void display(final AdaptiveHistogram h, final String title) {

    final XYHistogramChart demo = new XYHistogramChart(h, title);
    demo.pack();//w ww . j  a  v a 2  s.  c o  m
    RefineryUtilities.centerFrameOnScreen(demo);
    demo.setVisible(true);
    final Semaphore semaphore = new Semaphore(0);
    demo.addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(final WindowEvent we) {
            semaphore.release();
        }
    });
    try {
        semaphore.acquire();
    } catch (final InterruptedException e) {
        //
    }
}

From source file:com.qwazr.search.index.SchemaInstance.java

private static Semaphore atomicAquire(Semaphore semaphore) throws InterruptedException {
    if (semaphore == null)
        return null;
    semaphore.acquire();
    return semaphore;
}

From source file:com.all.app.ApplicationUtils.java

public static void showFrameAndWaitForClose(Window window) {
    try {// ww w .j  a v a  2  s .  co  m
        final Semaphore lock = new Semaphore(0);
        window.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                lock.release();
            }
        });
        window.setVisible(true);
        lock.acquire();
    } catch (Exception e) {
        LOG.error(e, e);
    }
}

From source file:org.apache.kylin.storage.hbase.util.HbaseStreamingInput.java

public static void addData(String tableName) throws IOException {

    createTable(tableName);// w ww. j  a  va  2  s . co m

    final Semaphore semaphore = new Semaphore(0);
    new Thread(new Runnable() {
        @Override
        public void run() {
            scheduleJob(semaphore, 300000);//5 minutes a batch
        }
    }).start();

    while (true) {
        try {
            semaphore.acquire();
            int waiting = semaphore.availablePermits();
            if (waiting > 0) {
                logger.warn("There are another " + waiting + " batches waiting to be added");
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            e.printStackTrace();
        }

        Connection conn = getConnection();
        Table table = conn.getTable(TableName.valueOf(tableName));

        byte[] key = new byte[8 + 4];//time + id

        logger.info("============================================");
        long startTime = System.currentTimeMillis();
        logger.info("data load start time in millis: " + startTime);
        logger.info("data load start at " + formatTime(startTime));
        List<Put> buffer = Lists.newArrayList();
        for (int i = 0; i < (1 << 10); ++i) {
            long time = System.currentTimeMillis();
            Bytes.putLong(key, 0, time);
            Bytes.putInt(key, 8, i);
            Put put = new Put(key);
            byte[] cell = randomBytes(CELL_SIZE);
            put.addColumn(CF, QN, cell);
            buffer.add(put);
        }
        table.put(buffer);
        table.close();
        conn.close();
        long endTime = System.currentTimeMillis();
        logger.info("data load end at " + formatTime(endTime));
        logger.info("data load time consumed: " + (endTime - startTime));
        logger.info("============================================");
    }
}