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:eu.edisonproject.training.wsd.Wikidata.java

private Map<CharSequence, List<CharSequence>> getbroaderIDS(Set<Term> terms)
        throws MalformedURLException, InterruptedException, ExecutionException {
    Map<CharSequence, List<CharSequence>> map = new HashMap<>();
    if (terms.size() > 0) {
        int maxT = 2;
        BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue(maxT);
        ExecutorService pool = new ThreadPoolExecutor(maxT, maxT, 500L, TimeUnit.MICROSECONDS, workQueue);
        //            ExecutorService pool = new ThreadPoolExecutor(maxT, maxT, 5000L, TimeUnit.MILLISECONDS,  new ArrayBlockingQueue<>(maxT, true), new ThreadPoolExecutor.CallerRunsPolicy());

        Set<Future<Map<CharSequence, List<CharSequence>>>> set1 = new HashSet<>();
        String prop = "P31";
        for (Term t : terms) {
            URL url = new URL(
                    PAGE + "?action=wbgetclaims&format=json&props=&property=" + prop + "&entity=" + t.getUid());
            Logger.getLogger(Wikidata.class.getName()).log(Level.FINE, url.toString());
            WikiRequestor req = new WikiRequestor(url, t.getUid().toString(), 1);
            Future<Map<CharSequence, List<CharSequence>>> future = pool.submit(req);
            set1.add(future);/*from  ww  w  . j  a va2s . c  o m*/
        }
        pool.shutdown();

        for (Future<Map<CharSequence, List<CharSequence>>> future : set1) {
            while (!future.isDone()) {
                //                Logger.getLogger(Wikipedia.class.getName()).log(Level.INFO, "Task is not completed yet....");
                Thread.currentThread().sleep(10);
            }
            Map<CharSequence, List<CharSequence>> c = future.get();
            if (c != null) {
                map.putAll(c);
            }
        }
    }

    return map;
}

From source file:org.codice.ddf.commands.catalog.DumpCommand.java

