Example usage for java.util.concurrent ArrayBlockingQueue ArrayBlockingQueue

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

Introduction

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

Prototype

public ArrayBlockingQueue(int capacity) 

Source Link

Document

Creates an ArrayBlockingQueue with the given (fixed) capacity and default access policy.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    int capacity = 10;
    ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(capacity);

    for (int i = 0; i < 10; i++) {
        queue.add(i);/*w  w  w . j a v  a 2s . c o  m*/
    }

    System.out.println(queue.poll(10, TimeUnit.MINUTES));
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    List<Integer> list = new ArrayList<Integer>();

    int capacity = 100;
    ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(capacity);
    queue.put(0);/* www.  j av  a2  s  .c o m*/
    queue.put(1);
    queue.put(2);
    queue.drainTo(list, 3);
    System.out.println(queue);
    System.out.println(list);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    List<Integer> list = new ArrayList<Integer>();

    int capacity = 100;
    ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(capacity);
    queue.put(0);//from   w ww . j av  a 2 s  . c  om
    queue.put(1);
    queue.put(2);
    queue.drainTo(list);
    System.out.println(queue);
    System.out.println(list);
}

From source file:FibonacciTest.java

public static void main(String[] args) {

    ArrayBlockingQueue<Integer> queue;
    queue = new ArrayBlockingQueue<Integer>(10);
    new FibonacciProducer(queue);

    int nThreads = Integer.parseInt(args[0]);
    for (int i = 0; i < nThreads; i++)
        new FibonacciConsumer(queue);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    BlockingQueue<Runnable> blockingQueue = new ArrayBlockingQueue<Runnable>(48);
    ThreadPoolExecutor testExecutor = new ThreadPoolExecutor(6, 10, 1, TimeUnit.SECONDS, blockingQueue);
    List<Future<String>> futures = new ArrayList<>();
    for (int i = 0; i < 20; i++) {
        Future<String> testFuture = testExecutor.submit(new MyCallable(i));
        futures.add(testFuture);//  w  w w . j  a  v a2s .com
    }
    for (Future<String> testFuture : futures) {
        System.out.println("Output Returned is : " + testFuture.get());
    }
}

From source file:ReplaceWorker.java

public static void main(String[] args) {
    map.put("key", 1);
    Main test = new Main();
    ThreadPoolExecutor threadPool = new ThreadPoolExecutor(10, 10, 100, TimeUnit.MILLISECONDS,
            new ArrayBlockingQueue<>(1000));
    for (int i = 0; i < 100; i++) {
        threadPool.submit(new MergeWorker());
    }//from  www  .j  av  a 2  s  . c o m
    awaitTermination(threadPool);
    System.out.println(test.map.get("key"));

    map.put("key", 1);
    threadPool = new ThreadPoolExecutor(10, 10, 100, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(1000));
    for (int i = 0; i < 100; i++) {
        threadPool.submit(new ReplaceWorker());
    }
    awaitTermination(threadPool);
    System.out.println(test.map.get("key"));
}

From source file:BlockingQueueTest.java

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.print("Enter base directory (e.g. /usr/local/jdk1.6.0/src): ");
    String directory = in.nextLine();
    System.out.print("Enter keyword (e.g. volatile): ");
    String keyword = in.nextLine();

    final int FILE_QUEUE_SIZE = 10;
    final int SEARCH_THREADS = 100;

    BlockingQueue<File> queue = new ArrayBlockingQueue<File>(FILE_QUEUE_SIZE);

    FileEnumerationTask enumerator = new FileEnumerationTask(queue, new File(directory));
    new Thread(enumerator).start();
    for (int i = 1; i <= SEARCH_THREADS; i++)
        new Thread(new SearchTask(queue, keyword)).start();
}

From source file:org.meresco.lucene.numerate.NumerateHttpServer.java

