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:org.sapia.soto.util.concurrent.ExecutorService.java

/**
 * Internal factory method that creates the executor instance. Subclass may override this method to
 * change the actual executor implementation used.
 *
 * @param aThreadFactory The thread factory to use for the executor.
 * @return The created thead pool executor instance.
 *///from   ww w. j av a 2  s.c  o m
protected ThreadPoolExecutor createExecutor(ThreadFactory aThreadFactory) {
    // Create the queue of pending tasks
    BlockingQueue queue;
    if (_taskQueueSize == 0) {
        queue = new SynchronousQueue();
    } else {
        queue = new ArrayBlockingQueue(_taskQueueSize);
    }

    return new ThreadPoolExecutor(_coreThreadPoolSize, _maximumThreadPoolSize, _threadKeepAliveTime,
            TimeUnit.MILLISECONDS, queue, aThreadFactory, _rejectedExecutionHandler);
}

From source file:ubic.gemma.core.loader.protein.StringProteinInteractionLoader.java

/**
 * Method to generate and load Gene2GeneProteinAssociation one taxon at a time
 *
 * @param ensembl2ncbi                Map of peptide ids to NCBI gene ids
 * @param proteinInteractionsOneTaxon The protein interactions representing one taxon
 *///w ww . j av a  2  s. c  o  m
@SuppressWarnings({ "unused", "WeakerAccess" }) // Possible external use
public void loadOneTaxonAtATime(Map<String, Ensembl2NcbiValueObject> ensembl2ncbi,
        Collection<StringProteinProteinInteraction> proteinInteractionsOneTaxon) {
    long startTime = System.currentTimeMillis();
    converterDone.set(false);
    loaderDone.set(false);
    loadedGeneCount = 0;
    // generate gemma objects
    StringProteinProteinInteractionConverter converter = new StringProteinProteinInteractionConverter(
            ensembl2ncbi);
    converter.setStringExternalDatabase(this.getExternalDatabaseForString());

    // create queue for String objects to be converted
    final BlockingQueue<Gene2GeneProteinAssociation> gene2GeneProteinAssociationQueue = new ArrayBlockingQueue<>(
            StringProteinInteractionLoader.QUEUE_SIZE);
    converter.setProducerDoneFlag(converterDone);
    converter.convert(gene2GeneProteinAssociationQueue, proteinInteractionsOneTaxon);

    // Threaded consumer. Consumes Gene objects and persists them into the database
    this.load(gene2GeneProteinAssociationQueue);
    StringProteinInteractionLoader.log.debug("Time taken to load data in minutes is "
            + (((System.currentTimeMillis() / 1000) - (startTime) / 1000)) / 60);

}

From source file:org.apache.pig.impl.builtin.StreamingUDF.java

private void initialize() throws ExecException, IOException {
    inputQueue = new ArrayBlockingQueue<Tuple>(1);
    outputQueue = new ArrayBlockingQueue<Object>(2);
    soc = new ScriptingOutputCapturer(execType);
    startUdfController();/*from   www  . java 2 s .  co m*/
    createInputHandlers();
    setStreams();
    startThreads();
}