@Override
protected Object executeWithSubject() throws Exception {
    final File dumpDir = new File(dirPath);

    if (!dumpDir.exists()) {
        printErrorMessage("Directory [" + dirPath + "] must exist.");
        console.println("If the directory does indeed exist, try putting the path in quotes.");
        return null;
    }/*from w  w w . j  av a 2  s . c o m*/

    if (!dumpDir.isDirectory()) {
        printErrorMessage("Path [" + dirPath + "] must be a directory.");
        return null;
    }

    if (!DEFAULT_TRANSFORMER_ID.matches(transformerId)) {
        transformers = getTransformers();
        if (transformers == null) {
            console.println(transformerId + " is an invalid metacard transformer.");
            return null;
        }
    }

    CatalogFacade catalog = getCatalog();
    FilterBuilder builder = getFilterBuilder();

    Filter createdFilter = null;
    if ((createdAfter != null) && (createdBefore != null)) {
        DateTime createStartDateTime = DateTime.parse(createdAfter);
        DateTime createEndDateTime = DateTime.parse(createdBefore);
        createdFilter = builder.attribute(Metacard.CREATED).is().during().dates(createStartDateTime.toDate(),
                createEndDateTime.toDate());
    } else if (createdAfter != null) {
        DateTime createStartDateTime = DateTime.parse(createdAfter);
        createdFilter = builder.attribute(Metacard.CREATED).is().after().date(createStartDateTime.toDate());
    } else if (createdBefore != null) {
        DateTime createEndDateTime = DateTime.parse(createdBefore);
        createdFilter = builder.attribute(Metacard.CREATED).is().before().date(createEndDateTime.toDate());
    }

    Filter modifiedFilter = null;
    if ((modifiedAfter != null) && (modifiedBefore != null)) {
        DateTime modifiedStartDateTime = DateTime.parse(modifiedAfter);
        DateTime modifiedEndDateTime = DateTime.parse(modifiedBefore);
        modifiedFilter = builder.attribute(Metacard.MODIFIED).is().during()
                .dates(modifiedStartDateTime.toDate(), modifiedEndDateTime.toDate());
    } else if (modifiedAfter != null) {
        DateTime modifiedStartDateTime = DateTime.parse(modifiedAfter);
        modifiedFilter = builder.attribute(Metacard.MODIFIED).is().after().date(modifiedStartDateTime.toDate());
    } else if (modifiedBefore != null) {
        DateTime modifiedEndDateTime = DateTime.parse(modifiedBefore);
        modifiedFilter = builder.attribute(Metacard.MODIFIED).is().before().date(modifiedEndDateTime.toDate());
    }

    Filter filter = null;
    if ((createdFilter != null) && (modifiedFilter != null)) {
        // Filter by both created and modified dates
        filter = builder.allOf(createdFilter, modifiedFilter);
    } else if (createdFilter != null) {
        // Only filter by created date
        filter = createdFilter;
    } else if (modifiedFilter != null) {
        // Only filter by modified date
        filter = modifiedFilter;
    } else {
        // Don't filter by date range
        filter = builder.attribute(Metacard.ID).is().like().text(WILDCARD);
    }

    if (cqlFilter != null) {
        filter = CQL.toFilter(cqlFilter);
    }

    QueryImpl query = new QueryImpl(filter);
    query.setRequestsTotalResultsCount(false);
    query.setPageSize(pageSize);

    Map<String, Serializable> props = new HashMap<String, Serializable>();
    // Avoid caching all results while dumping with native query mode
    props.put("mode", "native");

    final AtomicLong resultCount = new AtomicLong(0);
    long start = System.currentTimeMillis();

    SourceResponse response = catalog.query(new QueryRequestImpl(query, props));

    BlockingQueue<Runnable> blockingQueue = new ArrayBlockingQueue<Runnable>(multithreaded);
    RejectedExecutionHandler rejectedExecutionHandler = new ThreadPoolExecutor.CallerRunsPolicy();
    final ExecutorService executorService = new ThreadPoolExecutor(multithreaded, multithreaded, 0L,
            TimeUnit.MILLISECONDS, blockingQueue, rejectedExecutionHandler);

    while (response.getResults().size() > 0) {
        response = catalog.query(new QueryRequestImpl(query, props));

        if (multithreaded > 1) {
            final List<Result> results = new ArrayList<Result>(response.getResults());
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    boolean transformationFailed = false;
                    for (final Result result : results) {
                        Metacard metacard = result.getMetacard();
                        try {
                            exportMetacard(dumpDir, metacard);
                        } catch (IOException | CatalogTransformerException e) {
                            transformationFailed = true;
                            LOGGER.debug("Failed to dump metacard {}", metacard.getId(), e);
                            executorService.shutdownNow();
                        }
                        printStatus(resultCount.incrementAndGet());
                    }
                    if (transformationFailed) {
                        LOGGER.error(
                                "One or more metacards failed to transform. Enable debug log for more details.");
                    }
                }
            });
        } else {
            for (final Result result : response.getResults()) {
                Metacard metacard = result.getMetacard();
                exportMetacard(dumpDir, metacard);
                printStatus(resultCount.incrementAndGet());
            }
        }

        if (response.getResults().size() < pageSize || pageSize == -1) {
            break;
        }

        if (pageSize > 0) {
            query.setStartIndex(query.getStartIndex() + pageSize);
        }
    }

    executorService.shutdown();

    while (!executorService.isTerminated()) {
        try {
            TimeUnit.MILLISECONDS.sleep(100);
        } catch (InterruptedException e) {
            // ignore
        }
    }

    long end = System.currentTimeMillis();
    String elapsedTime = timeFormatter.print(new Period(start, end).withMillis(0));
    console.printf(" %d file(s) dumped in %s\t%n", resultCount.get(), elapsedTime);
    LOGGER.info("{} file(s) dumped in {}", resultCount.get(), elapsedTime);
    console.println();

    return null;
}

From source file:com.amazon.mws.shared.MwsConnection.java

/**
 * Get the shared executor service that is used by async calls if no
 * executor is supplied./* ww w  .ja va2s .  c  om*/
 * 
 * @return The shared executor service.
 */
private ExecutorService getSharedES() {
    synchronized (this.getClass()) {
        if (sharedES != null) {
            return sharedES;
        }
        sharedES = new ThreadPoolExecutor(maxAsyncThreads / 10, maxAsyncThreads, 60L, TimeUnit.SECONDS,
                new ArrayBlockingQueue<Runnable>(maxAsyncQueueSize), new ThreadFactory() {
                    private final AtomicInteger threadNumber = new AtomicInteger(1);

                    public Thread newThread(Runnable task) {
                        Thread thread = new Thread(task, "MWSClient-" + threadNumber.getAndIncrement());
                        thread.setDaemon(true);
                        thread.setPriority(Thread.NORM_PRIORITY);
                        return thread;
                    }
                }, new RejectedExecutionHandler() {
                    public void rejectedExecution(Runnable task, ThreadPoolExecutor executor) {
                        if (!executor.isShutdown()) {
                            log.warn("MWSClient async queue full, running on calling thread.");
                            task.run();
                        } else {
                            throw new RejectedExecutionException();
                        }
                    }
                });
        return sharedES;
    }
}

