当前位置: 首页>>代码示例>>Java>>正文


Java ConcurrentHashMap.reduce方法代码示例

本文整理汇总了Java中java.util.concurrent.ConcurrentHashMap.reduce方法的典型用法代码示例。如果您正苦于以下问题:Java ConcurrentHashMap.reduce方法的具体用法?Java ConcurrentHashMap.reduce怎么用?Java ConcurrentHashMap.reduce使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.util.concurrent.ConcurrentHashMap的用法示例。


在下文中一共展示了ConcurrentHashMap.reduce方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: main

import java.util.concurrent.ConcurrentHashMap; //导入方法依赖的package包/类
public static void main(String[] args) {

		ConcurrentHashMap<String, ConcurrentLinkedDeque<Operation>> userHash = new ConcurrentHashMap<>();
		HashFiller hashFiller = new HashFiller(userHash);

		Thread[] threads = new Thread[10];
		for (int i = 0; i < 10; i++) {
			threads[i] = new Thread(hashFiller);
			threads[i].start();
		}

		for (int i = 0; i < 10; i++) {
			try {
				threads[i].join();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}

		System.out.printf("Size: %d\n", userHash.size());

		userHash.forEach(10, (user, list) -> {
			System.out.printf("%s: %s: %d\n", Thread.currentThread().getName(), user, list.size());
		});

		userHash.forEachEntry(10, entry -> {
			System.out.printf("%s: %s: %d\n", Thread.currentThread().getName(), entry.getKey(),
					entry.getValue().size());
		});

		Operation op = userHash.search(10, (user, list) -> {
			for (Operation operation : list) {
				if (operation.getOperation().endsWith("1")) {
					return operation;
				}
			}
			return null;
		});

		System.out.printf("The operation we have found is: %s, %s, %s,\n", op.getUser(), op.getOperation(),
				op.getTime());

		ConcurrentLinkedDeque<Operation> operations = userHash.search(10, (user, list) -> {
			if (list.size() > 10) {
				return list;
			}
			return null;
		});

		System.out.printf("The user we have found is: %s: %d operations\n", operations.getFirst().getUser(),
				operations.size());

		int totalSize = userHash.reduce(10, (user, list) -> {
			return list.size();
		}, (n1, n2) -> {
			return n1 + n2;
		});

		System.out.printf("The total size is: %d\n", totalSize);

	}
 
开发者ID:PacktPublishing,项目名称:Java-9-Concurrency-Cookbook-Second-Edition,代码行数:62,代码来源:Main.java


注:本文中的java.util.concurrent.ConcurrentHashMap.reduce方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。