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:gridool.processors.job.GridJobWorker.java

private R runJob() throws GridException {
    if (job.injectResources()) {
        annotationProc.injectResources(job);
    }//  w w w.  j av  a2 s .c om
    final Map<GridTask, GridNode> mappedTasks = job.map(router, arg);
    if (mappedTasks == null) {
        String errmsg = "Job mapping operation produces no mapped tasks for job: " + job;
        LOG.error(errmsg);
        throw new GridException(errmsg);
    }

    final String jobClassName = ClassUtils.getSimpleClassName(job);
    final String jobId = job.getJobId();
    final int numTasks = mappedTasks.size();
    if (numTasks == 0) {
        if (LOG.isInfoEnabled()) {
            LOG.info(jobClassName + "#map of a job [" + jobId + "] returns an empty result");
        }
        return job.reduce();
    }
    if (job.logJobInfo()) {
        LOG.info(jobClassName + " [" + jobId + "] is mapped to " + numTasks + " tasks (nodes)");
    }
    final TextProgressBar progressBar = new JobProgressBar(jobClassName + " [" + jobId + ']', numTasks);

    final BlockingQueue<GridTaskResult> resultQueue = new ArrayBlockingQueue<GridTaskResult>(numTasks * 5); // REVIEWME 5 times is enough?
    taskResponseQueue.addResponseQueue(jobId, resultQueue);
    final Map<String, Pair<GridTask, List<Future<?>>>> taskMap = new ConcurrentHashMap<String, Pair<GridTask, List<Future<?>>>>(
            numTasks); // taskMap contends rarely

    GridDiscoveryListener nodeFailoverHandler = job.handleNodeFailure()
            ? new GridNodeFailureHandler(mappedTasks, taskMap, resultQueue)
            : new GridDiscoveryFailureLogger();
    discoveryService.addListener(nodeFailoverHandler);

    // assign map tasks
    assignMappedTasks(mappedTasks, taskMap, resultQueue);

    if (job.isAsyncOps()) {
        discoveryService.removeListener(nodeFailoverHandler); // REVIEWME
        return null;
    }

    // collect map task results
    aggregateTaskResults(taskMap, resultQueue, progressBar);
    discoveryService.removeListener(nodeFailoverHandler);

    // invoke reduce
    R result = job.reduce();

    if (!taskMap.isEmpty()) {//TODO REVIEWME
        assert (!job.isAsyncOps());
        cancelRemainingTasks(taskMap);
    }

    progressBar.finish();
    return result;
}

From source file:org.dragoneronca.nlp.wol.disambiguation.Disambiguation.java

@Override
public boolean addConsumer(Consumer<LightSense> consumer) {
    if (!consumersMap.containsKey(consumer)) {
        ArrayBlockingQueue<LightSense> q = new ArrayBlockingQueue<>(MAX_QUEUE_SIZE);
        consumer.setInputQueue(q);/*from w w w . ja  v a  2s .c  o m*/
        consumersMap.put(consumer, q);
        return true;
    }
    return false;
}

From source file:cn.xdf.thinkutils.http2.AsyncHttpClient.java