From source file:com.codefollower.lealone.omid.client.TSOClient.java

public TSOClient(Configuration conf) throws IOException {
    state = State.DISCONNECTED;//from   w w w  . j a v  a  2s.  co  m
    queuedOps = new ArrayBlockingQueue<Op>(200);
    retryTimer = new Timer(true);

    commitCallbacks = Collections.synchronizedMap(new HashMap<Long, CommitCallback>());
    isCommittedCallbacks = Collections.synchronizedMap(new HashMap<Long, List<CommitQueryCallback>>());
    createCallbacks = new ConcurrentLinkedQueue<CreateCallback>();
    channel = null;

    LOG.info("Starting TSOClient");

    // Start client with Nb of active threads = 3 as maximum.
    factory = new NioClientSocketChannelFactory(Executors.newCachedThreadPool(),
            Executors.newCachedThreadPool(), 3);
    // Create the bootstrap
    bootstrap = new ClientBootstrap(factory);

    int executorThreads = conf.getInt("tso.executor.threads", 3);

    bootstrap.getPipeline().addLast("executor", new ExecutionHandler(
            new OrderedMemoryAwareThreadPoolExecutor(executorThreads, 1024 * 1024, 4 * 1024 * 1024)));
    bootstrap.getPipeline().addLast("handler", this);
    bootstrap.setOption("tcpNoDelay", false);
    bootstrap.setOption("keepAlive", true);
    bootstrap.setOption("reuseAddress", true);
    bootstrap.setOption("connectTimeoutMillis", 100);

    String host = conf.get("tso.host");
    int port = conf.getInt("tso.port", 1234);
    maxRetries = conf.getInt("tso.max_retries", 100);
    retryDelayMs = conf.getInt("tso.retry_delay_ms", 1000);

    if (host == null) {
        throw new IOException("tso.host missing from configuration");
    }

    addr = new InetSocketAddress(host, port);
    connectIfNeeded();
}

From source file:com.amazonservices.mws.client.MwsConnection.java

/**
 * Get the shared executor service that is used by async calls if no
 * executor is supplied./*ww  w  . j  ava 2 s . co m*/
 * 
 * @return The shared executor service.
 */
private ExecutorService getSharedES() {
    synchronized (this.getClass()) {
        if (sharedES != null) {
            return sharedES;
        }
        sharedES = new ThreadPoolExecutor(maxAsyncThreads / 10, maxAsyncThreads, 60L, TimeUnit.SECONDS,
                new ArrayBlockingQueue<Runnable>(maxAsyncQueueSize), new ThreadFactory() {
                    private final AtomicInteger threadNumber = new AtomicInteger(1);

                    //@Override
                    public Thread newThread(Runnable task) {
                        Thread thread = new Thread(task, "MWSClient-" + threadNumber.getAndIncrement());
                        thread.setDaemon(true);
                        thread.setPriority(Thread.NORM_PRIORITY);
                        return thread;
                    }
                }, new RejectedExecutionHandler() {
                    //@Override
                    public void rejectedExecution(Runnable task, ThreadPoolExecutor executor) {
                        if (!executor.isShutdown()) {
                            log.warn("MWSClient async queue full, running on calling thread.");
                            task.run();
                        } else {
                            throw new RejectedExecutionException();
                        }
                    }
                });
        return sharedES;
    }
}

From source file:microsoft.exchange.webservices.data.core.request.HangingServiceRequestBase.java

/**
 * Perform any bookkeeping needed when we connect
 * @throws XMLStreamException the XML stream exception
 *///from w ww.ja va 2  s  .  com
private void internalOnConnect() throws XMLStreamException, IOException, EWSHttpException {
    if (!this.isConnected()) {
        this.isConnected = true;

        if (this.getService().isTraceEnabledFor(TraceFlags.EwsResponseHttpHeaders)) {
            // Trace Http headers
            this.getService().processHttpResponseHeaders(TraceFlags.EwsResponseHttpHeaders, this.response);
        }
        int poolSize = 1;

        int maxPoolSize = 1;

        long keepAliveTime = 10;

        final ArrayBlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(1);
        ThreadPoolExecutor threadPool = new ThreadPoolExecutor(poolSize, maxPoolSize, keepAliveTime,
                TimeUnit.SECONDS, queue);
        threadPool.execute(new Runnable() {
            public void run() {
                parseResponses();
            }
        });
        threadPool.shutdown();
    }
}

From source file:org.apache.activemq.usecases.RequestReplyToTopicViaThreeNetworkHopsTest.java

