Example usage for java.util.concurrent ConcurrentLinkedQueue poll

List of usage examples for java.util.concurrent ConcurrentLinkedQueue poll

Introduction

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

Prototype

public E poll() 

Source Link

Usage

From source file:com.ibm.crail.tools.CrailBenchmark.java

void readMultiStream(String filename, int size, int loop, int batch) throws Exception {
    System.out.println(//from www  . jav a  2s .  c o  m
            "readMultiStream, filename " + filename + ", size " + size + ", loop " + loop + ", batch " + batch);

    //warmup
    ConcurrentLinkedQueue<CrailBuffer> bufferQueue = new ConcurrentLinkedQueue<CrailBuffer>();
    for (int i = 0; i < warmup; i++) {
        CrailBuffer buf = fs.allocateBuffer().limit(size).slice();
        bufferQueue.add(buf);
    }
    warmUp(filename, warmup, bufferQueue);
    while (!bufferQueue.isEmpty()) {
        CrailBuffer buf = bufferQueue.poll();
        fs.freeBuffer(buf);
    }

    //benchmark
    System.out.println("starting benchmark...");
    fs.getStatistics().reset();
    CrailBuffer _buf = null;
    if (size == CrailConstants.BUFFER_SIZE) {
        _buf = fs.allocateBuffer();
    } else if (size < CrailConstants.BUFFER_SIZE) {
        CrailBuffer __buf = fs.allocateBuffer();
        __buf.clear().limit(size);
        _buf = __buf.slice();
    } else {
        _buf = OffHeapBuffer.wrap(ByteBuffer.allocateDirect(size));
    }
    ByteBuffer buf = _buf.getByteBuffer();
    for (int i = 0; i < loop; i++) {
        CrailBufferedInputStream multiStream = fs.lookup(filename).get().asMultiFile().getMultiStream(batch);
        double sumbytes = 0;
        long _sumbytes = 0;
        double ops = 0;
        buf.clear();
        long start = System.currentTimeMillis();
        int ret = multiStream.read(buf);
        while (ret >= 0) {
            sumbytes = sumbytes + ret;
            long _ret = (long) ret;
            _sumbytes += _ret;
            ops = ops + 1.0;
            buf.clear();
            ret = multiStream.read(buf);
        }
        long end = System.currentTimeMillis();
        multiStream.close();

        double executionTime = ((double) (end - start)) / 1000.0;
        double throughput = 0.0;
        double latency = 0.0;
        double sumbits = sumbytes * 8.0;
        if (executionTime > 0) {
            throughput = sumbits / executionTime / 1000.0 / 1000.0;
            latency = 1000000.0 * executionTime / ops;
        }

        System.out.println("round " + i + ":");
        System.out.println("bytes read " + _sumbytes);
        System.out.println("execution time " + executionTime);
        System.out.println("ops " + ops);
        System.out.println("throughput " + throughput);
        System.out.println("latency " + latency);
    }

    fs.getStatistics().print("close");
}

From source file:ua.pp.serga.socketio.IOConnection.java

/**
 * Transport connected.// w w w  . j  ava 2  s .  c  o  m
 * 
 * {@link IOTransport} calls this when a connection is established.
 */
public void transportConnected() {
    setState(STATE_READY);
    if (reconnectTask != null) {
        reconnectTask.cancel();
        reconnectTask = null;
    }
    resetTimeout();
    synchronized (outputBuffer) {
        if (transport.canSendBulk()) {
            ConcurrentLinkedQueue<String> outputBuffer = this.outputBuffer;
            this.outputBuffer = new ConcurrentLinkedQueue<String>();
            try {
                // DEBUG
                String[] texts = outputBuffer.toArray(new String[outputBuffer.size()]);
                logger.info("Bulk start:");
                for (String text : texts) {
                    logger.info("> " + text);
                }
                logger.info("Bulk end");
                // DEBUG END
                transport.sendBulk(texts);
            } catch (IOException e) {
                this.outputBuffer = outputBuffer;
            }
        } else {
            String text;
            while ((text = outputBuffer.poll()) != null)
                sendPlain(text);
        }
        this.keepAliveInQueue = false;
    }
}

From source file:org.restcomm.app.qoslib.Services.Events.EventUploader.java

