一、初识computeIfAbsent
Java 8中的ConcurrentHashMap提供了一个能够将计算与缓存相结合的方法:computeIfAbsent。使用computeIfAbsent方法能够避免在多线程的情况下重复计算,提高程序的执行效率。
ConcurrentHashMap map = new ConcurrentHashMap();
map.computeIfAbsent("apple", key -> key.length());
System.out.println(map.get("apple")); //输出5
二、computeIfAbsent的函数式编程风格
作为Java 8的新特性,computeIfAbsent方法的函数式编程风格大大提高了开发效率。我们可以使用lambda表达式来编写计算逻辑,从而不需要再编写大量的逻辑代码。
ConcurrentHashMap map = new ConcurrentHashMap();
map.computeIfAbsent("apple", key -> {
int length = key.length();
return length;
});
System.out.println(map.get("apple")); //输出5
三、computeIfAbsent方法避免多线程重复计算
在多线程的情况下,computeIfAbsent方法可以避免重复计算,能够提高程序的执行效率。例如:
ConcurrentHashMap map = new ConcurrentHashMap();
ExecutorService executor = Executors.newFixedThreadPool(10);
for(int i=0; i {
map.computeIfAbsent("apple", key -> {
int length = key.length();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
return length;
});
});
}
executor.shutdown();
while(!executor.isTerminated()) {}
System.out.println(map.get("apple")); //输出5
在上面的例子中,我们使用10个线程来向map中插入100000个元素,每个元素都要执行10ms的等待时间。当我们使用computeIfAbsent方法时,每个元素只需要进行一次计算,因为多个线程之间共享同一个map对象。如果使用传统的方式,每个线程都会重复计算,造成大量的资源浪费。
四、computeIfAbsent的性能优化
对于一些耗时较长的操作,我们可以使用ConcurrentHashMap的compute方法来完成计算,从而提高程序的性能。
ConcurrentHashMap map = new ConcurrentHashMap();
map.compute("apple", (key, value) -> {
if(value == null) {
int length = key.length();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
return length;
} else {
return value;
}
});
System.out.println(map.get("apple")); //输出5
在上面的例子中,我们使用了compute方法来进行元素的计算。如果元素存在,则直接返回值,否则进行计算并插入map中。使用compute方法能够减少一些不必要的判断和操作,提高程序的性能。
五、computeIfAbsent对于非ConcurrentHashMap的应用
虽然computeIfAbsent方法是由ConcurrentHashMap提供的,但是我们也可以使用它来对其他类的操作进行优化。
Map map = new HashMap();
map.computeIfAbsent("apple", key -> key.length());
System.out.println(map.get("apple")); //输出5
虽然HashMap不是线程安全的容器,但是由于computeIfAbsent方法的特殊性质,我们依然可以使用它来对map中的元素进行计算和缓存。
原创文章,作者:CZPHH,如若转载,请注明出处:https://www.506064.com/n/334140.html
微信扫一扫
支付宝扫一扫