@Test
public void runWithTempTopicReplyTo() throws Exception {
    EchoService echo_svc;//  w w w.  j  ava  2  s . co  m
    TopicTrafficGenerator traffic_gen;
    Thread start1;
    Thread start2;
    Thread start3;
    Thread start4;
    ThreadPoolExecutor clientExecPool;
    final CountDownLatch clientCompletionLatch;
    int iter;

    fatalTestError = false;
    testError = false;

    //
    // Execute up to 20 clients at a time to simulate that load.
    //

    clientExecPool = new ThreadPoolExecutor(CONCURRENT_CLIENT_COUNT, CONCURRENT_CLIENT_COUNT, 0,
            TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10000));
    clientCompletionLatch = new CountDownLatch(TOTAL_CLIENT_ITER);

    // Use threads to avoid startup deadlock since the first broker started waits until
    // it knows the name of the remote broker before finishing its startup, which means
    // the remote must already be running.

    start1 = new Thread() {
        @Override
        public void run() {
            try {
                edge1.start();
            } catch (Exception ex) {
                LOG.error(null, ex);
            }
        }
    };

    start2 = new Thread() {
        @Override
        public void run() {
            try {
                edge2.start();
            } catch (Exception ex) {
                LOG.error(null, ex);
            }
        }
    };

    start3 = new Thread() {
        @Override
        public void run() {
            try {
                core1.start();
            } catch (Exception ex) {
                LOG.error(null, ex);
            }
        }
    };

    start4 = new Thread() {
        @Override
        public void run() {
            try {
                core2.start();
            } catch (Exception ex) {
                LOG.error(null, ex);
            }
        }
    };

    start1.start();
    start2.start();
    start3.start();
    start4.start();

    start1.join();
    start2.join();
    start3.join();
    start4.join();

    traffic_gen = new TopicTrafficGenerator(edge1.getConnectionUrl(), edge2.getConnectionUrl());
    traffic_gen.start();

    //
    // Now start the echo service with that queue.
    //
    echo_svc = new EchoService("echo", edge1.getConnectionUrl());
    echo_svc.start();

    //
    // Run the tests on Temp Topics.
    //

    LOG.info("** STARTING TEMP TOPIC TESTS");
    iter = 0;
    while ((iter < TOTAL_CLIENT_ITER) && (!fatalTestError)) {
        clientExecPool.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    RequestReplyToTopicViaThreeNetworkHopsTest.this.testTempTopic(edge1.getConnectionUrl(),
                            edge2.getConnectionUrl());
                } catch (Exception exc) {
                    LOG.error("test exception", exc);
                    fatalTestError = true;
                    testError = true;
                }

                clientCompletionLatch.countDown();
            }
        });

        iter++;
    }

    boolean allDoneOnTime = clientCompletionLatch.await(20, TimeUnit.MINUTES);

    LOG.info("** FINISHED TEMP TOPIC TESTS AFTER " + iter + " ITERATIONS, testError:" + testError + ", fatal: "
            + fatalTestError + ", onTime:" + allDoneOnTime);

    Thread.sleep(100);

    echo_svc.shutdown();
    traffic_gen.shutdown();

    shutdown();

    assertTrue("test completed in time", allDoneOnTime);
    assertTrue("no errors", !testError);
}

From source file:nl.uva.sne.disambiguators.Wikidata.java