public void report(boolean local, Location location) {
    try {/*from   w  ww .ja va 2  s.c  o m*/
        //now create a list of eventData variables
        List<EventData> eventDataList = new ArrayList<EventData>();
        String strPhone = "";
        long duration = 0;

        EventDataEnvelope eventDataEnvelope = null;
        if (event != null) {
            // Dont send an unconfirmed Travel event
            if (event.getEventType() == EventType.TRAVEL_CHECK
                    && owner.getTravelDetector().isConfirmed() == false) {
                LoggerUtil.logToFile(LoggerUtil.Level.DEBUG, TAG, "report", "skip unconfirmed travel event");
                return;
            }

            EventData eventData = generateEventDataFromEvent(event, local);
            if (eventData == null)
                return;
            //MMCLogger.logToFile(MMCLogger.Level.DEBUG, TAG, "run", "reporting event type=" + event.getEventType() + ",lat:" + eventData.getFltEventLat() + ",local=" + local);
            if (eventData.getFltEventLat() == 0 && location != null) {
                eventData.setFltEventLat((float) location.getLatitude());
                eventData.setFltEventLng((float) location.getLongitude());
                eventData.setiUncertainty((int) location.getAccuracy());
            }
            eventData.setCallID(event.getLocalID());
            EventData eventData2 = null;
            eventDataList.add(eventData);

            if (event.getEventType() == EventType.APP_MONITORING && event.getDownloadSpeed() > 0
                    && event.getUploadSpeed() > 0) {
                //App throughput was getting stored twice
                boolean stored = PreferenceManager.getDefaultSharedPreferences(owner)
                        .getBoolean(PreferenceKeys.Miscellaneous.THROUGHPUT_STORED, false);
                if (!stored) {
                    owner.getReportManager().storeEvent(eventData);
                    PreferenceManager.getDefaultSharedPreferences(owner).edit()
                            .putBoolean(PreferenceKeys.Miscellaneous.THROUGHPUT_STORED, true).commit();
                } else
                    PreferenceManager.getDefaultSharedPreferences(owner).edit()
                            .putBoolean(PreferenceKeys.Miscellaneous.THROUGHPUT_STORED, false).commit();
                ;
            }
            if (event.getEventType() == EventType.MAN_PLOTTING) {
                eventData.setLookupid1(event.getBuildingID());
                eventData.setRunningApps(event.getAppData()); // contains user's polyline
            }

            // Event is 'reported' locally before GPS sampling is complete
            // to make it show up on the map as soon as it gets a first fix
            //if (local == true && ((event.getEventType() != EventType.MAN_SPEEDTEST && event.getEventType() != EventType.LATENCY_TEST && event.getEventType() != EventType.APP_MONITORING) || event.latency != 0))
            //if (local == true && (event.getEventType().waitsForSpeed() == false || event.getLatency() != 0))
            //   (local == false && event.getEventType() == EventType.MAN_SPEEDTEST))  // but for speedtest, wait until complete
            if ((event.getEventType().waitsForSpeed() == false || event.getLatency() != 0)) {
                if (event.getLocalID() > 0 && eventData.getFltEventLng() != 0) {
                    owner.getReportManager().updateEventField(event.getLocalID(), Events.KEY_LATITUDE,
                            String.valueOf(eventData.getFltEventLat()));
                    owner.getReportManager().updateEventField(event.getLocalID(), Events.KEY_LONGITUDE,
                            String.valueOf(eventData.getFltEventLng()));
                } else if (event.getLocalID() == 0) {
                    int evtID = owner.getReportManager().storeEvent(eventData); // .reportEvent (eventData, event, local, location);
                    event.setLocalID(evtID);
                    eventData.setCallID(evtID);
                }
                if (complimentaryEvent != null) {
                    if (complimentaryEvent.getLocalID() > 0 && location != null) {
                        owner.getReportManager().updateEventField(complimentaryEvent.getLocalID(),
                                Events.KEY_LATITUDE, String.valueOf(location.getLatitude()));
                        owner.getReportManager().updateEventField(complimentaryEvent.getLocalID(),
                                Events.KEY_LONGITUDE, String.valueOf(location.getLongitude()));
                    } else if (complimentaryEvent.getLocalID() == 0) {
                        int evtID = owner.getReportManager().storeEvent(eventData); //(eventData2, complimentaryEvent, local, location);
                        complimentaryEvent.setLocalID(evtID);
                    }
                }
            }
            if (local)
                return;

            //if the complimentary event is not null, then this event must be
            //the "starting end" of an event couple. If so, then this event should
            //be uploaded alongside the main event
            if (complimentaryEvent != null && local == false) {
                eventData2 = generateEventDataFromEvent(complimentaryEvent, local);
                if (eventData2 != null) {
                    //complimentaryEvent.setFlag (EventObj.SERVER_SENDING, true);
                    eventData2.setCallID(complimentaryEvent.getLocalID());
                    eventDataList.add(eventData2);
                }
            }
            //now create the eventDataEnvelope to wrap the list of eventData variables
            //along with other required variables
            String phoneNumFirst4 = "";
            if (strPhone != null && strPhone.length() > 4)
                phoneNumFirst4 = strPhone.substring(0, 4);

            eventDataEnvelope = generateEventDataEnvFromEventList(eventDataList, phoneNumFirst4);
            // when event is filled in, travel and fillin would like to see it before sending
            if (owner.isServiceRunning() && owner.getTravelDetector() != null)
                owner.getTravelDetector().eventCompleted(event);
        } else // null event create dummy event envelope to trigger sending the event queue (without adding a new event)
            eventDataEnvelope = new EventDataEnvelope();

        boolean bSent = false, bFromQueue = false, bAddedQueue = false;
        loadEventsQueue(); // only loads if queue hasn't loaded yet (ensure loaded)
        ConcurrentLinkedQueue<EventDataEnvelope> eventQueue = owner.getEventManager().getEventQueue();

        // Send this event and any other events that were in the queue, as long as it didn't fail to send
        while (eventDataEnvelope != null) {
            bSent = uploadEventEnvelope(eventDataEnvelope, bFromQueue); // as long as event was sent to server, it sent (even if server had an error)
            if (!bSent) {
                //if (!bFromQueue)
                {
                    bAddedQueue = true;
                    eventQueue.add(eventDataEnvelope);
                    while (eventQueue.size() > 200)
                        eventQueue.poll();
                }
                break;
            } else {
                eventDataEnvelope = eventQueue.poll();
                bFromQueue = true;
            }
        }
        // persist the queue every 3 hrs in case something happens
        if (event != null && (event.isCheckin || bAddedQueue))
            saveEvents(eventQueue);
    } finally {
        if (!local) {
            //LoggerUtil.logToFile(LoggerUtil.Level.DEBUG, TAG, "report(false)", "uploadingEvent(false)");
            owner.uploadingEvent(false);

        }
    }
}

