Example usage for java.util.concurrent ArrayBlockingQueue isEmpty

List of usage examples for java.util.concurrent ArrayBlockingQueue isEmpty

Introduction

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

Prototype

boolean isEmpty();

Source Link

Document

Returns true if this collection contains no elements.

Usage

From source file:org.kchine.rpf.db.MainNodeManager.java

private static void createTask() {
    try {/* w w  w  .j  ava  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();
    }
}