public AsyncHttpClient() {
    BasicHttpParams httpParams = new BasicHttpParams();

    ConnManagerParams.setTimeout(httpParams, socketTimeout);
    ConnManagerParams.setMaxConnectionsPerRoute(httpParams, new ConnPerRouteBean(maxConnections));
    ConnManagerParams.setMaxTotalConnections(httpParams, DEFAULT_MAX_CONNECTIONS);

    HttpConnectionParams.setSoTimeout(httpParams, socketTimeout);
    HttpConnectionParams.setConnectionTimeout(httpParams, socketTimeout);
    HttpConnectionParams.setTcpNoDelay(httpParams, true);
    HttpConnectionParams.setSocketBufferSize(httpParams, DEFAULT_SOCKET_BUFFER_SIZE);

    HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setUserAgent(httpParams,
            String.format("thinkandroid/%s (http://www.thinkandroid.cn)", VERSION));

    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
    ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(httpParams, schemeRegistry);

    httpContext = new SyncBasicHttpContext(new BasicHttpContext());
    httpClient = new DefaultHttpClient(cm, httpParams);
    httpClient.addRequestInterceptor(new HttpRequestInterceptor() {
        @Override/*from  www . ja v a 2 s  .c  o  m*/
        public void process(HttpRequest request, HttpContext context) {
            if (!request.containsHeader(HEADER_ACCEPT_ENCODING)) {
                request.addHeader(HEADER_ACCEPT_ENCODING, ENCODING_GZIP);
            }
            for (String header : clientHeaderMap.keySet()) {
                request.addHeader(header, clientHeaderMap.get(header));
            }
        }
    });

    httpClient.addResponseInterceptor(new HttpResponseInterceptor() {
        @Override
        public void process(HttpResponse response, HttpContext context) {
            final HttpEntity entity = response.getEntity();
            if (entity == null) {
                return;
            }
            final Header encoding = entity.getContentEncoding();
            if (encoding != null) {
                for (HeaderElement element : encoding.getElements()) {
                    if (element.getName().equalsIgnoreCase(ENCODING_GZIP)) {
                        response.setEntity(new InflatingEntity(response.getEntity()));
                        break;
                    }
                }
            }
        }
    });

    httpClient.setHttpRequestRetryHandler(new RetryHandler(DEFAULT_MAX_RETRIES));

    threadPool = new ThreadPoolExecutor(DEFAULT_CORE_POOL_SIZE, DEFAULT_MAXIMUM_POOL_SIZE,
            DEFAULT_KEEP_ALIVETIME, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(3),
            new ThreadPoolExecutor.CallerRunsPolicy());

    requestMap = new WeakHashMap<Context, List<WeakReference<Future<?>>>>();
    clientHeaderMap = new HashMap<String, String>();
}

From source file:com.attribyte.essem.ApplicationCache.java

ApplicationCache(final AsyncClient client, final RequestOptions requestOptions, final ESEndpoint esEndpoint,
        final Logger logger) {

    this.client = client;
    this.requestOptions = requestOptions;
    this.esEndpoint = esEndpoint;
    this.logger = logger;

    final BlockingQueue<Runnable> requestQueue = new ArrayBlockingQueue<>(4096);
    final Gauge<Integer> requestQueueSize = new Gauge<Integer>() {
        @Override/*  ww  w. jav  a  2s.c  o  m*/
        public Integer getValue() {
            return requestQueue.size();
        }
    };

    final ThreadPoolExecutor requestExecutor = new ThreadPoolExecutor(2, 8, 5L, TimeUnit.MINUTES, requestQueue,
            new ThreadFactoryBuilder().setNameFormat("application-cache-%d").build());
    requestExecutor.prestartAllCoreThreads();

    final Counter rejectedRequests = new Counter();
    requestExecutor.setRejectedExecutionHandler(new RejectedExecutionHandler() {
        @Override
        public void rejectedExecution(final Runnable r, final ThreadPoolExecutor executor) {
            rejectedRequests.inc();
        }
    });
    this.requestExecutor = MoreExecutors
            .listeningDecorator(MoreExecutors.getExitingExecutorService(requestExecutor));

    this.appRequestTimer = new Timer();
    this.appRequestErrors = new Counter();

    this.nameRequestTimer = new Timer();
    this.nameRequestErrors = new Counter();

    this.statsRequestTimer = new Timer();
    this.statsRequestErrors = new Counter();

    Gauge<Integer> appCacheSize = new Gauge<Integer>() {
        @Override
        public Integer getValue() {
            return appCache.size();
        }
    };

    this.metrics = ImmutableMap.<String, com.codahale.metrics.Metric>builder()
            .put("request-queue-size", requestQueueSize).put("rejected-background-requests", rejectedRequests)
            .put("app-requests", appRequestTimer).put("app-request-errors", appRequestErrors)
            .put("name-requests", nameRequestTimer).put("name-request-errors", nameRequestErrors)
            .put("app-cache-size", appCacheSize).put("stats-requests", statsRequestTimer)
            .put("stats-request-errors", statsRequestErrors).build();
}