From source file:de.mmbbs.io.socket.IOConnection.java

/**
 * Transport connected./*from   w  w  w .j a  v a2  s  .  c o m*/
 * 
 * {@link IOTransport} calls this when a connection is established.
 */
public synchronized void transportConnected() {
    setState(STATE_READY);
    if (reconnectTask != null) {
        reconnectTask.cancel();
        reconnectTask = null;
    }
    if (heartBeatTask != null) {
        heartBeatTask.cancel();
    }
    heartBeatTask = new HeartBeatTask(); // heartbeat loop
    backgroundTimer.schedule(heartBeatTask, heartbeatTimeout, heartbeatTimeout);
    resetTimeout();
    if (transport.canSendBulk()) {
        ConcurrentLinkedQueue<String> outputBuffer = this.outputBuffer;
        this.outputBuffer = new ConcurrentLinkedQueue<String>();
        try {
            // DEBUG
            String[] texts = outputBuffer.toArray(new String[outputBuffer.size()]);
            logger.info("Bulk start:");
            for (String text : texts) {
                logger.info("> " + text);
            }
            logger.info("Bulk end");
            // DEBUG END
            transport.sendBulk(texts);
        } catch (IOException e) {
            this.outputBuffer = outputBuffer;
        }
    } else {
        String text;
        while ((text = outputBuffer.poll()) != null)
            sendPlain(text);
    }
    this.keepAliveInQueue = false;
}

From source file:com.xeiam.xchange.streaming.socketio.IOConnection.java

