Example usage for java.util.concurrent ConcurrentHashMap entrySet

List of usage examples for java.util.concurrent ConcurrentHashMap entrySet

Introduction

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

Prototype

EntrySetView entrySet

To view the source code for java.util.concurrent ConcurrentHashMap entrySet.

Click Source Link

Usage

From source file:cn.leancloud.diamond.client.impl.DefaultDiamondSubscriber.java

/**
 * ?DataID//from  w  w  w  .  j  a va2  s .  c om
 * 
 * @param localModifySet
 * @return
 */
private String getProbeUpdateString() {
    // ?checkDataID:Group:MD5
    StringBuilder probeModifyBuilder = new StringBuilder();
    for (Entry<String, ConcurrentHashMap<String, CacheData>> cacheDatasEntry : this.cache.entrySet()) {
        String dataId = cacheDatasEntry.getKey();
        ConcurrentHashMap<String, CacheData> cacheDatas = cacheDatasEntry.getValue();
        if (null == cacheDatas) {
            continue;
        }
        for (Entry<String, CacheData> cacheDataEntry : cacheDatas.entrySet()) {
            final CacheData data = cacheDataEntry.getValue();
            // ???diamond server
            if (!data.isUseLocalConfigInfo()) {
                probeModifyBuilder.append(dataId).append(Constants.WORD_SEPARATOR);

                if (null != cacheDataEntry.getValue().getGroup()
                        && Constants.NULL != cacheDataEntry.getValue().getGroup()) {
                    probeModifyBuilder.append(cacheDataEntry.getValue().getGroup())
                            .append(Constants.WORD_SEPARATOR);
                } else {
                    probeModifyBuilder.append(Constants.WORD_SEPARATOR);
                }

                if (null != cacheDataEntry.getValue().getMd5()
                        && Constants.NULL != cacheDataEntry.getValue().getMd5()) {
                    probeModifyBuilder.append(cacheDataEntry.getValue().getMd5())
                            .append(Constants.LINE_SEPARATOR);
                } else {
                    probeModifyBuilder.append(Constants.LINE_SEPARATOR);
                }
            }
        }
    }
    String probeModifyString = probeModifyBuilder.toString();
    return probeModifyString;
}

From source file:org.kde.kdeconnect.UserInterface.DeviceFragment.java

void refreshUI() {
    //Log.e("DeviceFragment", "refreshUI");

    if (device == null || rootView == null) {
        return;//from   w  w w  .j a v  a  2  s.com
    }

    //Once in-app, there is no point in keep displaying the notification if any
    device.hidePairingNotification();

    mActivity.runOnUiThread(new Runnable() {
        @Override
        public void run() {

            if (device.isPairRequestedByPeer()) {
                ((TextView) rootView.findViewById(R.id.pair_message)).setText(R.string.pair_requested);
                rootView.findViewById(R.id.pair_progress).setVisibility(View.GONE);
                rootView.findViewById(R.id.pair_button).setVisibility(View.GONE);
                rootView.findViewById(R.id.pair_request).setVisibility(View.VISIBLE);
            } else {

                boolean paired = device.isPaired();
                boolean reachable = device.isReachable();
                boolean onData = NetworkHelper.isOnMobileNetwork(getContext());

                rootView.findViewById(R.id.pairing_buttons).setVisibility(paired ? View.GONE : View.VISIBLE);
                rootView.findViewById(R.id.not_reachable_message)
                        .setVisibility((paired && !reachable && !onData) ? View.VISIBLE : View.GONE);
                rootView.findViewById(R.id.on_data_message)
                        .setVisibility((paired && !reachable && onData) ? View.VISIBLE : View.GONE);

                try {
                    ArrayList<ListAdapter.Item> items = new ArrayList<>();

                    //Plugins button list
                    final Collection<Plugin> plugins = device.getLoadedPlugins().values();
                    for (final Plugin p : plugins) {
                        if (!p.hasMainActivity())
                            continue;
                        if (p.displayInContextMenu())
                            continue;

                        items.add(new PluginItem(p, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                p.startMainActivity(mActivity);
                            }
                        }));
                    }

                    //Failed plugins List
                    final ConcurrentHashMap<String, Plugin> failed = device.getFailedPlugins();
                    if (!failed.isEmpty()) {
                        if (errorHeader == null) {
                            errorHeader = new TextView(mActivity);
                            errorHeader.setPadding(0, ((int) (28 * getResources().getDisplayMetrics().density)),
                                    0, ((int) (8 * getResources().getDisplayMetrics().density)));
                            errorHeader.setOnClickListener(null);
                            errorHeader.setOnLongClickListener(null);
                            errorHeader.setText(getResources().getString(R.string.plugins_failed_to_load));
                        }
                        items.add(new CustomItem(errorHeader));
                        for (Map.Entry<String, Plugin> entry : failed.entrySet()) {
                            String pluginKey = entry.getKey();
                            final Plugin plugin = entry.getValue();
                            if (plugin == null) {
                                items.add(new SmallEntryItem(pluginKey));
                            } else {
                                items.add(
                                        new SmallEntryItem(plugin.getDisplayName(), new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                plugin.getErrorDialog(mActivity).show();
                                            }
                                        }));
                            }
                        }
                    }

                    ListView buttonsList = (ListView) rootView.findViewById(R.id.buttons_list);
                    ListAdapter adapter = new ListAdapter(mActivity, items);
                    buttonsList.setAdapter(adapter);

                    mActivity.invalidateOptionsMenu();

                } catch (IllegalStateException e) {
                    e.printStackTrace();
                    //Ignore: The activity was closed while we were trying to update it
                } catch (ConcurrentModificationException e) {
                    Log.e("DeviceActivity", "ConcurrentModificationException");
                    this.run(); //Try again
                }

            }
        }
    });

}

