Example usage for com.google.common.util.concurrent AbstractExecutionThreadService startAsync

List of usage examples for com.google.common.util.concurrent AbstractExecutionThreadService startAsync

Introduction

In this page you can find the example usage for com.google.common.util.concurrent AbstractExecutionThreadService startAsync.

Prototype

@Override
public final Service startAsync() 

Source Link

Usage

From source file:com.github.kroepke.Main.java

public static void main(String[] args) {

    final LinkedBlockingQueue<Long> queue = new LinkedBlockingQueue<>(100);

    Processor processor = new Processor();

    AbstractExecutionThreadService consumer = new Consumer(1, processor, queue);
    AbstractExecutionThreadService consumer1 = new Consumer(2, processor, queue);
    AbstractExecutionThreadService consumer2 = new Consumer(3, processor, queue);

    AbstractExecutionThreadService producer = new Producer(queue);
    AbstractExecutionThreadService waterMarkPrinter = new AbstractExecutionThreadService() {
        @Override//  ww  w  .  j  ava 2  s .c  om
        protected void run() throws Exception {
            while (isRunning()) {
                sleepUninterruptibly(1, SECONDS);
                log.info("Queue watermark {}, producer blocks {}", queue.size(), producerBlocks.get());
            }
        }
    };

    waterMarkPrinter.startAsync().awaitRunning();
    consumer.startAsync().awaitRunning();
    consumer1.startAsync().awaitRunning();
    consumer2.startAsync().awaitRunning();
    producer.startAsync().awaitRunning();

    sleepUninterruptibly(1, MINUTES);

    producer.stopAsync().awaitTerminated();
    consumer.stopAsync().awaitTerminated();
    consumer1.stopAsync().awaitTerminated();
    consumer2.stopAsync().awaitTerminated();
    waterMarkPrinter.stopAsync().awaitTerminated();

}