Example usage for java.util.concurrent ScheduledThreadPoolExecutor ScheduledThreadPoolExecutor

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

Introduction

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

Prototype

public ScheduledThreadPoolExecutor(int corePoolSize) 

Source Link

Document

Creates a new ScheduledThreadPoolExecutor with the given core pool size.

Usage

From source file:Main.java

public static void main(String args[]) {
    ScheduledThreadPoolExecutor stpe = new ScheduledThreadPoolExecutor(5);

    stpe.scheduleAtFixedRate(new Job1(), 0, 5, TimeUnit.SECONDS);
    stpe.scheduleAtFixedRate(new Job2(), 1, 2, TimeUnit.SECONDS);
}

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);//  www .  j a v  a  2s  . co m
}

From source file:Test.java

public static void main(String args[]) throws Exception {
    ExecutorService pool = new ScheduledThreadPoolExecutor(3);
    AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(Paths.get("data.txt"),
            EnumSet.of(StandardOpenOption.READ), pool);
    CompletionHandler<Integer, ByteBuffer> handler = new CompletionHandler<Integer, ByteBuffer>() {
        public synchronized void completed(Integer result, ByteBuffer attachment) {
            for (int i = 0; i < attachment.limit(); i++) {
                System.out.print((char) attachment.get(i));
            }//ww  w. j  av a  2 s . c  om
        }

        public void failed(Throwable e, ByteBuffer attachment) {
        }
    };
    final int bufferCount = 5;
    ByteBuffer buffers[] = new ByteBuffer[bufferCount];
    for (int i = 0; i < bufferCount; i++) {
        buffers[i] = ByteBuffer.allocate(10);
        fileChannel.read(buffers[i], i * 10, buffers[i], handler);
    }
    pool.awaitTermination(1, TimeUnit.SECONDS);
    for (ByteBuffer byteBuffer : buffers) {
        for (int i = 0; i < byteBuffer.limit(); i++) {
            System.out.println((char) byteBuffer.get(i));
        }
    }
}

From source file:Test.java

public static void main(String args[]) throws Exception {
    ExecutorService pool = new ScheduledThreadPoolExecutor(3);
    AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(Paths.get("data.txt"),
            EnumSet.of(StandardOpenOption.READ), pool);
    CompletionHandler<Integer, ByteBuffer> handler = new CompletionHandler<Integer, ByteBuffer>() {
        @Override/*from   w w w  .j ava 2s  .co m*/
        public synchronized void completed(Integer result, ByteBuffer attachment) {
            for (int i = 0; i < attachment.limit(); i++) {
                System.out.println((char) attachment.get(i));
            }
        }

        @Override
        public void failed(Throwable e, ByteBuffer attachment) {
        }
    };
    final int bufferCount = 5;
    ByteBuffer buffers[] = new ByteBuffer[bufferCount];
    for (int i = 0; i < bufferCount; i++) {
        buffers[i] = ByteBuffer.allocate(10);
        fileChannel.read(buffers[i], i * 10, buffers[i], handler);
    }
    pool.awaitTermination(1, TimeUnit.SECONDS);
    for (ByteBuffer byteBuffer : buffers) {
        for (int i = 0; i < byteBuffer.limit(); i++) {
            System.out.print((char) byteBuffer.get(i));
        }
    }
}

From source file:Main.java

public static void main(String[] a) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(200, 300);//from w ww  .  j  a  v a  2s.c  o  m
    PaintSurface canvas = new PaintSurface();

    ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(3);
    executor.scheduleAtFixedRate(canvas, 0L, 100L, TimeUnit.MILLISECONDS);

    f.add(canvas);
    f.setVisible(true);
}

From source file:BallRoom.java

public static void main(String[] a) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(200, 300);// w w  w.ja v  a2  s  .  c o  m
    PaintSurface canvas = new PaintSurface();

    ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(3);
    executor.scheduleAtFixedRate(canvas, 0L, 100L, TimeUnit.MILLISECONDS);

    f.getContentPane().add(canvas);
    f.setVisible(true);

}