From source file:com.vc.net.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("uroad-android-httpclient/%s (http://www.u-road.com/)", 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//  w  ww  .  j av  a 2s.  co  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.frand.easyandroid.http.FFHttpClient.java

public FFHttpClient() {
    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);

    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// w  w  w .java  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 FFRetryHandler(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:nl.uva.sne.disambiguators.WikipediaOnline.java

private Map<String, List<String>> getCategories(Set<Term> terms)
        throws MalformedURLException, InterruptedException, ExecutionException {
    int maxT = 3;
    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<String, List<String>> cats = new HashMap<>();
    Set<Future<Map<String, List<String>>>> set = new HashSet<>();
    int count = 0;
    for (Term t : terms) {
        URL url = new URL(page + "?action=query&format=json&prop=categories&pageids=" + t.getUID());
        System.err.println(url);//from w  ww  .  jav  a2s  .  c o  m
        WikiRequestor req = new WikiRequestor(url, t.getUID(), 0);
        Future<Map<String, List<String>>> future = pool.submit(req);
        set.add(future);
    }
    pool.shutdown();

    for (Future<Map<String, List<String>>> future : set) {
        while (!future.isDone()) {
            //                Logger.getLogger(WikipediaOnline.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:hudson.plugins.active_directory.ActiveDirectoryUnixAuthenticationProvider.java

public ActiveDirectoryUnixAuthenticationProvider(ActiveDirectorySecurityRealm realm) {
    this.site = realm.site;
    this.domains = realm.domains;
    this.groupLookupStrategy = realm.getGroupLookupStrategy();
    this.descriptor = realm.getDescriptor();
    this.cache = realm.cache;

    if (cache == null) {
        this.cache = new CacheConfiguration(0, 0);
    }//w  w w.  j  ava2  s.c o  m

    // On startup userCache and groupCache are not created and cache is different from null
    if (cache.getUserCache() == null || cache.getGroupCache() == null) {
        this.cache = new CacheConfiguration(cache.getSize(), cache.getTtl());
    }

    this.userCache = cache.getUserCache();
    this.groupCache = cache.getGroupCache();

    this.threadPoolExecutor = new ThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveTime,
            TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(queueSize),
            new NamingThreadFactory(new DaemonThreadFactory(), "ActiveDirectory.updateUserCache"),
            new ThreadPoolExecutor.DiscardPolicy());

    Map<String, String> extraEnvVarsMap = ActiveDirectorySecurityRealm.EnvironmentProperty
            .toMap(realm.environmentProperties);
    props.put(LDAP_CONNECT_TIMEOUT, System.getProperty(LDAP_CONNECT_TIMEOUT, DEFAULT_LDAP_CONNECTION_TIMEOUT));
    props.put(LDAP_READ_TIMEOUT, System.getProperty(LDAP_READ_TIMEOUT, DEFAULT_LDAP_READ_TIMEOUT));
    // put all the user defined properties into our context environment replacing any mappings that already exist.
    props.putAll(extraEnvVarsMap);
}

From source file:com.taobao.adfs.database.tdhsocket.client.statement.StatementImpl.java

protected TDHSResponse sendRequest(TDHSCommon.RequestType type, RequestWithCharest request,
        List<String> fieldNames) throws TDHSException {
    if (request == null) {
        throw new IllegalArgumentException("request can't be NULL!");
    }/*from ww  w . j  a va2s  . c om*/
    if (StringUtils.isBlank(request.getCharestName())) {
        //use default charestName
        request.setCharestName(this.charestName);
    }
    byte data[] = protocol.encode(request);
    BasePacket packet = new BasePacket(type, id.getAndIncrement(), hash, data);
    ArrayBlockingQueue<BasePacket> queue = new ArrayBlockingQueue<BasePacket>(1);
    long seqId = packet.getSeqId();
    responses.put(seqId, queue);
    try {
        tdhsNet.write(packet);
        return do_response(queue, fieldNames, request.getCharestName());
    } finally {
        responses.remove(seqId);
        queue = null;
    }
}

From source file:com.ngdata.sep.impl.SepConsumer.java

/**
 * @param subscriptionTimestamp timestamp of when the index subscription became active (or more accurately, not
 *        inactive)//from ww  w.jav  a 2 s .  c  om
 * @param listener listeners that will process the events
 * @param threadCnt number of worker threads that will handle incoming SEP events
 * @param hostName hostname to bind to
 * @param payloadExtractor extracts payloads to include in SepEvents
 */
public SepConsumer(String subscriptionId, long subscriptionTimestamp, EventListener listener, int threadCnt,
        String hostName, ZooKeeperItf zk, Configuration hbaseConf, PayloadExtractor payloadExtractor)
        throws IOException, InterruptedException {
    Preconditions.checkArgument(threadCnt > 0, "Thread count must be > 0");
    this.subscriptionId = SepModelImpl.toInternalSubscriptionName(subscriptionId);
    this.subscriptionTimestamp = subscriptionTimestamp;
    this.listener = listener;
    this.zk = zk;
    this.hbaseConf = hbaseConf;
    this.sepMetrics = new SepMetrics(subscriptionId);
    this.payloadExtractor = payloadExtractor;
    this.executors = Lists.newArrayListWithCapacity(threadCnt);

    InetSocketAddress initialIsa = new InetSocketAddress(hostName, 0);
    if (initialIsa.getAddress() == null) {
        throw new IllegalArgumentException("Failed resolve of " + initialIsa);
    }
    String name = "regionserver/" + initialIsa.toString();
    this.rpcServer = new RpcServer(this, name, getServices(),
            /*HBaseRPCErrorHandler.class, OnlineRegions.class},*/
            initialIsa, // BindAddress is IP we got for this server.
            hbaseConf.getInt("hbase.regionserver.handler.count", 10),
            hbaseConf.getInt("hbase.regionserver.metahandler.count", 10), hbaseConf, HConstants.QOS_THRESHOLD);
    this.serverName = new ServerName(hostName, rpcServer.getListenerAddress().getPort(),
            System.currentTimeMillis());
    this.zkWatcher = new ZooKeeperWatcher(hbaseConf, this.serverName.toString(), null);

    // login the zookeeper client principal (if using security)
    ZKUtil.loginClient(hbaseConf, "hbase.zookeeper.client.keytab.file",
            "hbase.zookeeper.client.kerberos.principal", hostName);

    // login the server principal (if using secure Hadoop)
    User.login(hbaseConf, "hbase.regionserver.keytab.file", "hbase.regionserver.kerberos.principal", hostName);

    for (int i = 0; i < threadCnt; i++) {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, 10, TimeUnit.SECONDS,
                new ArrayBlockingQueue<Runnable>(100));
        executor.setRejectedExecutionHandler(new WaitPolicy());
        executors.add(executor);
    }
}

From source file:middleware.NewServerSocket.java

NewServerSocket(SharedData s) {
    sharedData = s;//from  w  w  w .  ja v  a  2s .  com
    try {
        serverSocketChannel = ServerSocketChannel.open();
        serverSocketChannel.socket().bind(new InetSocketAddress(s.getMiddlePortNum()));
        serverSocketChannel.configureBlocking(false);

    } catch (IOException e) {
        System.out.println("Error: cannot bind to port " + s.getMiddlePortNum());
        e.printStackTrace();
    }

    try {
        adminServerSocketChannel = ServerSocketChannel.open();
        adminServerSocketChannel.socket().bind(new InetSocketAddress(s.getAdminPortNum()));
        adminServerSocketChannel.configureBlocking(false);
    } catch (IOException e) {
        System.out.println("Error: cannot bind to port " + s.getAdminPortNum());
        e.printStackTrace();
    }

    try {
        selector = Selector.open();
        serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
        adminServerSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
    } catch (IOException e) {
        e.printStackTrace();
    }

    keyIterator = null;

    dir = new File(sharedData.getFilePathName() + File.separator + "Transactions");
    if (!dir.exists()) {
        dir.mkdirs();
    } else {
        for (File f : dir.listFiles()) {
            if (!f.delete()) {
                // TODO
            }
        }
    }

    numWorkers = sharedData.getNumWorkers();
    workers = new NewWorker[numWorkers];
    for (int i = 0; i < numWorkers; ++i) {
        Selector tmpS = null;
        try {
            tmpS = Selector.open();
        } catch (IOException e) {
            e.printStackTrace();
        }

        workers[i] = new NewWorker(sharedData, tmpS);
        workers[i].start();
    }

    data = new byte[sharedData.getMaxSize()];
    buffer = ByteBuffer.wrap(data);
    endingMonitoring = false;
    sendingFiles = false;
    monitoring = false;
    dstatDeployed = false;
    failDeployDstat = false;
    configSetenv = false;

    mysql_user = null;
    mysql_pass = null;
    mysql_host = null;
    mysql_port = null;

    sharedData.allTransactionData = new ArrayList<TransactionData>();
    sharedData.allTransactions = new ConcurrentSkipListMap<Integer, byte[]>();
    // sharedData.allStatementsInfo = new ConcurrentLinkedQueue<byte[]>();
    sharedData.allQueries = new ConcurrentSkipListMap<Long, QueryData>();

    userInfo = Encrypt.getUsrMap(sharedData.getUserInfoFilePath());

    fileBuffer = new byte[1024];

    curUser = null;

    userList = new ArrayList<MiddleSocketChannel>();

    dstat = null;

    ntpdate = null;

    stopRemoteDstat = null;

    incrementalLogQueue = new ArrayBlockingQueue<IncrementalLog>(64 * 1024);
}