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

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

Introduction

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

Prototype

@Override
public final Service stopAsync() 

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/* www.  ja  v  a  2 s  . c  o m*/
        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();

}