Example usage for java.util.concurrent LinkedBlockingQueue LinkedBlockingQueue

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

Introduction

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

Prototype

public LinkedBlockingQueue() 

Source Link

Document

Creates a LinkedBlockingQueue with a capacity of Integer#MAX_VALUE .

Usage

From source file:com.netflix.curator.framework.imps.TestFramework.java

@Test
public void testConnectionState() throws Exception {
    Timing timing = new Timing();
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(),
            timing.connection(), new RetryOneTime(1));
    try {/* w ww .j  a  va2s. c  o  m*/
        final BlockingQueue<ConnectionState> queue = new LinkedBlockingQueue<ConnectionState>();
        ConnectionStateListener listener = new ConnectionStateListener() {
            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
                queue.add(newState);
            }
        };
        client.getConnectionStateListenable().addListener(listener);

        client.start();
        Assert.assertEquals(queue.poll(timing.multiple(4).seconds(), TimeUnit.SECONDS),
                ConnectionState.CONNECTED);
        server.stop();
        Assert.assertEquals(queue.poll(timing.multiple(4).seconds(), TimeUnit.SECONDS),
                ConnectionState.SUSPENDED);
        Assert.assertEquals(queue.poll(timing.multiple(4).seconds(), TimeUnit.SECONDS), ConnectionState.LOST);
    } finally {
        IOUtils.closeQuietly(client);
    }
}

From source file:io.socket.AbstractTestSocketIO.java

/**
 * Sets up the test. Starts the node testserver on a randomly choosed port,
 * starts backgroundthreads for processing stdin/stdout. Adds shutdown-hook
 * for clean kill of the node server.//from   ww  w .  ja v  a 2  s  . c  o m
 * 
 * @throws Exception
 *             the exception
 */
