Example usage for java.util.concurrent ConcurrentLinkedQueue iterator

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

Introduction

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

Prototype

public Iterator<E> iterator() 

Source Link

Document

Returns an iterator over the elements in this queue in proper sequence.

Usage

From source file:com.taobao.weex.ui.component.list.template.WXRecyclerTemplateList.java

/**
 * copy cell async and save to cache/*  w w  w .  j  av a  2  s. c o m*/
 * */
private void asyncLoadTemplateCache(final String template) {
    if (Thread.currentThread() != Looper.getMainLooper().getThread()) {
        if (listData == null || listData.size() == 0) {
            return;
        }
        boolean firstScreenContains = false;
        for (int i = 0; i < listData.size(); i++) {
            if (template.equals(getTemplateKey(i))) {
                firstScreenContains = true;
                break;
            }
        }
        if (!firstScreenContains) {
            return;
        }
    }
    final WXCell source = mTemplateSources.get(template);
    if (source == null) {
        return;
    }
    TemplateCache cellCache = mTemplatesCache.get(template);
    if (cellCache == null) {
        cellCache = new TemplateCache();
        mTemplatesCache.put(template, cellCache);
    }
    if (cellCache.cells.size() > 0) {
        cellCache.isLoadIng = false;
        return;
    }
    if (cellCache.isLoadIng) {
        return;
    }
    cellCache.isLoadIng = true;
    AsyncTask<Void, Void, Void> preloadTask = new AsyncTask<Void, Void, Void>() {
        @Override
        protected Void doInBackground(Void... params) {
            TemplateCache cellCache = mTemplatesCache.get(template);
            if (cellCache == null || cellCache.cells == null) {
                return null;
            }
            while (cellCache.cells.size() < templateCacheSize) {
                WXCell component = (WXCell) copyCell(source);
                if (component == null) {
                    return null;
                }
                if (source.getInstance() == null || source.getInstance().isDestroy()) {
                    return null;
                }
                cellCache.cells.add(component);
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            if (source.getInstance() == null || source.getInstance().isDestroy()) {
                return;
            }
            final TemplateCache cellCache = mTemplatesCache.get(template);
            if (cellCache == null) {
                return;
            }
            if (cellCache.cells == null || cellCache.cells.size() == 0) {
                cellCache.isLoadIng = false;
                return;
            }
            Looper.myQueue().addIdleHandler(new MessageQueue.IdleHandler() {
                @Override
                public boolean queueIdle() {
                    if (source.getInstance() == null || source.getInstance().isDestroy()) {
                        return false;
                    }
                    ConcurrentLinkedQueue<WXCell> queue = cellCache.cells;
                    Iterator<WXCell> iterator = queue.iterator();
                    while (iterator.hasNext()) {
                        WXCell component = iterator.next();
                        if (component.isLazy()) {
                            doInitLazyCell(component, template, true);
                            return iterator.hasNext();
                        }
                    }
                    return false;
                }
            });
            cellCache.isLoadIng = false;
        }
    };
    preloadTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}

From source file:com.chinamobile.bcbsp.bspstaff.BSPStaff.java

@SuppressWarnings("unchecked")
private void aggregate(ConcurrentLinkedQueue<IMessage> messages, BSPJob job, Vertex vertex,
        int superStepCount) {
    try {/*from   ww  w  .java2 s  . c o  m*/
        for (Entry<String, Class<? extends AggregateValue<?, ?>>> entry : this.nameToAggregateValue
                .entrySet()) {

            String aggName = entry.getKey();

            // Init the aggregate value for this head node.
            AggregateValue aggValue1 = this.aggregateValuesCurrent.get(aggName);
            AggregationContext aggContext = new AggregationContext(job, vertex, superStepCount);
            publishAggregateValues(aggContext);
            aggValue1.initValue(messages.iterator(), aggContext);

            // Get the current aggregate value.
            AggregateValue aggValue0;
            aggValue0 = this.aggregateValues.get(aggName);

            // Get the aggregator for this kind of aggregate value.
            Aggregator<AggregateValue> aggregator;
            aggregator = (Aggregator<AggregateValue>) this.nameToAggregator.get(aggName).newInstance();

            // Aggregate
            if (aggValue0 == null) { // the first time aggregate.
                aggValue0 = (AggregateValue) aggValue1.clone();
                this.aggregateValues.put(aggName, aggValue0);
            } else {
                ArrayList<AggregateValue> tmpValues = new ArrayList<AggregateValue>();
                tmpValues.add(aggValue0);
                tmpValues.add(aggValue1);
                AggregateValue aggValue = aggregator.aggregate(tmpValues);
                this.aggregateValues.put(aggName, aggValue);
            }
        }
    } catch (InstantiationException e) {
        LOG.error("[BSPStaff:aggregate]", e);
    } catch (IllegalAccessException e) {
        LOG.error("[BSPStaff:aggregate]", e);
    }
}

From source file:com.chinamobile.bcbsp.bspstaff.BSPStaff.java

/**
 * Aggregate for peer compute./*  w  w  w.j a  v  a 2 s .c o  m*/
 * 
 * @param messages
 * @param job
 * @param peer
 * @param currentSuperStepCounter
 */
private void aggregate(ConcurrentLinkedQueue<IMessage> messages, BSPJob job, BSPPeer peer,
        int currentSuperStepCounter) {

    try {
        for (Entry<String, Class<? extends AggregateValue<?, ?>>> entry : this.nameToAggregateValue
                .entrySet()) {

            String aggName = entry.getKey();

            // Init the aggregate value for this head node.
            AggregateValue aggValue1 = this.aggregateValuesCurrent.get(aggName);
            AggregationContext aggContext = new AggregationContext(job, peer, currentSuperStepCounter);
            publishAggregateValues(aggContext);
            aggValue1.initValue(messages.iterator(), aggContext);

            // Get the current aggregate value.
            AggregateValue aggValue0;
            aggValue0 = this.aggregateValues.get(aggName);

            // Get the aggregator for this kind of aggregate value.
            Aggregator<AggregateValue> aggregator;
            aggregator = (Aggregator<AggregateValue>) this.nameToAggregator.get(aggName).newInstance();

            // Aggregate
            if (aggValue0 == null) { // the first time aggregate.
                aggValue0 = (AggregateValue) aggValue1.clone();
                this.aggregateValues.put(aggName, aggValue0);
            } else {
                ArrayList<AggregateValue> tmpValues = new ArrayList<AggregateValue>();
                tmpValues.add(aggValue0);
                tmpValues.add(aggValue1);
                AggregateValue aggValue = aggregator.aggregate(tmpValues);
                this.aggregateValues.put(aggName, aggValue);
            }
        }
    } catch (InstantiationException e) {
        LOG.error("[BSPStaff:aggregate]", e);
    } catch (IllegalAccessException e) {
        LOG.error("[BSPStaff:aggregate]", e);
    }

}

From source file:com.chinamobile.bcbsp.bspstaff.BSPStaff.java

@Override
public void peerProcessing(BSPPeer peer, BSP bsp, BSPJob job, int superStepCounter, BSPStaffContext context,
        boolean activeFlag) {
    // /**Feng added for migrate staff messages*/
    // ConcurrentLinkedQueue<IMessage> migrateMessages;
    String migrateMessages = null;
    StringBuffer sb = new StringBuffer();
    /** Clock *///  w w  w .j  ava 2s .  c  o  m
    long tmpStart = System.currentTimeMillis();
    if (peer == null) {
        throw new RuntimeException("No key-value to compute for staff " + this.getSid());
    }
    loadGraphTime = loadGraphTime + (System.currentTimeMillis() - tmpStart);
    // Get the incomed message queue for this peer(partition).
    try {
        ConcurrentLinkedQueue<IMessage> messages = this.communicator
                .getMessageQueue(String.valueOf(Constants.DEFAULT_PEER_DST_MESSSAGE_ID));
        // Aggregate the new values for each vertex. Clock the time
        // cost.
        publishAggregateValues(context);
        LOG.info("ljn test : peerProcessing messages size is " + messages.size());
        tmpStart = System.currentTimeMillis();
        aggregate(messages, job, peer, this.currentSuperStepCounter);
        aggregateTime = aggregateTime + (System.currentTimeMillis() - tmpStart);
        context.updatePeer(peer);
        if (superStepCounter > 0) {
            if (!activeFlag && (messages.size() == 0)) {
                return;
            }
        }
        Iterator<IMessage> messagesIter = messages.iterator();
        // Call the compute function for local computation.
        /*
         * Publish the total result aggregate values into the bsp's cache for the
         * user's function's accession in the next super step.
         */
        // publishAggregateValues(context);
        /** Clock */
        tmpStart = System.currentTimeMillis();
        try {
            // LOG.info("bsp.compute vertex message!");
            bsp.compute(messagesIter, context);
        } catch (Exception e) {
            throw new RuntimeException("catch exception", e);
        }
        computeTime = computeTime + (System.currentTimeMillis() - tmpStart);
        /** Clock */
        messages.clear();
        peer.resetPair();
        /** Clock */
        tmpStart = System.currentTimeMillis();
        collectMsgsTime = collectMsgsTime + (System.currentTimeMillis() - tmpStart);
    } catch (Exception e) {
        throw new RuntimeException("catch exception on peer compute", e);
    }
    /** Clock */
}

From source file:com.chinamobile.bcbsp.bspstaff.BSPStaff.java

@Override
public void vertexProcessing(Vertex v, BSP bsp, BSPJob job, int superStepCounter, BSPStaffContext context,
        boolean activeFlag) throws IOException {
    // /**Feng added for migrate staff messages*/
    // ConcurrentLinkedQueue<IMessage> migrateMessages;
    String migrateMessages = null;
    StringBuffer sb = new StringBuffer();
    /** Clock */// w  w  w  . j  a  va  2 s .c  o  m
    // long tmpStart = System.currentTimeMillis();
    if (v == null) {
        LOG.error("Fail to get the HeadNode of index[" + "] " + "and the system will skip the record");
        return;
    }
    // loadGraphTime = loadGraphTime + (System.currentTimeMillis() - tmpStart);
    // Get the incomed message queue for this vertex.
    ConcurrentLinkedQueue<IMessage> messages = this.communicator
            .getMessageQueue(String.valueOf(v.getVertexID()));
    if (this.migratedStaffFlag == true) {
        // LOG.info("Feng test use the migrated messages! "+v.getVertexID());
        messages = this.communicator// Feng test use the migrated messages
                .getMigrateMessageQueue(String.valueOf(v.getVertexID()));
    }
    // LOG.info("Feng test Messagesize! "+messages.size());
    // if (messages.size() > 0)
    // LOG.info("<TEST1>  V: " +v.getVertexID() +" MSG ID " +
    // messages.poll().getMessageId());

    // Aggregate the new values for each vertex. Clock the time
    // cost.
    // tmpStart = System.currentTimeMillis();
    aggregate(messages, job, v, this.currentSuperStepCounter);
    // aggregateTime = aggregateTime + (System.currentTimeMillis() - tmpStart);
    // Note Ever Fault.
    // Note Ever Edit /\2014-01-23
    context.refreshVertex(v);
    if (superStepCounter > 0) {
        if (!activeFlag && (messages.size() == 0)) {
            return;
        }
        // for activemap update
        else {
            upadateActiveBitMap(v);
        }
    }
    if (this.openMigrateMode == true && messages.size() > 0) {
        // this.migrateMessages.clear();
        // migrateMessages.addAll(messages);
        // LOG.info("Feng test messageSize "+messages.size());
        // for(IMessage mig:messages){
        // LOG.info("Feng test message "+mig.intoString());
        // }
        Iterator<IMessage> iterator = messages.iterator();
        sb.append(v.getVertexID().toString() + Constants.MESSAGE_SPLIT);
        while (iterator.hasNext()) {
            IMessage msg = iterator.next();
            String msgID = msg.getMessageId().toString();
            String msgValue = msg.getContent().toString();
            if (msgID != null) {
                sb.append(msgID + Constants.SPLIT_FLAG + msgValue + Constants.SPACE_SPLIT_FLAG);
            }
            migrateMessages = sb.toString();
            this.migrateMessagesString.add(migrateMessages);
        }
        // LOG.info("Feng test record migrate messages!");
    }
    Iterator<IMessage> messagesIter = messages.iterator();
    // Iterator<IMessage> messagesIterTest = messages.iterator();
    // if(messagesIterTest.hasNext()){
    // IMessage i = messagesIterTest.next();
    // LOG.info("Feng test staff messages" +i.getMessageId()
    // +"messagesSize! "+i.getContent());
    // }

    // LOG.info("Feng test! "+ ((PageRankMessage)
    // (messagesIterTest.next())).getContent());
    // Call the compute function for local computation.
    /*
     * Publish the total result aggregate values into the bsp's cache for the
     * user's function's accession in the next super step.
     */
    publishAggregateValues(context);
    /** Clock */
    // tmpStart = System.currentTimeMillis();
    try {
        // LOG.info("bsp.compute vertex message!");
        bsp.compute(messagesIter, context);
    } catch (Exception e) {
        throw new RuntimeException("catch exception", e);
    }
    // computeTime = computeTime + (System.currentTimeMillis() - tmpStart);
    /** Clock */
    messages.clear();
    /** Clock */
    // tmpStart = System.currentTimeMillis();
    // collectMsgsTime = collectMsgsTime + (System.currentTimeMillis() -
    // tmpStart);
    /** Clock */

}