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.kchine.rpf.db.MainNodeManager.java

private static void createTask() {
    try {/* w  ww .  ja  va  2 s . c o m*/
        System.out.println(" create worker round at " + new Date());
        long T1 = System.currentTimeMillis();

        final ArrayBlockingQueue<String> indexesQueue = new ArrayBlockingQueue<String>(200);

        Vector<NodeDataDB> nodes = _registry.getNodeData("");
        for (int i = 0; i < nodes.size(); ++i) {
            final String nodeName = nodes.elementAt(i).getNodeName();
            String nodeIp = nodes.elementAt(i).getHostIp();
            String nodePrefix = nodes.elementAt(i).getPoolPrefix();

            Vector<HashMap<String, Object>> servants = _registry.getTableData("SERVANTS", "NODE_NAME='"
                    + nodeName + "'" + " OR (HOST_IP='" + nodeIp + "' AND NAME like '" + nodePrefix + "%')");

            final int missing = nodes.elementAt(i).getServantNbrMin() - servants.size();
            if (missing > 0) {
                System.out.println("Node<" + nodeName + "> missing :" + missing);
                for (int j = 0; j < missing; ++j) {
                    indexesQueue.add(nodeName);
                }
            }
        }

        Thread[] t = new Thread[10];
        for (int i = 0; i < t.length; ++i) {
            t[i] = new Thread(new Runnable() {
                public void run() {

                    while (true) {

                        if (indexesQueue.isEmpty())
                            break;
                        try {
                            if (_nodeData == null) {
                                if (!indexesQueue.isEmpty()) {
                                    String nodeName = indexesQueue.poll();
                                    if (nodeName != null) {
                                        try {
                                            _registry.lookup(
                                                    System.getProperty("node.manager.name") + '_' + nodeName);
                                        } catch (NotBoundException nbe) {
                                            NodeManager _manager = (NodeManager) _registry
                                                    .lookup(System.getProperty("node.manager.name"));
                                            ManagedServant ms = _manager.createServant(nodeName);
                                            System.out.println(ms + "  successfully created");
                                        }
                                    }
                                }
                            } else {

                                if (!indexesQueue.isEmpty()) {
                                    String nodeName = indexesQueue.poll();
                                    if (nodeName != null && nodeName.equals(_nodeData.getNodeName())) {
                                        NodeManager _manager = (NodeManager) _registry.lookup(_nodeManagerName);
                                        ManagedServant ms = _manager.createServant(nodeName);
                                        System.out.println(ms + "  successfully created");
                                    }
                                }

                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                        } finally {
                            try {
                                Thread.sleep(500);
                            } catch (Exception e) {
                            }
                        }
                    }

                }
            });

            t[i].start();

        }

        for (int i = 0; i < t.length; ++i) {
            t[i].join();
        }

        System.out
                .println("Last create servants round took :" + (System.currentTimeMillis() - T1) + " millisec");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.apache.kylin.cube.inmemcubing.ITInMemCubeBuilderTest.java

private void testBuildInner() throws Exception {

    IJoinedFlatTableDesc flatDesc = EngineFactory.getJoinedFlatTableDesc(cube.getDescriptor());
    InMemCubeBuilder cubeBuilder = new InMemCubeBuilder(cube.getDescriptor(), flatDesc, dictionaryMap);
    //DoggedCubeBuilder cubeBuilder = new DoggedCubeBuilder(cube.getDescriptor(), dictionaryMap);
    cubeBuilder.setConcurrentThreads(nThreads);

    ArrayBlockingQueue<List<String>> queue = new ArrayBlockingQueue<List<String>>(1000);
    ExecutorService executorService = Executors.newSingleThreadExecutor();

    try {//from  ww w.  jav  a2s  .c  om
        // round 1
        {
            Future<?> future = executorService
                    .submit(cubeBuilder.buildAsRunnable(queue, new ConsoleGTRecordWriter()));
            feedData(cube, flatTable, queue, nInpRows);
            future.get();
        }

        // round 2, zero input
        {
            Future<?> future = executorService
                    .submit(cubeBuilder.buildAsRunnable(queue, new ConsoleGTRecordWriter()));
            feedData(cube, flatTable, queue, 0);
            future.get();
        }

        // round 3
        {
            Future<?> future = executorService
                    .submit(cubeBuilder.buildAsRunnable(queue, new ConsoleGTRecordWriter()));
            feedData(cube, flatTable, queue, nInpRows);
            future.get();
        }

    } catch (Exception e) {
        logger.error("stream build failed", e);
        throw new IOException("Failed to build cube ", e);
    }
}

From source file:org.apache.s4.core.classloader.TestClassLoader.java

@Test
public void testInitialDeploymentFromFileSystem() throws Exception {

    File producerS4R = new File(producerS4rDir, "testApp-0.0.0-SNAPSHOT.s4r");
    String uriProducer = producerS4R.toURI().toString();

    initializeS4Node();//from   w w  w  . j a v  a  2s. c  o  m

    final BlockingQueue<Object> queue = new ArrayBlockingQueue<Object>(1);
    zkClient.subscribeDataChanges("/s4/classLoader", new IZkDataListener() {

        @Override
        public void handleDataDeleted(String dataPath) throws Exception {
        }

        @Override
        public void handleDataChange(String dataPath, Object data) throws Exception {
            queue.put(data);
        }
    });
    DeploymentUtils.initAppConfig(new AppConfig.Builder().appURI(uriProducer).build(), PRODUCER_CLUSTER, true,
            "localhost:2181");

    Object classLoaderRecord = queue.poll(20, TimeUnit.SECONDS);
    assertTrue("Stored record has unexpected type", classLoaderRecord instanceof ZNRecord);
    ZNRecord record = (ZNRecord) classLoaderRecord;

    assertEquals("Unexpected classloader runs the app init()", S4RLoader.class.getName(), record.getId());
}

From source file:com.kurento.kmf.media.test.HttpGetEndpointTest.java

/**
 * Test for {@link MediaSessionTerminatedEvent}
 *
 * @throws InterruptedException// w w w .  jav  a  2  s .  c o m
 * @throws IOException
 * @throws ClientProtocolException
 */
@Test
public void testEventMediaSessionTerminated()
        throws InterruptedException, ClientProtocolException, IOException {
    final PlayerEndpoint player = pipeline.newPlayerEndpoint(URL_SMALL).build();
    HttpGetEndpoint httpEP = pipeline.newHttpGetEndpoint().terminateOnEOS().build();
    player.connect(httpEP);

    httpEP.addMediaSessionStartedListener(new MediaEventListener<MediaSessionStartedEvent>() {

        @Override
        public void onEvent(MediaSessionStartedEvent event) {
            player.play();
        }
    });

    final BlockingQueue<MediaSessionTerminatedEvent> events = new ArrayBlockingQueue<>(1);
    httpEP.addMediaSessionTerminatedListener(new MediaEventListener<MediaSessionTerminatedEvent>() {

        @Override
        public void onEvent(MediaSessionTerminatedEvent event) {
            events.add(event);
        }
    });

    try (CloseableHttpClient httpclient = HttpClientBuilder.create().build()) {
        // This should trigger MediaSessionStartedEvent
        httpclient.execute(new HttpGet(httpEP.getUrl()));
    }

    Assert.assertNotNull("MediaSessionTerminatedEvent not sent in 20s", events.poll(20, SECONDS));

    httpEP.release();
    player.release();
}

From source file:com.datatorrent.contrib.kafka.KafkaConsumer.java

/**
 * This method is called in setup method of the operator
 *//*from   www  . ja  v a 2  s.c  o  m*/
public void create() {
    initBrokers();
    holdingBuffer = new ArrayBlockingQueue<KafkaMessage>(cacheSize);
}

From source file:org.kurento.client.test.HttpGetEndpointTest.java

/**
 * Test for {@link MediaSessionTerminatedEvent}
 *
 * @throws InterruptedException/*from   w  w w. j a  v a 2 s  . co  m*/
 * @throws IOException
 * @throws ClientProtocolException
 */
@Test
public void testEventMediaSessionTerminated()
        throws InterruptedException, ClientProtocolException, IOException {

    final PlayerEndpoint player = new PlayerEndpoint.Builder(pipeline, URL_SMALL).build();

    HttpGetEndpoint httpEP = new HttpGetEndpoint.Builder(pipeline).terminateOnEOS().build();

    player.connect(httpEP);

    httpEP.addMediaSessionStartedListener(new EventListener<MediaSessionStartedEvent>() {

        @Override
        public void onEvent(MediaSessionStartedEvent event) {
            player.play();
        }
    });

    final BlockingQueue<MediaSessionTerminatedEvent> events = new ArrayBlockingQueue<>(1);
    httpEP.addMediaSessionTerminatedListener(new EventListener<MediaSessionTerminatedEvent>() {

        @Override
        public void onEvent(MediaSessionTerminatedEvent event) {
            events.add(event);
        }
    });

    try (CloseableHttpClient httpclient = HttpClientBuilder.create().build()) {
        // This should trigger MediaSessionStartedEvent
        httpclient.execute(new HttpGet(httpEP.getUrl()));
    }

    Assert.assertNotNull("MediaSessionTerminatedEvent not sent in 20s", events.poll(20, SECONDS));

    httpEP.release();
    player.release();
}

From source file:aptgraph.server.JsonRpcServer.java

/**
 * Start the server, blocking. This method will only return if the server
 * crashed...//ww w  . j a va2s  .  c  o m
 * @throws java.io.IOException if the graph file cannot be read
 * @throws java.lang.ClassNotFoundException if the Graph class is not found
 * @throws java.lang.Exception if the server cannot start...
 */
public final void start() throws IOException, ClassNotFoundException, Exception {

    LOGGER.info("Reading graphs from disk...");
    ObjectInputStream input = new ObjectInputStream(new BufferedInputStream(input_file));
    HashMap<String, LinkedList<Graph<Request>>> user_graphs = (HashMap<String, LinkedList<Graph<Request>>>) input
            .readObject();
    input.close();

    Map.Entry<String, LinkedList<Graph<Request>>> entry_set = user_graphs.entrySet().iterator().next();
    String first_key = entry_set.getKey();
    LOGGER.log(Level.INFO, "Graph has {0} features", user_graphs.get(first_key).size());
    LOGGER.log(Level.INFO, "k-NN Graph : k = {0}", user_graphs.get(first_key).getFirst().getK());
    LOGGER.log(Level.INFO, "Starting JSON-RPC server at http://{0}:{1}",
            new Object[] { config.getServerHost(), config.getServerPort() });

    RequestHandler request_handler = new RequestHandler(user_graphs);

    ObjectMapper object_mapper = new ObjectMapper();
    SimpleModule module = new SimpleModule();
    module.addSerializer(Graph.class, new GraphSerializer());
    module.addSerializer(Domain.class, new DomainSerializer());
    module.addSerializer(Neighbor.class, new NeighborSerializer());
    object_mapper.registerModule(module);

    com.googlecode.jsonrpc4j.JsonRpcServer jsonrpc_server = new com.googlecode.jsonrpc4j.JsonRpcServer(
            object_mapper, request_handler);

    QueuedThreadPool thread_pool = new QueuedThreadPool(config.getMaxThreads(), config.getMinThreads(),
            config.getIdleTimeout(), new ArrayBlockingQueue<Runnable>(config.getMaxPendingRequests()));

    http_server = new org.eclipse.jetty.server.Server(thread_pool);
    //http_server = new org.eclipse.jetty.server.Server();

    ServerConnector http_connector = new ServerConnector(http_server);
    http_connector.setHost(config.getServerHost());
    http_connector.setPort(config.getServerPort());

    http_server.setConnectors(new Connector[] { http_connector });
    http_server.setHandler(new JettyHandler(jsonrpc_server));

    http_server.start();
}

From source file:com.espertech.esper.core.thread.ThreadingServiceImpl.java

private BlockingQueue<Runnable> makeQueue(Integer threadPoolTimerExecCapacity) {
    if ((threadPoolTimerExecCapacity == null) || (threadPoolTimerExecCapacity <= 0)
            || (threadPoolTimerExecCapacity == Integer.MAX_VALUE)) {
        return new LinkedBlockingQueue<Runnable>();
    } else {/*  w w  w . j  a v  a 2 s . co m*/
        return new ArrayBlockingQueue<Runnable>(threadPoolTimerExecCapacity);
    }
}

From source file:android.concurrent.ThreadPool.java

private static ThreadPoolExecutor createThreadPoolExecutor(String poolName) {
    ArrayBlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(DEFAULT_BLOCKING_QUEUE_SIZE);
    //      CallerRunsPolicy policy = new CallerRunsPolicy();
    ThreadPoolExecutor.DiscardPolicy policy = new ThreadPoolExecutor.DiscardPolicy();
    ThreadPoolExecutor excuter = new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE_TIME,
            UNIT, queue, new PoolThreadFactory(poolName), policy);
    excuter.prestartAllCoreThreads();/*ww w . j  av a 2  s . c o  m*/
    excuter.allowCoreThreadTimeOut(true);

    return excuter;
}

From source file:eu.stratosphere.nephele.checkpointing.WriteThread.java

WriteThread(final FileBufferManager fileBufferManager, final ExecutionVertexID vertexID,
        final int numberOfConnectedChannels) {

    super("Write thread for vertex " + vertexID);

    this.fileBufferManager = fileBufferManager;
    this.vertexID = vertexID;
    this.numberOfConnectedChannels = numberOfConnectedChannels;
    this.queuedEnvelopes = new ArrayBlockingQueue<TransferEnvelope>(256);

    final boolean dist = CheckpointUtils.allowDistributedCheckpoints();

    if (dist) {// w w  w . j av  a2  s.  com
        final Path p = CheckpointUtils.getDistributedCheckpointPath();
        if (p == null) {
            LOG.error("No distributed checkpoint path configured, writing local checkpoints instead");
            this.checkpointPath = CheckpointUtils.getLocalCheckpointPath();
            this.distributed = false;
        } else {
            this.checkpointPath = p;
            this.distributed = true;
        }
    } else {
        this.checkpointPath = CheckpointUtils.getLocalCheckpointPath();
        this.distributed = false;
    }
}