From source file:com.chinamobile.bcbsp.comm.MessageQueuesForDisk.java

/**
 * Current strategy is first find the max bucket in memory, and second get the
 * max queue in the bucket.//from  ww w . j  av  a 2s  .c  o m
 */
@Override
public String getMaxIncomingQueueIndex() {
    int maxSize = 0;
    int maxBucket = 0;
    // Find the max buckets in memory.
    for (int i = 0; i < this.hashBucketNumber; i++) {
        BucketMeta meta = this.incomingQueues.get(i);
        if (meta.count > maxSize && !meta.onDiskFlag) {
            maxSize = meta.count;
            maxBucket = i;
        }
    }
    // Find the max queue in the maxBucket.
    String maxQueueIndex = null;
    ConcurrentHashMap<String, ConcurrentLinkedQueue<IMessage>> queueMap = this.incomingQueues
            .get(maxBucket).queueMap;
    Entry<String, ConcurrentLinkedQueue<IMessage>> entry = null;
    Iterator<Entry<String, ConcurrentLinkedQueue<IMessage>>> it = queueMap.entrySet().iterator();
    maxSize = 0;
    while (it.hasNext()) {
        entry = it.next();
        if (entry.getValue().size() > maxSize) {
            maxSize = entry.getValue().size();
            maxQueueIndex = entry.getKey();
        }
    }
    return maxQueueIndex;
}

From source file:edu.berkeley.compbio.phyloutils.HugenholtzTaxonomyService.java

public synchronized Set<String> getCachedNamesForId(Integer id) {
    HashMultimap<String, Integer> nameToIdsMap = (HashMultimap<String, Integer>) nameToIdsMapStub.get();
    ConcurrentHashMap<String, Integer> nameToUniqueIdMap = (ConcurrentHashMap<String, Integer>) nameToUniqueIdMapStub
            .get();//from   w ww  .  ja v  a 2s  .com

    //PERF, need a BiMultiMap or something
    Set<String> result = new HashSet<String>();
    for (Map.Entry<String, Integer> entry : nameToUniqueIdMap.entrySet()) {
        if (entry.getValue().equals(id)) {
            result.add(entry.getKey());
        }
    }
    for (Map.Entry<String, Integer> entry : nameToIdsMap.entries()) {
        if (entry.getValue().equals(id)) {
            result.add(entry.getKey());
        }
    }
    return result;
}

From source file:com.chinamobile.bcbsp.comm.MessageQueuesForDisk.java

/**
 * Cache the bucket of messages indexed by bucketIndex onto disk file.
 * @param queuesBuckets/*from   ww  w  .j  a va2  s  .c o  m*/
 * @param bucketIndex
 * @param queuePath
 * @throws IOException
 */
