Example usage for java.util.concurrent ScheduledThreadPoolExecutor schedule

List of usage examples for java.util.concurrent ScheduledThreadPoolExecutor schedule

Introduction

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

Prototype

public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit) 

Source Link

Usage

From source file:Main.java

public static void main(String[] args) {
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(2);
    scheduledThreadPoolExecutor.scheduleAtFixedRate(() -> {

        throw new RuntimeException(" scheduleAtFixedRate test ScheduledThreadPoolExecutor");
    }, 0, 3000, TimeUnit.MILLISECONDS);

    scheduledThreadPoolExecutor.scheduleAtFixedRate(() -> {

        System.out.println("scheduleAtFixedRate: " + LocalDateTime.now().format(formatter));
    }, 0, 2000, TimeUnit.MILLISECONDS);

    scheduledThreadPoolExecutor.schedule(() -> {

        System.out.println("schedule");
    }, 1, TimeUnit.SECONDS);//  ww  w  .  j a  v  a2  s.  co  m
}

From source file:com.l2jfree.util.concurrent.L2ThreadPool.java

public static ScheduledFuture<?> schedule(Runnable r, long delay) {
    r = ExecuteWrapper.wrap(r);// www . j  ava 2 s.co  m
    delay = validate(delay);

    final ScheduledThreadPoolExecutor stpe = getRandomPool(_scheduledPools);
    final ScheduledFuture<?> sf = stpe.schedule(r, delay, TimeUnit.MILLISECONDS);

    return new ScheduledFutureWrapper(sf);
}

From source file:com.fusesource.forge.jmstest.executor.BenchmarkController.java

@Override
synchronized public void stop() {

    coordinator.release();//from   www .  ja va2 s.co m
    super.stop();

    log().info("BenchmarkController going down in 5 Seconds");

    final CountDownLatch brokerStopLatch = new CountDownLatch(1);
    final ScheduledThreadPoolExecutor waiter = new ScheduledThreadPoolExecutor(1);
    waiter.schedule(new Runnable() {
        public void run() {
            brokerStopLatch.countDown();
            waiter.shutdown();
        }
    }, 5, TimeUnit.SECONDS);

    try {
        brokerStopLatch.await();
    } catch (InterruptedException e1) {
    }

    if (broker != null) {
        log().info("Stopping embedded broker for Benchmark framework: ");
        try {
            broker.stop();
        } catch (Exception e) {
            // log().error("Embedded broker could not be stopped.", e);
        }
    }
}

From source file:com.almende.eve.transport.Router.java

@Override
public void connect() throws IOException {
    final Set<Transport> result = new HashSet<Transport>(transports.size());
    for (final Transport transport : transports.values()) {
        result.add(transport);/*w  w w  . j  av a2 s. co m*/
    }
    for (final Transport transport : result) {
        final long[] sleep = new long[1];
        sleep[0] = 1000L;
        final double rnd = Math.random();
        final ScheduledThreadPoolExecutor STE = ThreadPool.getScheduledPool();
        STE.schedule(new Runnable() {
            @Override
            public void run() {
                try {
                    transport.connect();
                } catch (IOException e) {
                    LOG.log(Level.WARNING, "Failed to reconnect agent on new transport.", e);
                    if (sleep[0] <= 80000) {
                        sleep[0] *= 2;
                        STE.schedule(this, (long) (sleep[0] * rnd), TimeUnit.MILLISECONDS);
                    }
                }
            }

        }, (long) (sleep[0] * rnd), TimeUnit.MILLISECONDS);
    }
}

From source file:com.fusesource.forge.jmstest.executor.BenchmarkValueRecorder.java