public static void main(String[] args) throws Exception {
    Options options = new Options();

    Option option = new Option("p", "port", true, "Port number");
    option.setType(Integer.class);
    option.setRequired(true);//from w  w w .  j  ava  2 s  . c  o  m
    options.addOption(option);

    option = new Option("d", "stateDir", true, "Directory in which lucene data is located");
    option.setType(String.class);
    option.setRequired(true);
    options.addOption(option);

    PosixParser parser = new PosixParser();
    CommandLine commandLine = null;
    try {
        commandLine = parser.parse(options, args);
    } catch (MissingOptionException e) {
        HelpFormatter helpFormatter = new HelpFormatter();
        helpFormatter.printHelp("start-numerate-server", options);
        System.exit(1);
    }

    Integer port = new Integer(commandLine.getOptionValue("p"));
    String stateDir = commandLine.getOptionValue("d");

    if (Charset.defaultCharset() != Charset.forName("UTF-8")) {
        System.err.println("file.encoding must be UTF-8.");
        System.exit(1);
    }

    UriEnumerate uriEnumerate = new UriEnumerate(new File(stateDir, "keys-termnumerator").getAbsolutePath());

    ExecutorThreadPool pool = new ExecutorThreadPool(50, 200, 60, TimeUnit.SECONDS,
            new ArrayBlockingQueue<Runnable>(1000));
    Server server = new Server(pool);
    ServerConnector http = new ServerConnector(server, new HttpConnectionFactory());
    http.setPort(port);
    server.addConnector(http);

    registerShutdownHandler(stateDir, uriEnumerate, server);

    server.setHandler(new NumerateHandler(uriEnumerate));
    server.start();
    server.join();
}

From source file:org.meresco.lucene.suggestion.SuggestionHttpServer.java

public static void main(String[] args) throws Exception {
    Options options = new Options();

    Option option = new Option("p", "port", true, "Port number");
    option.setType(Integer.class);
    option.setRequired(true);/*  ww w  .jav a  2s  .c  o m*/
    options.addOption(option);

    option = new Option("d", "stateDir", true, "Directory in which lucene data is located");
    option.setType(String.class);
    option.setRequired(true);
    options.addOption(option);

    PosixParser parser = new PosixParser();
    CommandLine commandLine = null;
    try {
        commandLine = parser.parse(options, args);
    } catch (MissingOptionException e) {
        HelpFormatter helpFormatter = new HelpFormatter();
        helpFormatter.printHelp("start-suggestion-server", options);
        System.exit(1);
    }

    Integer port = new Integer(commandLine.getOptionValue("p"));
    String stateDir = commandLine.getOptionValue("d");

    if (Charset.defaultCharset() != Charset.forName("UTF-8")) {
        System.err.println("file.encoding must be UTF-8.");
        System.exit(1);
    }

    SuggestionIndex suggestionIndex = new SuggestionIndex(stateDir + "/suggestions", stateDir + "/ngram",
            MIN_SHINGLE_SIZE, MAX_SHINGLE_SIZE, COMMIT_COUNT);

    ExecutorThreadPool pool = new ExecutorThreadPool(50, 200, 60, TimeUnit.SECONDS,
            new ArrayBlockingQueue<Runnable>(1000));
    Server server = new Server(pool);
    ServerConnector http = new ServerConnector(server, new HttpConnectionFactory());
    http.setPort(port);
    server.addConnector(http);

    OutOfMemoryShutdown shutdown = new SuggestionShutdown(server, suggestionIndex);
    registerShutdownHandler(shutdown);

    server.setHandler(new SuggestionHandler(suggestionIndex, shutdown));
    server.start();
    server.join();
}

From source file:org.meresco.lucene.http.LuceneHttpServer.java