private void saveBucket(ArrayList<BucketMeta> queuesBuckets, int bucketIndex, String queuePath)
        throws IOException {
    if (queuesBuckets.get(bucketIndex).countInMemory < this.countThresholdForBucket) {
        return;
    }
    LOG.info("[MessageQueuesForDisk] is saving the [" + queuePath + " Bucket-" + bucketIndex + "] >>> size = "
            + queuesBuckets.get(bucketIndex).countInMemory + ".");
    long start = System.currentTimeMillis();
    /* Clock */
    File messagesDataFileBucket;
    FileWriter fwMessagesData;
    BufferedWriter bwMessagesData;
    File messagesDataFileQueue = new File(this.messagesDataFile + "/" + queuePath);
    if (!messagesDataFileQueue.exists()) {
        if (!messagesDataFileQueue.mkdir()) {
            throw new IOException("Make dir " + messagesDataFileQueue + " fail!");
        }
    }
    messagesDataFileBucket = new File(messagesDataFileQueue + "/" + "bucket-" + bucketIndex);
    boolean isNewFile = false;
    // The bucket file does not exit, create it.
    if (!messagesDataFileBucket.exists()) {
        if (!messagesDataFileBucket.createNewFile()) {
            throw new IOException("Create bucket file" + messagesDataFileBucket + " failed!");
        }
        isNewFile = true;
    }
    // Append to the bucket file by line.
    fwMessagesData = new FileWriter(messagesDataFileBucket, true);
    bwMessagesData = new BufferedWriter(fwMessagesData, 65536);
    if (isNewFile) {
        // Write the file header.
        bwMessagesData.write(Constants.MSG_BUCKET_FILE_HEADER + "-" + queuePath + "-" + bucketIndex);
    }
    ConcurrentHashMap<String, ConcurrentLinkedQueue<IMessage>> queueMap = queuesBuckets
            .get(bucketIndex).queueMap;
    ConcurrentLinkedQueue<IMessage> tempQueue = null;
    Entry<String, ConcurrentLinkedQueue<IMessage>> entry = null;
    Iterator<Entry<String, ConcurrentLinkedQueue<IMessage>>> it = queueMap.entrySet().iterator();
    // Traverse the map of queues and cache them to disk file.
    while (it.hasNext()) {
        entry = it.next();
        String key = entry.getKey();
        tempQueue = entry.getValue();
        if (tempQueue.size() <= 0) {
            continue;
        }
        bwMessagesData.newLine();
        bwMessagesData.write(key + Constants.KV_SPLIT_FLAG + queueToString(tempQueue));
        this.sizeOfHashMapsInMem = this.sizeOfHashMapsInMem
                - (sizeOfRef + sizeOfInteger + sizeOfEmptyMessageQueue);
    } // while
    bwMessagesData.close();
    fwMessagesData.close();
    // Update the meta data of the bucket.
    BucketMeta meta = queuesBuckets.get(bucketIndex);
    // Update the size of messages data in memory.
    this.sizeOfMessagesDataInMem = this.sizeOfMessagesDataInMem - meta.lengthInMemory;
    this.countOfMessagesDataInMem = this.countOfMessagesDataInMem - meta.countInMemory;
    meta.onDiskFlag = true;
    meta.lengthInMemory = 0;
    meta.countInMemory = 0;
    meta.queueMap.clear();
    this.writeDiskTime = this.writeDiskTime + (System.currentTimeMillis() - start);
}

From source file:org.jpac.JPac.java

protected void prepareRemoteConnections()
        throws ConfigurationException, RemoteException, RemoteSignalException {
    if (remoteSignalsEnabled) {
        //start serving incoming remote signal requests
        RemoteSignalServer.start(remoteSignalPort);
        //instantiate outgoing remote signals
        ConcurrentHashMap<String, RemoteSignalConnection> remoteHosts = RemoteSignalRegistry.getInstance()
                .getRemoteHosts();/*from  www  . j a  v  a 2s . c  om*/
        for (Entry<String, RemoteSignalConnection> entry : remoteHosts.entrySet()) {
            entry.getValue().open();
        }
    }
}

From source file:org.jpac.JPac.java