From source file:ubic.gemma.core.loader.expression.arrayDesign.ArrayDesignProbeMapperServiceImpl.java

@Override
public void processArrayDesign(ArrayDesign arrayDesign, ProbeMapperConfig config, boolean useDB) {

    assert config != null;

    if (arrayDesign.getTechnologyType().equals(TechnologyType.GENELIST)
            || arrayDesign.getTechnologyType().equals(TechnologyType.SEQUENCING)
            || arrayDesign.getTechnologyType().equals(TechnologyType.OTHER)) {
        throw new IllegalArgumentException(
                "Do not use this service to process platforms that do not use an probe-based technology.");
    }//from  w  ww .j  a v a2  s .co m

    Collection<Taxon> taxa = arrayDesignService.getTaxa(arrayDesign.getId());

    Taxon taxon = arrayDesign.getPrimaryTaxon();
    if (taxa.size() > 1 && taxon == null) {
        throw new IllegalArgumentException(
                "Array design has sequence from multiple taxa and has no primary taxon set: " + arrayDesign);
    }

    GoldenPathSequenceAnalysis goldenPathDb = new GoldenPathSequenceAnalysis(taxon);

    BlockingQueue<BACS> persistingQueue = new ArrayBlockingQueue<>(
            ArrayDesignProbeMapperServiceImpl.QUEUE_SIZE);
    AtomicBoolean generatorDone = new AtomicBoolean(false);
    AtomicBoolean loaderDone = new AtomicBoolean(false);

    this.load(persistingQueue, generatorDone, loaderDone, useDB);

    if (useDB) {
        ArrayDesignProbeMapperServiceImpl.log.info("Removing any old associations");
        arrayDesignService.deleteGeneProductAssociations(arrayDesign);
    }

    int count = 0;
    int hits = 0;
    int numWithNoResults = 0;
    ArrayDesignProbeMapperServiceImpl.log
            .info("Start processing " + arrayDesign.getCompositeSequences().size() + " probes ...");
    for (CompositeSequence compositeSequence : arrayDesign.getCompositeSequences()) {

        Map<String, Collection<BlatAssociation>> results = this.processCompositeSequence(config, taxon,
                goldenPathDb, compositeSequence);

        if (results == null) {
            numWithNoResults++;
            continue;
        }

        for (Collection<BlatAssociation> col : results.values()) {
            for (BlatAssociation association : col) {
                if (ArrayDesignProbeMapperServiceImpl.log.isDebugEnabled())
                    ArrayDesignProbeMapperServiceImpl.log.debug(association);
                persistingQueue.add(new BACS(compositeSequence, association));

            }
            ++hits;
        }

        if (++count % 200 == 0) {
            ArrayDesignProbeMapperServiceImpl.log.info("Processed " + count + " composite sequences"
                    + " with blat results; " + hits + " mappings found.");
        }
    }

    generatorDone.set(true);

    ArrayDesignProbeMapperServiceImpl.log.info("Waiting for loading to complete ...");
    while (!loaderDone.get()) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    ArrayDesignProbeMapperServiceImpl.log.info(
            "Processed " + count + " composite sequences with blat results; " + hits + " mappings found.");

    if (numWithNoResults > 0) {
        ArrayDesignProbeMapperServiceImpl.log.info(numWithNoResults + " had no blat results");
    }

    arrayDesignReportService.generateArrayDesignReport(arrayDesign.getId());

    this.deleteOldFiles(arrayDesign);
}

From source file:eu.edisonproject.training.wsd.WikipediaOnline.java