@Override
protected void createHandlerChain() {
    super.createHandlerChain();

    getConnector().addHandler(new DefaultCommandHandler() {
        public boolean handleCommand(BenchmarkCommand command) {
            switch (command.getCommandType()) {
            case CommandTypes.PREPARE_BENCHMARK:
                PrepareBenchmarkCommand prepCmd = (PrepareBenchmarkCommand) command;
                File benchmarkDir = getBenchmarkWorkDirectory(prepCmd.getBenchmarkConfig().getBenchmarkId());
                try {
                    FileUtils.deleteDirectory(benchmarkDir);
                    benchmarkDir.mkdirs();
                } catch (IOException e) {
                    log().error("Error creating directory : " + benchmarkDir.getAbsolutePath(), e);
                    e.printStackTrace();
                }//from w w  w  . j av  a  2 s  .c  om
                return true;
            case CommandTypes.REPORT_STATS:
                ReportStatsCommand stats = (ReportStatsCommand) command;
                recordStats(stats);
                return true;
            case CommandTypes.END_BENCHMARK:
                EndBenchmarkCommand endCommand = (EndBenchmarkCommand) command;
                final String benchmarkId = endCommand.getBenchmarkId();
                final ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
                executor.schedule(new Runnable() {
                    public void run() {
                        for (BenchmarkPostProcessor processor : getPostProcessors()) {
                            processor.resetStatistics();
                            processor.setWorkDir(getBenchmarkWorkDirectory(benchmarkId));
                            processor.processData();
                        }
                        executor.shutdown();
                    }
                }, 5, TimeUnit.SECONDS);
                return true;
            default:
                return false;
            }
        }
    });
}

From source file:org.testdwr.plain.Test.java

public Verify checkScriptSessionListener(final JavascriptFunction progress1,
        final JavascriptFunction progress2) {
    final ServerContext serverContext = ServerContextFactory.get();
    final String testPage = serverContext.getContextPath() + "/checkSession.html";

    Verify verify = new Verify();

    final int createdBefore = TestScriptSessionListener.created;
    final int createdBefore2 = Test2ScriptSessionListener.created;
    final int destroyedBefore = TestScriptSessionListener.destroyed;
    final int destroyedBefore2 = Test2ScriptSessionListener.destroyed;

    // At least one test window is open ...
    verify.isTrue("createdBefore > 0", createdBefore > 0);
    verify.isTrue("createdBefore2 > 0", createdBefore2 > 0);

    // Open a new window
    Window.open(testPage, "checkSession");

    // We'll fill these in in the first cron, and use them in the second
    final AtomicInteger createdMid = new AtomicInteger();
    final AtomicInteger createdMid2 = new AtomicInteger();
    final AtomicInteger destroyedMid = new AtomicInteger();
    final AtomicInteger destroyedMid2 = new AtomicInteger();

    // Give it a second to open, check counters and close it
    ScheduledThreadPoolExecutor executorService = serverContext.getContainer()
            .getBean(ScheduledThreadPoolExecutor.class);
    executorService.schedule(new Runnable() {
        public void run() {
            createdMid.set(TestScriptSessionListener.created);
            createdMid2.set(Test2ScriptSessionListener.created);
            destroyedMid.set(TestScriptSessionListener.destroyed);
            destroyedMid2.set(Test2ScriptSessionListener.destroyed);

            Verify verify1 = new Verify();
            verify1.isTrue("createdMid > createdBefore", createdMid.intValue() > createdBefore);
            verify1.isTrue("createdMid2 > createdBefore2", createdMid2.intValue() > createdBefore2);
            verify1.equals("destroyedMid == destroyedBefore", destroyedMid.intValue(), destroyedBefore);
            verify1.equals("destroyedMid2 == destroyedBefore2", destroyedMid2.intValue(), destroyedBefore2);

            // Find it and close it
            Browser.withPage(testPage, new Runnable() {
                public void run() {
                    Window.close();
                }/*w ww  .  j a  va  2s  . com*/
            });
            progress1.executeAndClose(verify1);
        }
    }, 1, TimeUnit.SECONDS);

    // Give it 2 seconds to open and be closed then check counters again
    executorService.schedule(new Runnable() {
        public void run() {
            int createdAfter = TestScriptSessionListener.created;
            int createdAfter2 = Test2ScriptSessionListener.created;
            int destroyedAfter = TestScriptSessionListener.destroyed;
            int destroyedAfter2 = Test2ScriptSessionListener.destroyed;

            Verify verify2 = new Verify();
            verify2.equals("createdAfter == createdMid", createdAfter, createdMid.intValue());
            verify2.equals("createdAfter2 == createdMid2", createdAfter2, createdMid2.intValue());
            verify2.isTrue("destroyedAfter > destroyedMid", destroyedAfter > destroyedMid.intValue());
            verify2.isTrue("destroyedAfter2 > destroyedMid2", destroyedAfter2 > destroyedMid2.intValue());

            progress2.executeAndClose(verify2);
        }
    }, 2, TimeUnit.SECONDS);

    return verify;
}