protected void closeRemoteConnections() {
    final long CLOSECONNECTIONTIMEOUT = 3000000000L; // 3 sec
    try {//from   w w  w.  j  a v  a 2  s . c  o  m
        if (remoteSignalsEnabled) {
            if (Log.isInfoEnabled())
                Log.info("closing remote connections ...");
            //invoke closure of all open remote connections
            ConcurrentHashMap<String, RemoteSignalConnection> remoteHosts = RemoteSignalRegistry.getInstance()
                    .getRemoteHosts();
            for (Entry<String, RemoteSignalConnection> entry : remoteHosts.entrySet()) {
                entry.getValue().close();
            }
            //wait for all connections to close
            for (Entry<String, RemoteSignalConnection> entry : remoteHosts.entrySet()) {
                long timeoutTime = System.nanoTime() + CLOSECONNECTIONTIMEOUT;
                while (!entry.getValue().isClosed() && System.nanoTime() < timeoutTime)
                    ;
                if (!entry.getValue().isClosed()) {
                    Log.error("   failed to close remote connection to "
                            + entry.getValue().getRemoteJPacInstance());
                }
            }
            RemoteSignalRegistry.getInstance().stopWatchdog();
            if (Log.isInfoEnabled())
                Log.info("... closing of remote connections done");
        }
    } catch (Exception exc) {
        Log.error("Error:", exc);
    } catch (Error exc) {
        Log.error("Error:", exc);
    }
}

From source file:com.starit.diamond.client.impl.DefaultDiamondSubscriber.java

private void checkSnapshot() {
    for (Entry<String, ConcurrentHashMap<String, CacheData>> cacheDatasEntry : cache.entrySet()) {
        ConcurrentHashMap<String, CacheData> cacheDatas = cacheDatasEntry.getValue();
        if (null == cacheDatas) {
            continue;
        }/*  w w  w  .j a  v a 2s .  com*/
        for (Entry<String, CacheData> cacheDataEntry : cacheDatas.entrySet()) {
            final CacheData cacheData = cacheDataEntry.getValue();
            // ??diamond server???,snapshot
            if (!cacheData.isUseLocalConfigInfo() && cacheData.getFetchCount() == 0) {
                String configInfo = getSnapshotConfiginfomation(cacheData.getDataId(), cacheData.getGroup());
                if (configInfo != null) {
                    popConfigInfo(cacheData, configInfo, RotateType.SNAPSHOT);
                }
            }
        }
    }
}

From source file:com.starit.diamond.client.impl.DefaultDiamondSubscriber.java

private void checkLocalConfigInfo() {
    for (Entry<String/* dataId */, ConcurrentHashMap<String/* group */, CacheData>> cacheDatasEntry : cache
            .entrySet()) {//from  w  ww.j  ava 2 s .  c  o m
        ConcurrentHashMap<String, CacheData> cacheDatas = cacheDatasEntry.getValue();
        if (null == cacheDatas) {
            continue;
        }
        for (Entry<String, CacheData> cacheDataEntry : cacheDatas.entrySet()) {
            final CacheData cacheData = cacheDataEntry.getValue();
            try {
                String configInfo = getLocalConfigureInfomation(cacheData);
                if (null != configInfo) {
                    if (log.isInfoEnabled()) {
                        log.info("???, dataId:" + cacheData.getDataId() + ", group:"
                                + cacheData.getGroup());
                    }
                    popConfigInfo(cacheData, configInfo, RotateType.LOCAL);
                    continue;
                }
                if (cacheData.isUseLocalConfigInfo()) {
                    continue;
                }
            } catch (Exception e) {
                log.error("????", e);
            }
        }
    }
}

From source file:org.jpac.JPac.java

protected void pushSignalsOverRemoteConnections() throws ConfigurationException, RemoteSignalException {
    if (remoteSignalsEnabled) {
        ConcurrentHashMap<String, RemoteSignalConnection> remoteHosts = RemoteSignalRegistry.getInstance()
                .getRemoteHosts();/* www.j a  v  a2s.c o  m*/
        //remoteHosts.entrySet().iterator()
        for (Entry<String, RemoteSignalConnection> entry : remoteHosts.entrySet()) {
            entry.getValue().pushSignals(cycleNumber);
        }
    }
}