private Map<String, List<String>> getCategories(Set<Term> terms)
        throws MalformedURLException, InterruptedException, ExecutionException {
    Map<String, List<String>> cats = new HashMap<>();

    if (terms.size() > 0) {
        int maxT = 2;
        BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue(maxT);
        ExecutorService pool = new ThreadPoolExecutor(maxT, maxT, 500L, TimeUnit.MICROSECONDS, workQueue);

        //            ExecutorService pool = new ThreadPoolExecutor(maxT, maxT,
        //                    5000L, TimeUnit.MILLISECONDS,
        //                    new ArrayBlockingQueue<>(maxT, true), new ThreadPoolExecutor.CallerRunsPolicy());
        Set<Future<Map<String, List<String>>>> set1 = new HashSet<>();
        String prop = "P910";
        for (Term t : terms) {
            URL url = new URL(
                    page + "?action=wbgetclaims&format=json&props=&property=" + prop + "&entity=" + t.getUID());
            System.err.println(url);
            WikiRequestor req = new WikiRequestor(url, t.getUID(), 1);
            Future<Map<String, List<String>>> future = pool.submit(req);
            set1.add(future);//  w  w w. ja va2s .  c om
        }
        pool.shutdown();

        Map<String, List<String>> map = new HashMap<>();
        for (Future<Map<String, List<String>>> future : set1) {
            while (!future.isDone()) {
                //                Logger.getLogger(Wikipedia.class.getName()).log(Level.INFO, "Task is not completed yet....");
                Thread.currentThread().sleep(10);
            }
            Map<String, List<String>> c = future.get();
            if (c != null) {
                map.putAll(c);
            }
        }
        workQueue = new ArrayBlockingQueue(maxT);
        pool = new ThreadPoolExecutor(maxT, maxT, 500L, TimeUnit.MICROSECONDS, workQueue);

        //            pool = new ThreadPoolExecutor(maxT, maxT,
        //                    5000L, TimeUnit.MILLISECONDS,
        //                    new ArrayBlockingQueue<>(maxT, true), new ThreadPoolExecutor.CallerRunsPolicy());
        Set<Future<Map<String, List<String>>>> set2 = new HashSet<>();
        for (Term t : terms) {
            List<String> catIDs = map.get(t.getUID());
            for (String catID : catIDs) {
                URL url = new URL(
                        page + "?action=wbgetentities&format=json&props=labels&languages=en&ids=" + catID);
                System.err.println(url);
                WikiRequestor req = new WikiRequestor(url, t.getUID(), 2);
                Future<Map<String, List<String>>> future = pool.submit(req);
                set2.add(future);
            }
        }
        pool.shutdown();

        for (Future<Map<String, List<String>>> future : set2) {
            while (!future.isDone()) {
                //                Logger.getLogger(Wikipedia.class.getName()).log(Level.INFO, "Task is not completed yet....");
                Thread.currentThread().sleep(10);
            }
            Map<String, List<String>> c = future.get();
            if (c != null) {
                cats.putAll(c);
            }
        }
    }

    return cats;
}

From source file:com.xerox.amazonws.sdb.Domain.java

public ThreadPoolExecutor getThreadPoolExecutor() {
    if (executor != null) {
        return executor;
    } else {//  ww w.ja va 2  s .  c om
        return new ThreadPoolExecutor(maxThreads, maxThreads, 5, TimeUnit.SECONDS,
                new ArrayBlockingQueue(maxThreads));
    }
}

From source file:com.mchp.android.PIC32_BTSK.PIC32_BTSK.java

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    if (D)/*  ww  w.  j  av  a  2  s .  c om*/
        Log.e(TAG, "+++ ON CREATE +++");

    // delegated in Android 5.0
    // Request the Action Bar.
    //        requestWindowFeature(Window.FEATURE_ACTION_BAR);

    // Set up the action bar
    //           actionBar = getSupportActionBar();
    //           actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    //           actionBar.setHomeButtonEnabled(true);

    // Get the window layout
    setContentView(R.layout.main);

    // Use toolbar instead of action bar
    toolbar = (Toolbar) findViewById(R.id.my_toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setIcon(R.drawable.app_icon);

    // Get local Bluetooth adapter
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    // If the adapter is null, then Bluetooth is not supported on this device. Exit the app.
    if (mBluetoothAdapter == null) {
        Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
        finish();
        return;
    }

    // Initialize the temperature log
    mTemperatures = new LinkedList<Integer>();
    TemperatureFragment.initTemperatureList(mTemperatures, maxCount);

    // Set up the tab interface. Add one tab for each of the three fragments.
    mFragmentManager = this.getSupportFragmentManager();

    mTabHost = (FragmentTabHost) findViewById(R.id.tabhost);
    mTabHost.setup(this, mFragmentManager, R.id.tabFrameLayout);

    mTabHost.addTab(mTabHost.newTabSpec("color").setIndicator("Color"), ColorFragment.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("temperature").setIndicator("Temperature"), TemperatureFragment.class,
            null);
    mTabHost.addTab(mTabHost.newTabSpec("text").setIndicator("Text"), TextFragment.class, null);
    mTabHost.setOnTabChangedListener(this);

    // Set up the send delay count down timer. While it's counting down, no messages can be sent over Bluetooth.
    mSendTimer = new CountDownTimer(mSendDelay, mSendDelay) {
        public void onTick(long millisUntilFinished) {
        }

        public void onFinish() {
            mSendTimeElapsed = true;
            // Send the last color
            if (mLastColor != null) {
                sendMessage(mLastColor);
                mLastColor = null;
                return;
            }
        }
    };

    // Get the input method manager for hiding the soft keyboard
    mgr = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);

    // Setup toasts
    mToast = Toast.makeText(this, "", Toast.LENGTH_SHORT);

    BTtoSocketThreadQueue = new ArrayBlockingQueue<String>(5);
    socketThreadToBtQueue = new ArrayBlockingQueue<String>(5);
}