Java Code Examples for java.util.concurrent.ConcurrentMap#forEach()
The following examples show how to use
java.util.concurrent.ConcurrentMap#forEach() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConcurrentRemoveIf.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
private void testMap(ConcurrentMap<Integer, Integer> map, Consumer<ConcurrentMap<Integer, Integer>> action) throws InterruptedException { // put 0's fillMap(map, 0); // To start working simultaneously CyclicBarrier threadStarted = new CyclicBarrier(2); // This task puts 1's into map CompletableFuture<Void> putter = CompletableFuture.runAsync( awaitOn(threadStarted, () -> fillMap(map, 1)), executorService); // This task performs the map action to remove all 0's from map CompletableFuture<Void> remover = CompletableFuture.runAsync( awaitOn(threadStarted, () -> action.accept(map)), executorService); // Wait for both tasks to complete CompletableFuture.allOf(putter, remover).join(); assertEquals(map.size(), SIZE, "Map size incorrect"); map.forEach((k, v) -> assertEquals(v, (Integer)1)); }
Example 2
Source File: HttpHandler.java From smartacus-mqtt-broker with Apache License 2.0 | 5 votes |
private void getTopics(ChannelHandlerContext ctx, FullHttpRequest req) { ArrayList<TopicVO> vos = new ArrayList<>(); ConcurrentMap<String, List<ClientSub>> topicSubers = PostMan.topicSubers; if(null !=topicSubers && !topicSubers.isEmpty()){ topicSubers.forEach((k,v)->{ TopicVO vo=new TopicVO(); vo.setTopic(k); List<SubClient> scList= Lists.newArrayList(); if(!v.isEmpty()){ v.forEach(e->{ SubClient client=new SubClient(); client.setClientId(e.getClientId()); client.setQos(e.getSubQos()); scList.add(client); }); } vo.setClientList(scList); vos.add(vo); }); } // 1.设置响应 Result result= new Result<Object>().ok(vos); FullHttpResponse resp = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.copiedBuffer(JSONObject.toJSONString(result), CharsetUtil.UTF_8)); resp.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html; charset=UTF-8"); // 2.发送 // 注意必须在使用完之后,close channel ctx.writeAndFlush(resp).addListener(ChannelFutureListener.CLOSE); }
Example 3
Source File: DefaultOvsdbClient.java From onos with Apache License 2.0 | 5 votes |
/** * Helper method which retrieves mirrorings statistics using bridge uuid. * * @param bridgeUuid the uuid of the bridge * @return the list of the mirrorings statistics. */ private List<MirroringStatistics> getMirrorings(Uuid bridgeUuid) { DatabaseSchema dbSchema = schema.get(DATABASENAME); if (dbSchema == null) { log.warn("Unable to retrieve dbSchema {}", DATABASENAME); return null; } OvsdbRowStore rowStore = getRowStore(DATABASENAME, BRIDGE); if (rowStore == null) { log.warn("Unable to retrieve rowStore {} of {}", BRIDGE, DATABASENAME); return null; } Row bridgeRow = rowStore.getRow(bridgeUuid.value()); Bridge bridge = (Bridge) TableGenerator. getTable(dbSchema, bridgeRow, OvsdbTable.BRIDGE); Set<Uuid> mirroringsUuids = (Set<Uuid>) ((OvsdbSet) bridge .getMirrorsColumn().data()).set(); OvsdbRowStore mirrorRowStore = getRowStore(DATABASENAME, MIRROR); if (mirrorRowStore == null) { log.warn("Unable to retrieve rowStore {} of {}", MIRROR, DATABASENAME); return null; } List<MirroringStatistics> mirroringStatistics = new ArrayList<>(); ConcurrentMap<String, Row> mirrorTableRows = mirrorRowStore.getRowStore(); mirrorTableRows.forEach((key, row) -> { if (!mirroringsUuids.contains(Uuid.uuid(key))) { return; } Mirror mirror = (Mirror) TableGenerator .getTable(dbSchema, row, OvsdbTable.MIRROR); mirroringStatistics.add(MirroringStatistics.mirroringStatistics(mirror.getName(), (Map<String, Integer>) ((OvsdbMap) mirror .getStatisticsColumn().data()).map())); }); return ImmutableList.copyOf(mirroringStatistics); }
Example 4
Source File: DefaultOvsdbClient.java From onos with Apache License 2.0 | 5 votes |
private List<Controller> getControllers(Uuid bridgeUuid) { DatabaseSchema dbSchema = schema.get(DATABASENAME); if (dbSchema == null) { return null; } OvsdbRowStore rowStore = getRowStore(DATABASENAME, BRIDGE); if (rowStore == null) { log.debug("There is no bridge table"); return null; } Row bridgeRow = rowStore.getRow(bridgeUuid.value()); Bridge bridge = (Bridge) TableGenerator. getTable(dbSchema, bridgeRow, OvsdbTable.BRIDGE); //FIXME remove log log.warn("type of controller column", bridge.getControllerColumn() .data().getClass()); Set<Uuid> controllerUuids = (Set<Uuid>) ((OvsdbSet) bridge .getControllerColumn().data()).set(); OvsdbRowStore controllerRowStore = getRowStore(DATABASENAME, CONTROLLER); if (controllerRowStore == null) { log.debug("There is no controller table"); return null; } List<Controller> ovsdbControllers = new ArrayList<>(); ConcurrentMap<String, Row> controllerTableRows = controllerRowStore.getRowStore(); controllerTableRows.forEach((key, row) -> { if (!controllerUuids.contains(Uuid.uuid(key))) { return; } Controller controller = (Controller) TableGenerator .getTable(dbSchema, row, OvsdbTable.CONTROLLER); ovsdbControllers.add(controller); }); return ovsdbControllers; }
Example 5
Source File: Lesson2.java From Java-Concurrency-Multithreading-in-Practice with MIT License | 4 votes |
private static void demoConcurrentHashMap() throws InterruptedException { Random random = new Random(); ExecutorService service = Executors.newCachedThreadPool(); String brandNewShoes = "Brand new shows"; String oldPhone = "Old phone"; String leatherHat = "Leather hat"; String cowboyShoes = "Cowboy shoes"; ConcurrentMap<String, String> itemToBuyerMap = new ConcurrentHashMap<>(); BiConsumer<String, String> buyItemIfNotTaken = (buyer, item) -> { try { TimeUnit.MILLISECONDS.sleep(random.nextInt(1000)); itemToBuyerMap.putIfAbsent(item, buyer); } catch (Exception e) { e.printStackTrace(); } }; service.submit(() -> { buyItemIfNotTaken.accept("Alice", brandNewShoes); buyItemIfNotTaken.accept("Alice", cowboyShoes); buyItemIfNotTaken.accept("Alice", leatherHat); }); service.submit(() -> { buyItemIfNotTaken.accept("Bob", brandNewShoes); buyItemIfNotTaken.accept("Bob", cowboyShoes); buyItemIfNotTaken.accept("Bob", leatherHat); }); service.submit(() -> { buyItemIfNotTaken.accept("Carol", brandNewShoes); buyItemIfNotTaken.accept("Carol", cowboyShoes); buyItemIfNotTaken.accept("Carol", leatherHat); }); service.awaitTermination(2000, TimeUnit.MILLISECONDS); itemToBuyerMap .forEach((item, buyer) -> System.out.printf("%s bought by %s%n", item, buyer)); }
Example 6
Source File: PeerExchange_IT.java From bt with Apache License 2.0 | 4 votes |
public void testPeerExchange() { ConcurrentMap<Peer, Set<ConnectionKey>> discoveredPeers = new ConcurrentHashMap<>(); swarm.getSeeders().forEach(seeder -> seeder.getHandle().startAsync(state -> { Set<ConnectionKey> peerPeers = discoveredPeers.get(seeder.getPeer()); if (peerPeers == null) { peerPeers = ConcurrentHashMap.newKeySet(); Set<ConnectionKey> existing = discoveredPeers.putIfAbsent(seeder.getPeer(), peerPeers); if (existing != null) { peerPeers = existing; } } peerPeers.addAll(state.getConnectedPeers()); }, 1000)); try { Thread.sleep(20000L); } catch (InterruptedException e) { throw new RuntimeException("Test unexpectedly interrupted", e); } Set<Peer> swarmPeers = new HashSet<>(); swarmPeers.addAll(swarm.getSeeders().stream().map(SwarmPeer::getPeer).collect(Collectors.toSet())); LOGGER.info("Swarm peers:"); swarmPeers.forEach(peer -> { LOGGER.info("{} ({})", peer, peer.getClass()); }); assertTrue(discoveredPeers.keySet().containsAll(swarmPeers)); discoveredPeers.forEach((peer, peers) -> { LOGGER.info("Peer {} discovered the following peers:", peer); peers.forEach(discoveredPeer -> { LOGGER.info("{} ({})", discoveredPeer, discoveredPeer.getClass()); }); for (Peer swarmPeer : swarmPeers) { // TODO: this must be updated after switching to ConnectionKeys // assertContainsPeer(peers, swarmPeer); } }); }