/**
 * Transport connected. {@link IOTransport} calls this when a connection is established.
 *///from   w w w .jav  a  2s . co  m
public void transportConnected() {

    setState(STATE_READY);
    if (reconnectTask != null) {
        reconnectTask.cancel();
        reconnectTask = null;
    }
    resetTimeout();
    synchronized (outputBuffer) {
        if (transport.canSendBulk()) {
            ConcurrentLinkedQueue<String> outputBuffer = this.outputBuffer;
            this.outputBuffer = new ConcurrentLinkedQueue<String>();
            try {
                // DEBUG
                String[] texts = outputBuffer.toArray(new String[outputBuffer.size()]);
                log.debug("Bulk start:");
                for (String text : texts) {
                    log.debug("> " + text);
                }
                log.debug("Bulk end");
                // DEBUG END
                transport.sendBulk(texts);
            } catch (IOException e) {
                this.outputBuffer = outputBuffer;
            }
        } else {
            String text;
            while ((text = outputBuffer.poll()) != null) {
                sendPlain(text);
            }
        }
        this.keepAliveInQueue = false;
    }
}

From source file:com.ibm.crail.tools.CrailBenchmark.java

private void warmUp(String filename, int operations, ConcurrentLinkedQueue<CrailBuffer> bufferList)
        throws Exception {
    Random random = new Random();
    String warmupFilename = filename + random.nextInt();
    System.out.println("warmUp, warmupFile " + warmupFilename + ", operations " + operations);
    if (operations > 0) {
        CrailFile warmupFile = fs.create(warmupFilename, CrailNodeType.DATAFILE, CrailStorageClass.DEFAULT,
                CrailLocationClass.DEFAULT).get().asFile();
        CrailBufferedOutputStream warmupStream = warmupFile.getBufferedOutputStream(0);
        for (int i = 0; i < operations; i++) {
            CrailBuffer buf = bufferList.poll();
            buf.clear();/*from   w w w  . ja  va 2 s . c  o m*/
            warmupStream.write(buf.getByteBuffer());
            bufferList.add(buf);
        }
        warmupStream.purge().get();
        warmupStream.close();
        fs.delete(warmupFilename, false).get().syncDir();
    }
}

From source file:org.glassfish.grizzly.memcached.pool.BaseObjectPoolBenchmark.java

