當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。