private Map<CharSequence, List<CharSequence>> getCategories(Set<Term> terms)
        throws MalformedURLException, InterruptedException, ExecutionException {
    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());
    Map<CharSequence, List<CharSequence>> cats = new HashMap<>();
    Set<Future<Map<CharSequence, List<CharSequence>>>> set = new HashSet<>();
    for (Term t : terms) {
        URL url = new URL(PAGE + "?action=query&format=json&prop=categories&pageids=" + t.getUid());
        LOGGER.log(Level.FINE, url.toString());
        WikiRequestor req = new WikiRequestor(url, t.getUid().toString(), 0);
        Future<Map<CharSequence, List<CharSequence>>> future = pool.submit(req);
        set.add(future);//from  w w  w.  ja  v a 2 s  . c  o m
    }
    pool.shutdown();

    for (Future<Map<CharSequence, List<CharSequence>>> future : set) {
        while (!future.isDone()) {
            //                LOGGER.log(Level.INFO, "Task is not completed yet....");
            Thread.currentThread().sleep(10);
        }
        Map<CharSequence, List<CharSequence>> c = future.get();
        if (c != null) {
            cats.putAll(c);
        }
    }

    return cats;
}

From source file:com.lxh.util.http.LXH_HttpClient.java

public LXH_HttpClient() {
    BasicHttpParams httpParams = new BasicHttpParams();

    ConnManagerParams.setTimeout(httpParams, socketTimeout);
    ConnManagerParams.setMaxConnectionsPerRoute(httpParams, new ConnPerRouteBean(maxConnections));
    ConnManagerParams.setMaxTotalConnections(httpParams, DEFAULT_MAX_CONNECTIONS);

    HttpConnectionParams.setSoTimeout(httpParams, socketTimeout);
    HttpConnectionParams.setConnectionTimeout(httpParams, socketTimeout);
    HttpConnectionParams.setTcpNoDelay(httpParams, true);
    HttpConnectionParams.setSocketBufferSize(httpParams, DEFAULT_SOCKET_BUFFER_SIZE);

    HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setUserAgent(httpParams,
            String.format("thinkandroid/%s (http://www.thinkandroid.cn)", VERSION));

    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
    ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(httpParams, schemeRegistry);

    httpContext = new SyncBasicHttpContext(new BasicHttpContext());
    httpClient = new DefaultHttpClient(cm, httpParams);
    httpClient.addRequestInterceptor(new HttpRequestInterceptor() {
        @Override//from  ww w  .j  a  v a2  s. c  o m
        public void process(HttpRequest request, HttpContext context) {
            if (!request.containsHeader(HEADER_ACCEPT_ENCODING)) {
                request.addHeader(HEADER_ACCEPT_ENCODING, ENCODING_GZIP);
            }
            for (String header : clientHeaderMap.keySet()) {
                request.addHeader(header, clientHeaderMap.get(header));
            }
        }
    });

    httpClient.addResponseInterceptor(new HttpResponseInterceptor() {
        @Override
        public void process(HttpResponse response, HttpContext context) {
            final HttpEntity entity = response.getEntity();
            if (entity == null) {
                return;
            }
            final Header encoding = entity.getContentEncoding();
            if (encoding != null) {
                for (HeaderElement element : encoding.getElements()) {
                    if (element.getName().equalsIgnoreCase(ENCODING_GZIP)) {
                        response.setEntity(new InflatingEntity(response.getEntity()));
                        break;
                    }
                }
            }
        }
    });

    httpClient.setHttpRequestRetryHandler(new RetryHandler(DEFAULT_MAX_RETRIES));

    threadPool = new ThreadPoolExecutor(DEFAULT_CORE_POOL_SIZE, DEFAULT_MAXIMUM_POOL_SIZE,
            DEFAULT_KEEP_ALIVETIME, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(3),
            new ThreadPoolExecutor.CallerRunsPolicy());

    requestMap = new WeakHashMap<Context, List<WeakReference<Future<?>>>>();
    clientHeaderMap = new HashMap<String, String>();
}

From source file:ea.compoment.http.BaseAsyncHttpClient.java