From source file:com.amazonaws.http.HttpRequestTimer.java

private static ScheduledThreadPoolExecutor createExecutor() {
    ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(5);
    safeSetRemoveOnCancel(executor);//from ww w.ja  va  2 s  .  c  o m
    executor.setKeepAliveTime(5, TimeUnit.SECONDS);
    executor.allowCoreThreadTimeOut(true);
    return executor;
}

From source file:amqp.spring.camel.component.ContrivedLoadTest.java

@Test
public void testSynchronous() throws Exception {
    final int messageCount = 1000;
    int received = 0;
    ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(messageCount);
    List<Future<String>> futures = new ArrayList<Future<String>>();

    long startTime = System.currentTimeMillis();
    for (int i = 0; i < messageCount; ++i)
        futures.add(executorService.submit(new SynchronousRequestor(this.template)));
    LOG.info("Time to submit synchronous messages: {}", (System.currentTimeMillis() - startTime) / 1000.0f);

    startTime = System.currentTimeMillis();
    for (Future<String> future : futures) {
        String response = future.get(10000, TimeUnit.MILLISECONDS);
        if ("RESPONSE".equals(response))
            ++received;// w  w w.j a v  a  2 s  .  com
    }
    float elapsedTime = (System.currentTimeMillis() - startTime) / 1000.0f;
    int maxPoolSize = this.camelContext.getExecutorServiceManager().getDefaultThreadPoolProfile()
            .getMaxPoolSize();
    LOG.info("Time to receive synchronous messages: {}", elapsedTime);

    Assert.assertEquals(messageCount, received);
    //Assuming 1 second delay per message, elapsed time shouldn't exceed the number of messages sent 
    //dividied by the number of messages that can be simultaneously consumed.
    Assert.assertTrue(
            String.format("Possible performance issue: %d messages took %f seconds with %d consumers",
                    messageCount, elapsedTime, maxPoolSize),
            elapsedTime < (messageCount / (double) maxPoolSize) + 1);
}

From source file:org.rifidi.edge.ale.esper.timer.Timer.java

/**
 * Constructor./*from w  ww . j a  va  2s  . com*/
 * 
 * @param startTriggers
 * @param stopTriggers
 * @param esper
 */
public Timer(Set<Trigger> startTriggers, Set<Trigger> stopTriggers, EPServiceProvider esper) {
    logger.debug("New timer created. ");
    executor = new ScheduledThreadPoolExecutor(2);
    EventSender startSender = esper.getEPRuntime().getEventSender("StartEvent");
    EventSender stopSender = esper.getEPRuntime().getEventSender("StopEvent");
    for (Trigger trigger : startTriggers) {
        trigger.setTarget(this);
        trigger.setEventSender(startSender);
        trigger.setStart(true);
    }
    for (Trigger trigger : stopTriggers) {
        trigger.setTarget(this);
        trigger.setEventSender(stopSender);
        trigger.setStart(false);
    }
    for (Trigger trigger : startTriggers) {
        logger.debug("start trigger created: " + trigger.getPeriod() + " ms");
        executor.scheduleAtFixedRate(trigger, trigger.getDelayToNextExec(), trigger.getPeriod(),
                TimeUnit.MILLISECONDS);
    }
    for (Trigger trigger : stopTriggers) {
        logger.debug("stop trigger created: " + trigger.getPeriod() + " ms");
        executor.scheduleAtFixedRate(trigger, trigger.getDelayToNextExec(), trigger.getPeriod(),
                TimeUnit.MILLISECONDS);
    }
}

From source file:org.apache.camel.component.zeromq.ZeroMQConsumer.java

public ZeroMQConsumer(DefaultEndpoint endpoint, Processor processor) {
    super(endpoint, processor);
    this.concurrentConsumers = ((ZeroMQEndpoint) endpoint).getConcurrentConsumers();
    this.tasks = new Task[concurrentConsumers];
    this.executor = new ScheduledThreadPoolExecutor(concurrentConsumers);
}