public static void main(String[] args) throws Exception {
    Options options = new Options();

    Option option = new Option("p", "port", true, "Port number");
    option.setType(Integer.class);
    option.setRequired(true);//ww w .ja  v a 2s .  c  o m
    options.addOption(option);

    option = new Option("d", "stateDir", true, "Directory in which lucene data is located");
    option.setType(String.class);
    option.setRequired(true);
    options.addOption(option);

    option = new Option(null, "core", true, "Lucene core");
    option.setType(String[].class);
    option.setRequired(true);
    options.addOption(option);

    PosixParser parser = new PosixParser();
    CommandLine commandLine = null;
    try {
        commandLine = parser.parse(options, args);
    } catch (MissingOptionException e) {
        HelpFormatter helpFormatter = new HelpFormatter();
        helpFormatter.printHelp("start-lucene-server", options);
        System.exit(1);
    }

    Integer port = new Integer(commandLine.getOptionValue("p"));
    String storeLocation = commandLine.getOptionValue("d");
    String[] cores = commandLine.getOptionValues("core");

    if (Charset.defaultCharset() != Charset.forName("UTF-8")) {
        System.err.println("file.encoding must be UTF-8.");
        System.exit(1);
    }

    TermNumerator termNumerator = new TermNumerator(new File(storeLocation, "keys-termnumerator"));
    ContextHandlerCollection contexts = new ContextHandlerCollection();
    List<Lucene> lucenes = new ArrayList<Lucene>();
    for (String core : cores) {
        Lucene lucene = new Lucene(core, new File(storeLocation, "lucene-" + core));
        lucenes.add(lucene);
    }

    ExecutorThreadPool pool = new ExecutorThreadPool(50, 200, 60, TimeUnit.SECONDS,
            new ArrayBlockingQueue<Runnable>(1000));
    Server server = new Server(pool);
    ServerConnector http = new ServerConnector(server, new HttpConnectionFactory());
    http.setPort(port);
    server.addConnector(http);

    OutOfMemoryShutdown shutdown = new LuceneShutdown(server, lucenes, termNumerator, storeLocation);
    for (Lucene lucene : lucenes) {
        String core = lucene.name;
        ContextHandler context = new ContextHandler("/" + core + "/query");
        context.setHandler(new QueryHandler(lucene, shutdown));
        contexts.addHandler(context);

        context = new ContextHandler("/" + core + "/update");
        context.setHandler(new UpdateHandler(lucene, termNumerator, shutdown));
        contexts.addHandler(context);

        context = new ContextHandler("/" + core + "/delete");
        context.setHandler(new DeleteHandler(lucene, shutdown));
        contexts.addHandler(context);

        context = new ContextHandler("/" + core + "/settings");
        context.setHandler(new SettingsHandler(lucene, shutdown));
        contexts.addHandler(context);

        context = new ContextHandler("/" + core + "/prefixSearch");
        context.setHandler(new PrefixSearchHandler(lucene, shutdown));
        contexts.addHandler(context);

        context = new ContextHandler("/" + core);
        context.setHandler(new OtherHandler(lucene, shutdown));
        contexts.addHandler(context);
    }
    ContextHandler composedQueryHandler = new ContextHandler("/query");
    composedQueryHandler.setHandler(new ComposedQueryHandler(new MultiLucene(lucenes), shutdown));
    contexts.addHandler(composedQueryHandler);

    ContextHandler exportKeysHandler = new ContextHandler("/exportkeys");
    exportKeysHandler.setHandler(new ExportKeysHandler(new MultiLucene(lucenes), shutdown));
    contexts.addHandler(exportKeysHandler);

    ContextHandler numerateHandler = new ContextHandler("/numerate");
    numerateHandler.setHandler(new NumerateHandler(termNumerator, shutdown));
    contexts.addHandler(numerateHandler);

    ContextHandler commitHandler = new ContextHandler("/commit");
    commitHandler.setHandler(new CommitHandler(termNumerator, lucenes, shutdown));
    contexts.addHandler(commitHandler);

    registerShutdownHandler(shutdown);

    server.setHandler(contexts);
    server.start();
    server.join();
}