Example usage for java.util.concurrent.atomic AtomicInteger wait

List of usage examples for java.util.concurrent.atomic AtomicInteger wait

Introduction

In this page you can find the example usage for java.util.concurrent.atomic AtomicInteger wait.

Prototype

public final native void wait(long timeoutMillis) throws InterruptedException;

Source Link

Document

Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

Usage

From source file:net.audumla.scheduler.quartz.SunScheduleTest.java

protected AtomicInteger scheduleJob(Trigger trigger, int expectedCount, int maxWait) throws SchedulerException {
    AtomicInteger ai = new AtomicInteger(expectedCount);
    JobDetail job = JobBuilder.newJob(TestJob.class).withIdentity("testJob", "testJobGroup").storeDurably()
            .build();/*from  w  w w.  j a  v  a2 s .  c o  m*/
    job.getJobDataMap().put(TestJob.EXECUTION_COUNT, ai);
    StdSchedulerFactory.getDefaultScheduler().scheduleJob(job, trigger);
    if (!StdSchedulerFactory.getDefaultScheduler().isStarted()) {
        StdSchedulerFactory.getDefaultScheduler().start();

    }
    synchronized (ai) {
        try {
            ai.wait(maxWait * 1000);
        } catch (Exception ex) {
            logger.error(ex);
        }
    }
    return ai;

}

From source file:com.netflix.curator.framework.recipes.locks.TestInterProcessSemaphore.java

@Test
public void testMaxPerSession() throws Exception {
    final int CLIENT_QTY = 10;
    final int LOOP_QTY = 100;
    final Random random = new Random();
    final int SESSION_MAX = random.nextInt(75) + 25;

    List<Future<Object>> futures = Lists.newArrayList();
    ExecutorService service = Executors.newCachedThreadPool();
    final Counter counter = new Counter();
    final AtomicInteger available = new AtomicInteger(SESSION_MAX);
    for (int i = 0; i < CLIENT_QTY; ++i) {
        futures.add(service.submit(new Callable<Object>() {
            @Override/*from   w ww . j av a  2  s.c  o  m*/
            public Object call() throws Exception {
                CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(),
                        new RetryOneTime(1));
                client.start();
                try {
                    InterProcessSemaphoreV2 semaphore = new InterProcessSemaphoreV2(client, "/test",
                            SESSION_MAX);

                    for (int i = 0; i < LOOP_QTY; ++i) {
                        long start = System.currentTimeMillis();
                        int thisQty;
                        synchronized (available) {
                            if ((System.currentTimeMillis() - start) > 10000) {
                                throw new TimeoutException();
                            }
                            while (available.get() == 0) {
                                available.wait(10000);
                            }

                            thisQty = (available.get() > 1) ? (random.nextInt(available.get()) + 1) : 1;

                            available.addAndGet(-1 * thisQty);
                            Assert.assertTrue(available.get() >= 0);
                        }
                        Collection<Lease> leases = semaphore.acquire(thisQty, 10, TimeUnit.SECONDS);
                        Assert.assertNotNull(leases);
                        try {
                            synchronized (counter) {
                                counter.currentCount += thisQty;
                                if (counter.currentCount > counter.maxCount) {
                                    counter.maxCount = counter.currentCount;
                                }
                            }
                            Thread.sleep(random.nextInt(25));
                        } finally {
                            synchronized (counter) {
                                counter.currentCount -= thisQty;
                            }
                            semaphore.returnAll(leases);
                            synchronized (available) {
                                available.addAndGet(thisQty);
                                available.notifyAll();
                            }
                        }
                    }
                } finally {
                    client.close();
                }
                return null;
            }
        }));
    }

    for (Future<Object> f : futures) {
        f.get();
    }

    synchronized (counter) {
        Assert.assertTrue(counter.currentCount == 0);
        Assert.assertTrue(counter.maxCount > 0);
        Assert.assertTrue(counter.maxCount <= SESSION_MAX);
        System.out.println(counter.maxCount);
    }
}

From source file:org.polymap.core.operation.OperationWizard.java

/**
 * Creastes and opens a new dialog for the given wizard.
 * <p/>//from  ww w .ja  v  a  2  s . c o  m
 * This method blocks until the wizard dialogs OK button is pressed ot dialog
 * is canceled.
 *
 * @param wizard
 * @return True if the OK button of the wizard was pressed.
 */
public static boolean openDialog(final OperationWizard wizard) {
    final AtomicInteger returnCode = new AtomicInteger(-1);

    wizard.getDisplay().asyncExec(new Runnable() {

        public void run() {
            Shell shell = PolymapWorkbench.getShellToParentOn();
            OperationWizardDialog dialog = new OperationWizardDialog(shell, wizard) {
                @Override
                protected void setReturnCode(int code) {
                    super.setReturnCode(code);
                    returnCode.set(code);
                }
            };

            // earlyListeners
            for (Iterator<IPageChangedListener> it = wizard.earlyListeners.iterator(); it.hasNext();) {
                dialog.addPageChangedListener(it.next());
                it.remove();
            }
            dialog.setBlockOnOpen(false);
            dialog.open();

            //dialog.setBlockOnOpen( true );
            //returnCode.set( dialog.open() );
        }
    });

    // wait for dialog to close
    synchronized (returnCode) {
        while (returnCode.get() == -1 && !wizard.monitor.isCanceled()) {
            try {
                returnCode.wait(1000);
            } catch (InterruptedException e) {
            }
        }
    }
    return returnCode.get() == Window.OK;
}