赞
踩
import java.util.Random; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicInteger; /** * 实现方法 int count(String ip) 返回ip访问次数 */ public class IpCount { static volatile ConcurrentHashMap<String, AtomicInteger> count = new ConcurrentHashMap<>(); static final Object o = new Object(); public static int count(String ip) { if (count.keySet().contains(ip)) { return count.get(ip).incrementAndGet(); } synchronized (o) { if (count.keySet().contains(ip)) { return count.get(ip).incrementAndGet(); } else { count.put(ip, new AtomicInteger(1)); return 1; } } } public static void main(String[] args) throws InterruptedException { Thread[] threads = new Thread[10000]; Random r = new Random(); IpCount ipCount = new IpCount(); for (int i = 0; i < threads.length; i++) { threads[i] = new Thread(() -> { int ip = r.nextInt(5)+1; int a = ipCount.count(Integer.toString(ip)); }, "t" + i); } for (Thread t : threads) { t.start(); } Thread.sleep(5); System.out.println(ipCount.count); int sum = 0; for (AtomicInteger value : ipCount.count.values()) { sum +=value.get(); } System.out.println(sum); } }
import java.util.Random; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicInteger; /** * 实现方法 int count(String ip) 返回ip访问次数 */ public class IpCount { ConcurrentHashMap<String, AtomicInteger> count = new ConcurrentHashMap<>(); public int count(String ip) { if (count.keySet().contains(ip)) { return count.get(ip).incrementAndGet(); } else { count.put(ip, new AtomicInteger(1)); return 1; } } public static void main(String[] args) throws InterruptedException { Thread[] threads = new Thread[10000]; Random r = new Random(); IpCount ipCount = new IpCount(); for (int i = 0; i < threads.length; i++) { threads[i] = new Thread(() -> { int ip = r.nextInt(5)+1; int a = ipCount.count(Integer.toString(ip)); }, "t" + i); } for (Thread t : threads) { t.start(); } Thread.sleep(5); System.out.println(ipCount.count); int sum = 0; for (AtomicInteger value : ipCount.count.values()) { sum +=value.get(); } System.out.println(sum); } }
确实是出了一些问题,在意料之中
ConcurrentHashMap<String, AtomicInteger> count = new ConcurrentHashMap<>();
public int count(String ip) {
if (count.containsKey(ip)) {
return count.get(ip).incrementAndGet();
} else {
count.put(ip, new AtomicInteger(1));
return 1;
}
}
但如果使用count.putIfAbsent()还会更优
import java.util.Random; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicInteger; /** * 实现方法 int count(String ip) 返回ip访问次数 */ public class IpCount { ConcurrentHashMap<String, AtomicInteger> count = new ConcurrentHashMap<>(); public int count(String ip) { count.putIfAbsent(ip, new AtomicInteger(0)); return count.get(ip).incrementAndGet(); } public static void main(String[] args) throws InterruptedException { Thread[] threads = new Thread[10000]; Random r = new Random(); IpCount ipCount = new IpCount(); for (int i = 0; i < threads.length; i++) { threads[i] = new Thread(() -> { int ip = r.nextInt(5)+1; int a = ipCount.count(Integer.toString(ip)); }, "t" + i); } for (Thread t : threads) { t.start(); } Thread.sleep(5); System.out.println(ipCount.count); int sum = 0; for (AtomicInteger value : ipCount.count.values()) { sum +=value.get(); } System.out.println(sum); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。