@Before
public void setUp() throws Exception {
    assertNotNull("Transport is set correctly", transport);
    events = new LinkedBlockingQueue<String>();
    outputs = new LinkedBlockingQueue<String>();
    args = new LinkedBlockingQueue<Object>();
    System.out.println("Connect with " + transport);
    node = Runtime.getRuntime()
            .exec(new String[] { NODE, "./tests/io/socket/testutils/socketio.js", "" + getPort(), transport });
    proxy = new MutateProxy(getPort() + 1, getPort());
    proxy.start();

    stdoutThread = new Thread("stdoutThread") {
        @Override
        public void run() {
            BufferedReader reader = new BufferedReader(new InputStreamReader(node.getInputStream()));
            String line;
            try {
                while ((line = reader.readLine()) != null) {
                    if (line.startsWith("__:")) {
                        System.out.println("Out: " + line);
                        outputs.add(line.substring("__:".length()));
                    } else
                        System.out.println("Node: " + line);
                }
            } catch (IOException e) {
                if (!interrupted()) {
                    e.printStackTrace();
                    System.err.println("Node read error");
                }
            }
            System.err.println("Node output end");
        }
    };
    stderrThread = new Thread("stderrThread") {
        @Override
        public void run() {
            BufferedReader reader = new BufferedReader(new InputStreamReader(node.getErrorStream()));
            try {
                String line;
                while ((line = reader.readLine()) != null) {
                    System.err.println("Node: " + line);
                }
            } catch (IOException e) {
                if (!interrupted()) {
                    e.printStackTrace();
                    System.err.println("Node read error");
                }
            }
            System.err.println("Node output end");
        };
    };
    stderrThread.start();
    stdoutThread.start();
    Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override
        public void run() {
            try {
                node.destroy();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    assertEquals("OK", takeLine());
}

From source file:cx.fbn.nevernote.threads.ENThumbnailRunner.java

public ENThumbnailRunner(String logname, int t, String u, String i, String r, String b, String uid, String pswd,
        String cpswd) {/*w  w w .  ja  va2  s .  c  o  m*/
    this.logger = new ApplicationLogger(logname);
    this.conn = new DatabaseConnection(logger, u, i, r, b, uid, pswd, cpswd, 0);
    this.enThumbnailSignal = new ENThumbnailSignal();
    this.mutex = new QMutex();
    this.keepRunning = true;
    this.workQueue = new LinkedBlockingQueue<String>();
    this.limitSignal = new LimitSignal();
    this.user = new User();
    this.serverUrl = "";
}

From source file:mydropbox.MyDropboxSwing.java

/**
 * Creates new form MyDropboxSwing//www .j  a  v  a 2s.  c  om
 */
public MyDropboxSwing() {
    initComponents();
    config = new AppConfig();
    prop = config.getProperties();
    loadFileConfig();
    addWindowListener(this);
    lstCommit = new LinkedBlockingQueue<>();
    timer = new Thread(new AutoSyncService());
}

From source file:org.ala.spatial.analysis.index.LayerDistanceIndex.java

/**
 * @param threadcount    number of threads to run analysis.
 * @param onlyThesePairs array of distances to run as fieldId1 + " " +
 *                       fieldId2 where fieldId1.compareTo(fieldId2) &lt 0 or null for all missing
 *                       distances./*from ww  w  .  j a v  a 2s  . c o  m*/
 * @throws InterruptedException
 */
public void occurrencesUpdate(int threadcount, String[] onlyThesePairs) throws InterruptedException {

    //create distances file if it does not exist.
    File layerDistancesFile = new File(AlaspatialProperties.getAnalysisWorkingDir() + LAYER_DISTANCE_FILE);
    if (!layerDistancesFile.exists()) {
        try {
            FileWriter fw = new FileWriter(layerDistancesFile);
            fw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    Map<String, Double> map = loadDistances();

    LinkedBlockingQueue<String> todo = new LinkedBlockingQueue();

    if (onlyThesePairs != null && onlyThesePairs.length > 0) {
        for (String s : onlyThesePairs) {
            todo.add(s);
        }
    } else {
        //find all environmental layer analysis files
        File root = new File(AlaspatialProperties.getAnalysisLayersDir());
        File[] dirs = root.listFiles(new FileFilter() {

            @Override
            public boolean accept(File pathname) {
                return pathname != null && pathname.isDirectory();
            }
        });

        HashMap<String, String> domains = new HashMap<String, String>();
        for (File dir : dirs) {
            //iterate through files so we get everything
            File[] files = new File(dir.getPath()).listFiles(new FileFilter() {

                @Override
                public boolean accept(File pathname) {
                    return pathname.getName().endsWith(".grd") && pathname.getName().startsWith("el");
                }
            });

            for (int i = 0; i < files.length; i++) {
                for (int j = i + 1; j < files.length; j++) {
                    String file1 = files[i].getName().replace(".grd", "");
                    String file2 = files[j].getName().replace(".grd", "");

                    //only operate on file names that are valid fields
                    if (Client.getFieldDao().getFieldById(file1) != null
                            && Client.getFieldDao().getFieldById(file2) != null) {

                        String domain1 = domains.get(file1);
                        if (domain1 == null) {
                            String pid1 = Client.getFieldDao().getFieldById(file1).getSpid();
                            domain1 = Client.getLayerDao().getLayerById(Integer.parseInt(pid1)).getdomain();
                            domains.put(file1, domain1);
                        }
                        String domain2 = domains.get(file2);
                        if (domain2 == null) {
                            String pid2 = Client.getFieldDao().getFieldById(file2).getSpid();
                            domain2 = Client.getLayerDao().getLayerById(Integer.parseInt(pid2)).getdomain();
                            domains.put(file2, domain2);
                        }

                        String key = (file1.compareTo(file2) < 0) ? file1 + " " + file2 : file2 + " " + file1;

                        //domain test
                        if (isSameDomain(parseDomain(domain1), parseDomain(domain2))) {
                            if (!map.containsKey(key) && !todo.contains(key)) {
                                todo.put(key);
                            }
                        }
                    }
                }
            }
        }
    }

    LinkedBlockingQueue<String> toDisk = new LinkedBlockingQueue<String>();
    CountDownLatch cdl = new CountDownLatch(todo.size());
    CalcThread[] threads = new CalcThread[threadcount];
    for (int i = 0; i < threadcount; i++) {
        threads[i] = new CalcThread(cdl, todo, toDisk);
        threads[i].start();
    }

    ToDiskThread toDiskThread = new ToDiskThread(
            AlaspatialProperties.getAnalysisWorkingDir() + LAYER_DISTANCE_FILE, toDisk);
    toDiskThread.start();

    cdl.await();

    for (int i = 0; i < threadcount; i++) {
        threads[i].interrupt();
    }

    toDiskThread.interrupt();
}

From source file:cn.wanghaomiao.seimi.def.DefaultLocalQueue.java

public LinkedBlockingQueue<Request> getQueue(String crawlerName) {
    LinkedBlockingQueue<Request> queue = queueMap.get(crawlerName);
    if (queue == null) {
        queue = new LinkedBlockingQueue<>();
        queueMap.put(crawlerName, queue);
    }/*ww w .j  av a  2  s.  co  m*/
    return queue;
}

From source file:bigbird.benchmark.HttpBenchmark.java

public void execute() {
    params = getHttpParams(socketTimeout, useHttp1_0);

    for (RequestGenerator g : requestGenerators) {
        g.setParameters(params);/*from w w w.jav  a 2  s . c om*/
    }

    host = new HttpHost(url.getHost(), url.getPort(), url.getProtocol());

    ThreadPoolExecutor workerPool = new ThreadPoolExecutor(threads, threads, 5, TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>(), new ThreadFactory() {

                public Thread newThread(Runnable r) {
                    return new Thread(r, "ClientPool");
                }

            });
    workerPool.prestartAllCoreThreads();

    BenchmarkWorker[] workers = new BenchmarkWorker[threads];
    for (int i = 0; i < threads; i++) {
        workers[i] = new BenchmarkWorker(params, verbosity, requestGenerators[i], host, requests, keepAlive);
        workerPool.execute(workers[i]);
    }

    while (workerPool.getCompletedTaskCount() < threads) {
        Thread.yield();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ignore) {
        }
    }

    workerPool.shutdown();
    ResultProcessor.printResults(workers, host, url.toString(), contentLength);
}

From source file:com.openteach.diamond.network.waverider.network.Packet.java

public static void main(String[] args) {
    /*System.out.println("[DEBUG] Packet Header size = " + getHeaderSize());
            /*  ww  w  .  jav  a  2s .c o m*/
    SlaveState slaveState = new SlaveState();
    slaveState.setId(1L);
    slaveState.setIsMasterCandidate(false);
    Command command = CommandFactory.createHeartbeatCommand(slaveState.toByteBuffer());
    Packet packet = Packet.newDataPacket(command);
    ByteBuffer buffer = packet.marshall();
    Packet p = Packet.unmarshall(buffer);
    Command cmd = Command.unmarshall(p.getPayLoad());
    SlaveState ss = SlaveState.fromByteBuffer(cmd.getPayLoad());
    System.out.println(cmd.toString());
            
            
    // Test 2
    MasterState masterState = new MasterState();
    masterState.setId(1L);
    masterState.setIp("127.0.0.1");
    masterState.setPort(8206);
    List<SessionState> sessionStateList = new LinkedList<SessionState>();
    masterState.setSessionStateList(sessionStateList);
    SessionState sessionState = null;
    for(int i = 0; i < 10; i++) {
       sessionState = new SessionState();
       sessionState.setIp("127.0.0.1");
       sessionState.setPriority(1L);
       sessionState.setIsMasterCandidate(false);
       sessionStateList.add(sessionState);
    }
    Command command2 = CommandFactory.createHeartbeatCommand(masterState.toByteBuffer());
    Packet packet2 = Packet.newDataPacket(command2);
    ByteBuffer buffer2 = packet2.marshall();
    Packet p2 = Packet.unmarshall(buffer2);
    Command cmd2 = Command.unmarshall(p2.getPayLoad());
    MasterState ms = MasterState.fromByteBuffer(cmd2.getPayLoad());
    System.out.println(cmd.toString());
    */

    System.out.println("[DEBUG] Packet Header size = " + getHeaderSize());

    BlockingQueue<ByteBuffer> queue = new LinkedBlockingQueue<ByteBuffer>();
    Random rand = new Random();
    int count = 0;
    int size = 0;
    for (int i = 0; i < 100000; i++) {
        SlaveState state = new SlaveState();
        state.setId(Long.valueOf(i));
        state.setIsMasterCandidate(true);
        Packet packet = Packet.newDataPacket(
                CommandFactory.createCommand(Command.AVAILABLE_COMMAND_START, state.toByteBuffer()));
        ByteBuffer buffer = packet.marshall();
        rand.setSeed(System.currentTimeMillis());
        count = rand.nextInt(100) + 1;
        size = buffer.remaining() / count;
        for (int j = 0; j < count; j++) {
            ByteBuffer buf = null;
            if (j == count - 1) {
                buf = ByteBuffer.allocate(buffer.remaining() - j * size);
                buf.put(buffer.array(), j * size, buffer.remaining() - j * size);
            } else {
                buf = ByteBuffer.allocate(size);
                buf.put(buffer.array(), j * size, size);
            }
            buf.flip();
            queue.add(buf);
        }
    }

    for (int i = 0; i < 100000; i++) {
        //Packet packet = Packet.parse(queue);
        //Command commad = Command.unmarshall(packet.getPayLoad());
        //SlaveState state = SlaveState.fromByteBuffer(commad.getPayLoad());
        //System.out.println(state.toString());
    }
}

From source file:au.org.ala.layers.stats.ObjectsStatsGenerator.java

private static void updateBbox() {

    try {//from   w  w w .  ja  va 2 s.  c o  m
        Connection conn = getConnection();
        String sql = "SELECT pid from objects where bbox is null limit 200000;";
        logger.info("loading bbox ...");
        Statement s1 = conn.createStatement();
        ResultSet rs1 = s1.executeQuery(sql);

        LinkedBlockingQueue<String[]> data = new LinkedBlockingQueue<String[]>();
        while (rs1.next()) {
            data.put(new String[] { rs1.getString("pid") });
        }

        CountDownLatch cdl = new CountDownLatch(data.size());

        BboxThread[] threads = new BboxThread[CONCURRENT_THREADS];
        for (int j = 0; j < CONCURRENT_THREADS; j++) {
            threads[j] = new BboxThread(data, cdl, getConnection().createStatement());
            threads[j].start();
        }

        cdl.await();

        for (int j = 0; j < CONCURRENT_THREADS; j++) {
            threads[j].s.close();
            threads[j].interrupt();
        }
        return;
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }
    return;
}

From source file:cc.osint.graphd.client.ProtographClient.java

public ProtographClient(String host, int port, ProtographClientEventHandler eventHandler) throws Exception {
    this.host = host;
    this.port = port;
    connected = false;/*from   w w w  . j av a 2  s.  co  m*/
    shutdown = false;
    bootstrap = null;
    channel = null;
    lastWriteFuture = null;
    this.eventHandler = eventHandler;
    ProtographClientHandler protographClientHandler = new ProtographClientHandler(this);
    protographClientHandler.setProtographClient(this);
    clientCommandQueue = new LinkedBlockingQueue<ClientCommand>();
    clientCommandThread = new Thread(this);
    clientCommandThread.start();
    connect();
}