@Test
public void testBenchmarkingInMultiThreads() {
    final int threadCount = 1000;
    final int poolSize = 150;
    final KeyedPoolableObjectFactory<String, String> apacheFactory = new KeyedPoolableObjectFactory<String, String>() {
        private static final String VALUE_NAME = "value";
        private int id;
        private int count;

        @Override/*ww  w .  j ava2 s  . co m*/
        public synchronized String makeObject(String s) throws Exception {
            count++;
            return VALUE_NAME + ++id;
        }

        @Override
        public synchronized void destroyObject(String s, String s1) throws Exception {
            count--;
        }

        @Override
        public boolean validateObject(String s, String s1) {
            return true;
        }

        @Override
        public void activateObject(String s, String s1) throws Exception {
        }

        @Override
        public void passivateObject(String s, String s1) throws Exception {
        }
    };
    final GenericKeyedObjectPool<String, String> apachePool = new GenericKeyedObjectPool<String, String>(
            apacheFactory, poolSize, GenericKeyedObjectPool.WHEN_EXHAUSTED_FAIL, 0, poolSize, poolSize,
            poolSize, false, false, 1000 * 60 * 60, 0, 1000 * 60 * 60, false);

    final ConcurrentLinkedQueue<String> borrowObjects = new ConcurrentLinkedQueue<String>();
    final CountDownLatch startFlag = new CountDownLatch(1);
    final CountDownLatch finishFlag = new CountDownLatch(threadCount * 2);
    final AtomicInteger exceptionCnt = new AtomicInteger();
    final AtomicInteger successCnt = new AtomicInteger();
    for (int i = 0; i < threadCount; i++) {
        final Thread borrowThread = new Thread() {
            @Override
            public void run() {
                try {
                    startFlag.await();
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
                for (int j = 0; j < 30; j++) {
                    try {
                        final String object = apachePool.borrowObject("key");
                        Assert.assertNotNull(object);
                        successCnt.incrementAndGet();
                        Assert.assertTrue(borrowObjects.offer(object));
                    } catch (Exception ignore) {
                        exceptionCnt.incrementAndGet();
                    }
                }
                finishFlag.countDown();
            }
        };
        borrowThread.start();

        final Thread returnThread = new Thread() {
            @Override
            public void run() {
                try {
                    startFlag.await();
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
                for (int j = 0; j < 30; j++) {
                    try {
                        final String object = borrowObjects.poll();
                        if (object != null) {
                            apachePool.returnObject("key", object);
                        }
                    } catch (Exception e) {
                        Assert.fail(e.getMessage());
                    }
                }
                finishFlag.countDown();
            }
        };
        returnThread.start();
    }
    long startTime = System.currentTimeMillis();
    startFlag.countDown();
    try {
        finishFlag.await();
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }
    logger.info("apache common-pool elapse = {}", (System.currentTimeMillis() - startTime));
    try {
        logger.info("apache common-pool max gen-id = {}", apacheFactory.makeObject("key"));
    } catch (Exception ignore) {
    }
    logger.info("apache common-pool success counts = {}", successCnt.get());
    logger.info("apache common-pool exception counts = {}", exceptionCnt.get());

    try {
        apachePool.close();
    } catch (Exception ignore) {
    }

    // grizzly
    final PoolableObjectFactory<String, String> grizzlyFactory = new PoolableObjectFactory<String, String>() {
        private static final String VALUE_NAME = "value";
        private int id;
        private int count;

        @Override
        public synchronized String createObject(String s) throws Exception {
            count++;
            return VALUE_NAME + ++id;
        }

        @Override
        public synchronized void destroyObject(String s, String s1) throws Exception {
            count--;
        }

        @Override
        public boolean validateObject(String s, String s1) {
            return true;
        }
    };
    final BaseObjectPool.Builder<String, String> builder = new BaseObjectPool.Builder<String, String>(
            grizzlyFactory);
    builder.disposable(false);
    builder.keepAliveTimeoutInSecs(-1);
    builder.borrowValidation(false);
    builder.returnValidation(false);
    builder.max(poolSize);
    builder.min(poolSize);
    final ObjectPool<String, String> grizzlyPool = builder.build();

    final ConcurrentLinkedQueue<String> borrowObjects2 = new ConcurrentLinkedQueue<String>();
    final CountDownLatch startFlag2 = new CountDownLatch(1);
    final CountDownLatch finishFlag2 = new CountDownLatch(threadCount * 2);
    final AtomicInteger exceptionCnt2 = new AtomicInteger();
    final AtomicInteger successCnt2 = new AtomicInteger();
    for (int i = 0; i < threadCount; i++) {
        final Thread borrowThread = new Thread() {
            @Override
            public void run() {
                try {
                    startFlag2.await();
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
                for (int j = 0; j < 30; j++) {
                    try {
                        final String object = grizzlyPool.borrowObject("key", 0);
                        Assert.assertNotNull(object);
                        successCnt2.incrementAndGet();
                        Assert.assertTrue(borrowObjects2.offer(object));
                    } catch (Exception ignore) {
                        exceptionCnt2.incrementAndGet();
                    }
                }
                finishFlag2.countDown();
            }
        };
        borrowThread.start();

        final Thread returnThread = new Thread() {
            @Override
            public void run() {
                try {
                    startFlag2.await();
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
                for (int j = 0; j < 30; j++) {
                    try {
                        final String object = borrowObjects2.poll();
                        if (object != null) {
                            grizzlyPool.returnObject("key", object);
                        }
                    } catch (Exception e) {
                        Assert.fail(e.getMessage());
                    }
                }
                finishFlag2.countDown();
            }
        };
        returnThread.start();
    }
    startTime = System.currentTimeMillis();
    startFlag2.countDown();
    try {
        finishFlag2.await();
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }
    logger.info("grizzly pool elapse = {}", (System.currentTimeMillis() - startTime));
    try {
        logger.info("grizzly pool max gen-id = {}", grizzlyFactory.createObject("key"));
    } catch (Exception ignore) {
    }
    logger.info("grizzly pool success counts = {}", successCnt2.get());
    logger.info("grizzly pool exception counts= {}", exceptionCnt2.get());

    grizzlyPool.destroy();
}

From source file:spade.storage.Neo4j.java

public static void index(String dbpath, boolean printProgress) {

    int totalThreads = Runtime.getRuntime().availableProcessors();
    final ConcurrentLinkedQueue<Node> nodeTaskQueue = new ConcurrentLinkedQueue<Node>();
    final ConcurrentLinkedQueue<Relationship> edgeTaskQueue = new ConcurrentLinkedQueue<Relationship>();
    final ReentrantReadWriteLock nodeRwlock = new ReentrantReadWriteLock();
    final ReentrantReadWriteLock edgeRwlock = new ReentrantReadWriteLock();
    final Index<Node> vertexIndex;
    final RelationshipIndex edgeIndex;
    System.out.println("Loading database...");
    File databaseFile = new File(dbpath);
    final GraphDatabaseService graphDb = new GraphDatabaseFactory().newEmbeddedDatabaseBuilder(databaseFile)
            .setConfig(GraphDatabaseSettings.pagecache_memory,
                    "" + (Runtime.getRuntime().totalMemory() * 9) / 10)
            // .setConfig(GraphDatabaseSettings.keep_logical_logs, "false")
            .newGraphDatabase();/*from   ww w  .  ja  v  a2s .c  om*/

    System.out.println("Loaded");
    // clear already present indexes
    try (Transaction tx = graphDb.beginTx()) {
        graphDb.index().forNodes(spade.storage.Neo4j.VERTEX_INDEX).delete();
        tx.success();
    }

    try (Transaction tx = graphDb.beginTx()) {
        graphDb.index().forRelationships(spade.storage.Neo4j.EDGE_INDEX).delete();
        tx.success();
    }
    //

    System.out.println("Creating Indexing discriptors...");

    try (Transaction tx = graphDb.beginTx()) {
        vertexIndex = graphDb.index().forNodes(spade.storage.Neo4j.VERTEX_INDEX);
        tx.success();
    }

    try (Transaction tx = graphDb.beginTx()) {
        edgeIndex = graphDb.index().forRelationships(spade.storage.Neo4j.EDGE_INDEX);
        tx.success();
    }

    System.out.println("Created");

    class NodeIndexer implements Runnable {

        public void run() {

            Transaction tx = graphDb.beginTx();
            int counter = 0;
            try {
                while (!Thread.currentThread().isInterrupted()) {

                    if (counter < 10000) {
                        Node node = nodeTaskQueue.poll();
                        if (node == null) {
                            continue;
                        }

                        for (String key : node.getPropertyKeys()) {
                            vertexIndex.add(node, key, (String) node.getProperty(key));
                        }
                        node.setProperty(ID_STRING, node.getId());
                        vertexIndex.add(node, ID_STRING, Long.toString(node.getId()));

                        counter++;
                    }

                    if (counter > 1000 && nodeRwlock.writeLock().tryLock()) {
                        tx.success();
                        tx.close();
                        tx = graphDb.beginTx();
                        nodeRwlock.writeLock().unlock();
                        counter = 0;
                    }

                }

            } finally {
                // tx.success();
                tx.close();
                if (nodeRwlock.writeLock().isHeldByCurrentThread()) {
                    nodeRwlock.writeLock().unlock();
                }
            }
        }
    }

    class RelationshipIndexer implements Runnable {

        public void run() {

            Transaction tx = graphDb.beginTx();
            int counter = 0;
            try {
                while (!Thread.currentThread().isInterrupted()) {

                    if (counter < 10000) {
                        Relationship relationship = edgeTaskQueue.poll();
                        if (relationship == null) {
                            continue;
                        }

                        for (String key : relationship.getPropertyKeys()) {
                            edgeIndex.add(relationship, key, (String) relationship.getProperty(key));
                        }
                        relationship.setProperty(ID_STRING, relationship.getId());
                        edgeIndex.add(relationship, ID_STRING, Long.toString(relationship.getId()));

                        counter++;
                    }

                    if (counter > 1000 && edgeRwlock.writeLock().tryLock()) {
                        // tx.success();
                        tx.close();
                        tx = graphDb.beginTx();
                        edgeRwlock.writeLock().unlock();
                        counter = 0;
                    }

                }

            } finally {
                // tx.success();
                tx.close();
                if (edgeRwlock.writeLock().isHeldByCurrentThread()) {
                    edgeRwlock.writeLock().unlock();
                }
            }

        }
    }

    ArrayList<Thread> nodeWorkers = new ArrayList<Thread>();
    for (int i = 0; i < totalThreads / 2; i++) {
        Thread th = new Thread(new NodeIndexer());
        nodeWorkers.add(th);
        th.start();
    }

    ArrayList<Thread> edgeWorkers = new ArrayList<Thread>();
    for (int i = 0; i < totalThreads / 2; i++) {
        Thread th = new Thread(new RelationshipIndexer());
        edgeWorkers.add(th);
        th.start();
    }

    System.out.println("Counted Nodes and Relationships to index...");
    final long total;

    try (Transaction tx = graphDb.beginTx()) {
        total = Iterators.count(graphDb.getAllNodes().iterator())
                + Iterators.count(graphDb.getAllRelationships().iterator());
        tx.success();
    }
    System.out.println("done.\n");

    long percentageCompleted = 0;
    int count = 0;

    try (Transaction tx = graphDb.beginTx()) {

        // index nodes
        Iterator<Node> nodeIterator = graphDb.getAllNodes().iterator();
        Iterator<Relationship> edgeIterator = graphDb.getAllRelationships().iterator();

        while (edgeIterator.hasNext() || nodeIterator.hasNext()) {

            if (nodeIterator.hasNext() && nodeTaskQueue.size() < 10000) {
                nodeTaskQueue.add(nodeIterator.next());
                count = count + 1;
            }

            if (edgeIterator.hasNext() && edgeTaskQueue.size() < 10000) {
                edgeTaskQueue.add(edgeIterator.next());
                count = count + 1;
            }

            if (printProgress) {

                if (((count * 100) / total) > percentageCompleted) {
                    Runtime rt = Runtime.getRuntime();
                    long totalMemory = rt.totalMemory() / 1024 / 1024;
                    long freeMemory = rt.freeMemory() / 1024 / 1024;
                    long usedMemory = totalMemory - freeMemory;
                    System.out.print("| Cores: " + rt.availableProcessors() + " | Threads: " + totalThreads
                            + " | Heap (MB) - total: " + totalMemory + " , " + (freeMemory * 100) / totalMemory
                            + "% free"
                            // + " | Total Objects (nodes + relationships) to Index: " + total
                            + " | Indexing Object (nodes + relationships): " + count + " / " + total
                            + " | Completed: " + percentageCompleted + " %" + " |\r");
                }

                percentageCompleted = (count * 100) / total;
            }

        }

        tx.success();
    }

    System.out.println("\n\nIndexing completed. Waiting for queues to clear...");

    try {
        while (nodeTaskQueue.size() != 0 || edgeTaskQueue.size() != 0) {
            Thread.sleep(1000);
        }
    } catch (InterruptedException exception) {

    }

    System.out.println("Queues cleared. Threads teardown started...");

    for (int i = 0; i < totalThreads / 2; i++) {
        nodeWorkers.get(i).interrupt();
        try {
            nodeWorkers.get(i).join();
        } catch (InterruptedException exception) {

        }
    }

    for (int i = 0; i < totalThreads / 2; i++) {
        edgeWorkers.get(i).interrupt();
        try {
            edgeWorkers.get(i).join();
        } catch (InterruptedException exception) {

        }
    }

    System.out.println("Database shutdown started...");
    graphDb.shutdown();
}

From source file:org.restcomm.app.qoslib.Services.Events.EventUploader.java

/**
 * Loads event requests from storage, and adds it to the queue 
 *//*from w  ww  .ja va  2s  . c  o m*/
protected void loadEventsQueue() {

    ConcurrentLinkedQueue<EventDataEnvelope> eventQueue = owner.getEventManager().getEventQueue();
    if (eventQueue == null) {
        eventQueue = new ConcurrentLinkedQueue<EventDataEnvelope>();
        owner.getEventManager().setEventQueue(eventQueue);
    } else
        return;

    Gson gson = new Gson();
    SharedPreferences secureSettings = MainService.getSecurePreferences(owner);
    if (secureSettings.contains(PreferenceKeys.Miscellaneous.EVENTS_QUEUE)) {
        try {
            String strQueue = secureSettings.getString(PreferenceKeys.Miscellaneous.EVENTS_QUEUE, "");
            //LoggerUtil.logToFile(LoggerUtil.Level.DEBUG, TAG, "loadQueue", strQueue);
            if (strQueue.length() < 100)
                return;
            JSONArray jsonqueue = new JSONArray(strQueue);
            for (int i = 0; i < jsonqueue.length(); i++) {
                JSONObject jsonRequest = jsonqueue.getJSONObject(i);
                //if(jsonRequest.getString("type").equals(EventDataEnvelope.TAG)) 
                {
                    EventDataEnvelope request = gson.fromJson(jsonRequest.toString(), EventDataEnvelope.class);
                    //EventDataEnvelope request = new EventDataEnvelope(jsonRequest);
                    eventQueue.add(request);
                }
            }
            // remove the oldest events until queue is below 1000
            while (eventQueue.size() > 300)
                eventQueue.poll();
        } catch (JSONException e) {
            LoggerUtil.logToFile(LoggerUtil.Level.ERROR, TAG, "loadEventsQueue",
                    "JSONException loading events from storage", e);
        } catch (Exception e) {
            LoggerUtil.logToFile(LoggerUtil.Level.ERROR, TAG, "loadEventsQueue",
                    "Exception loading events from storage", e);
        }
    }

}

From source file:com.btoddb.fastpersitentqueue.JournalMgrIT.java

@Test
public void testThreading() throws IOException, ExecutionException {
    final int numEntries = 10000;
    final int numPushers = 3;
    int numPoppers = 3;

    final Random pushRand = new Random(1000L);
    final Random popRand = new Random(1000000L);
    final ConcurrentLinkedQueue<FpqEntry> events = new ConcurrentLinkedQueue<FpqEntry>();
    final AtomicInteger pusherFinishCount = new AtomicInteger();
    final AtomicInteger numPops = new AtomicInteger();
    final AtomicLong pushSum = new AtomicLong();
    final AtomicLong popSum = new AtomicLong();

    mgr.setMaxJournalFileSize(1000);/* w w  w  . j  a  va2  s . com*/
    mgr.init();

    ExecutorService execSrvc = Executors.newFixedThreadPool(numPushers + numPoppers);

    Set<Future> futures = new HashSet<Future>();

    // start pushing
    for (int i = 0; i < numPushers; i++) {
        Future future = execSrvc.submit(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < numEntries; i++) {
                    try {
                        long x = idGen.incrementAndGet();
                        FpqEntry entry = mgr.append(new FpqEntry(x, new byte[100]));
                        events.offer(entry);
                        pushSum.addAndGet(x);
                        if (x % 500 == 0) {
                            System.out.println("pushed ID = " + x);
                        }
                        Thread.sleep(pushRand.nextInt(5));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                pusherFinishCount.incrementAndGet();
            }
        });
        futures.add(future);
    }

    // start popping
    for (int i = 0; i < numPoppers; i++) {
        Future future = execSrvc.submit(new Runnable() {
            @Override
            public void run() {
                while (pusherFinishCount.get() < numPushers || !events.isEmpty()) {
                    try {
                        FpqEntry entry;
                        while (null != (entry = events.poll())) {
                            if (entry.getId() % 500 == 0) {
                                System.out.println("popped ID = " + entry.getId());
                            }
                            popSum.addAndGet(entry.getId());
                            numPops.incrementAndGet();
                            mgr.reportTake(entry);
                            Thread.sleep(popRand.nextInt(5));
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        futures.add(future);
    }

    boolean finished = false;
    while (!finished) {
        try {
            for (Future f : futures) {
                f.get();
            }
            finished = true;
        } catch (InterruptedException e) {
            // ignore
            Thread.interrupted();
        }
    }

    assertThat(numPops.get(), is(numEntries * numPushers));
    assertThat(popSum.get(), is(pushSum.get()));
    assertThat(mgr.getJournalIdMap().entrySet(), hasSize(1));
    assertThat(FileUtils.listFiles(theDir, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE), hasSize(1));
}