public BaseAsyncHttpClient() {
    BasicHttpParams httpParams = new BasicHttpParams();

    ConnManagerParams.setTimeout(httpParams, socketTimeout);
    ConnManagerParams.setMaxConnectionsPerRoute(httpParams, new ConnPerRouteBean(maxConnections));
    ConnManagerParams.setMaxTotalConnections(httpParams, maxConnections);

    HttpConnectionParams.setSoTimeout(httpParams, socketTimeout);
    HttpConnectionParams.setConnectionTimeout(httpParams, socketTimeout);
    HttpConnectionParams.setTcpNoDelay(httpParams, true);
    HttpConnectionParams.setSocketBufferSize(httpParams, DEFAULT_SOCKET_BUFFER_SIZE);

    HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
    //      HttpProtocolParams.setUserAgent(httpParams, String.format(
    //            "thinkandroid/%s (http://www.thinkandroid.cn)", VERSION));

    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
    ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(httpParams, schemeRegistry);

    httpContext = new SyncBasicHttpContext(new BasicHttpContext());
    httpClient = new DefaultHttpClient(cm, httpParams);
    httpClient.addRequestInterceptor(new HttpRequestInterceptor() {
        @Override//ww  w. ja  v a  2s  .  c  om
        public void process(HttpRequest request, HttpContext context) {
            if (!request.containsHeader(HEADER_ACCEPT_ENCODING)) {
                request.addHeader(HEADER_ACCEPT_ENCODING, ENCODING_GZIP);
            }
            for (String header : clientHeaderMap.keySet()) {
                request.addHeader(header, clientHeaderMap.get(header));
            }
        }
    });

    httpClient.addResponseInterceptor(new HttpResponseInterceptor() {
        @Override
        public void process(HttpResponse response, HttpContext context) {
            final HttpEntity entity = response.getEntity();
            if (entity == null) {
                return;
            }
            final Header encoding = entity.getContentEncoding();
            if (encoding != null) {
                for (HeaderElement element : encoding.getElements()) {
                    if (element.getName().equalsIgnoreCase(ENCODING_GZIP)) {
                        response.setEntity(new InflatingEntity(response.getEntity()));
                        break;
                    }
                }
            }
        }
    });

    httpClient.setHttpRequestRetryHandler(new RetryHandler(DEFAULT_MAX_RETRIES));

    threadPool = new ThreadPoolExecutor(DEFAULT_CORE_POOL_SIZE, DEFAULT_MAXIMUM_POOL_SIZE,
            DEFAULT_KEEP_ALIVETIME, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(3),
            new ThreadPoolExecutor.CallerRunsPolicy());

    requestMap = new WeakHashMap<Context, List<WeakReference<Future<?>>>>();
    clientHeaderMap = new HashMap<String, String>();
}

From source file:com.reactivetechnologies.analytics.core.IncrementalClassifierBean.java

/**
 * /* w  ww .j av a 2 s . com*/
 * @param c Base classifier
 * @param size size of blocking queue
 */
public IncrementalClassifierBean(Classifier c, int size) {
    if (!(c instanceof Classifier))
        throw new IllegalArgumentException("Not an instance of Classifier");
    clazzifier = c;
    queue = new ArrayBlockingQueue<>(size);

}

From source file:org.msec.sink.es.ESSink.java

private void initESThreadPool() {
    serverAddresses = new InetSocketTransportAddress[serverAddressStrings.length];
    workingQueue = new ArrayBlockingQueue<ESClientThread.ESThreadRequest>(serverAddressStrings.length * 2);

    for (int i = 0; i < serverAddressStrings.length; i++) {
        String[] hostPort = serverAddressStrings[i].trim().split(":");
        String host = hostPort[0].trim();
        int port = hostPort.length == 2 ? Integer.parseInt(hostPort[1].trim()) : DEFAULT_PORT;
        try {//from w w w.ja v  a 2s. c  o m
            serverAddresses[i] = new InetSocketTransportAddress(InetAddress.getByName(host), port);

            threadPool.submit(new ESClientThread(workingQueue, clusterName, serverAddresses[i]));
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
}