下面是我的类,它使用CountDownLatch
来确保当在主、次和三级映射上发生写入时,不会第一次在这些映射上发生读取。
public class ClientData {
public static class Mappings {
public final Map<String, Map<Integer, String>> primary;
public final Map<String, Map<Integer, String>> secondary;
public final Map<String, Map<Integer, String>> tertiary;
public Mappings(
Map<String, Map<Integer, String>> primary,
Map<String, Map<Integer, String>> secondary,
Map<String, Map<Integer, String>> tertiary
) {
this.primary = primary;
this.secondary = secondary;
this.tertiary = tertiary;
}
}
private static final AtomicReference<Mappings> mappings = new AtomicReference<>();
private static final CountDownLatch hasBeenInitialized = new CountDownLatch(1);
public static Mappings getMappings() {
try {
hasBeenInitialized.await();
return mappings.get();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new IllegalStateException(e);
}
}
public static void setMappings(
Map<String, Map<Integer, String>> primary,
Map<String, Map<Integer, String>> secondary,
Map<String, Map<Integer, String>> tertiary
) {
setMappings(new Mappings(primary, secondary, tertiary));
}
public static void setMappings(Mappings newMappings) {
mappings.set(newMappings);
hasBeenInitialized.countDown();
}
}
public class TempBackgroundThread {
// parse the response and store it in a variable
private void parseResponse(String response) {
//...
Map<String, Map<Integer, String>> primaryTables = null;
Map<String, Map<Integer, String>> secondaryTables = null;
Map<String, Map<Integer, String>> tertiaryTables = null;
//...
// store the three map data in ClientData class variables if anything has changed
// which can be used by other threads, this will be updated once every four or five months
if(changed) {
ClientData.setMappings(primaryTables, secondaryTables, tertiaryTables);
}
}
}
问题陈述:
如果我对mappings对象以及主要、次要和第三层映射进行各种空或健全性检查,性能会下降很多(不确定原因)。但如果我不做任何健全或空检查,性能就会非常好。谁能给我解释一下怎么了,为什么会这样?
下面是一个例子-
我正在使用clientdata
类获取主线程中的所有映射。正如您在下面看到的,我正在进行各种健全性检查,以确保mappings
、mappings.primary
、mappings.secondary
和mappings.tourity
不为空。如果它们为空,则记录错误并返回
class Task implements Callable<String> {
public Task() {
}
public String call() throws Exception {
int compId = 100;
String localPath = "hello";
String remotePath = "world";
Mappings mappings = ClientData.getMappings();
if (MyUtilityClass.isEmpty(mappings)
|| (MyUtilityClass.isEmpty(mappings.primary) && MyUtilityClass
.isEmpty(mappings.secondary))
|| MyUtilityClass.isEmpty(mappings.tertiary)) {
// log error and return
}
// otherwise extract values from them
String localPAddress = null;
String remotePAddress = null;
if (MyUtilityClass.isNotEmpty(mappings.primary)) {
String localPId = mappings.primary.get(localPath).get(compId);
localPAddress = mappings.tertiary.get(localPath).get(
Integer.parseInt(localPId));
String remotePId = mappings.primary.get(remotePath).get(compId);
remotePAddress = mappings.tertiary.get(remotePath).get(
Integer.parseInt(remotePId));
}
String localSAddress = null;
String remoteSAddress = null;
if (MyUtilityClass.isNotEmpty(mappings.secondary)) {
String localSId = mappings.secondary.get(localPath).get(compId);
localSAddress = mappings.tertiary.get(localPath).get(
Integer.parseInt(localSId));
String remoteSId = mappings.secondary.get(remotePath).get(compId);
remoteSAddress = mappings.tertiary.get(remotePath).get(
Integer.parseInt(remoteSId));
}
// now use - localPAddress, remotePAddress, localSAddress and remoteSAddress
}
}
通过对主要、次要和第三次映射进行上述健全性和空检查,应用程序的总体性能(第95个百分位数)为4毫秒。
但如果我这样做,没有对主要、次要和第三次映射进行任何健全性检查或空检查,我得到的总穿孔(第95百分位)为0.87ms。
class Task implements Callable<String> {
public Task() {
}
public String call() throws Exception {
int compId = 100;
String localPath = "hello";
String remotePath = "world";
Mappings mappings = ClientData.getMappings();
String localPId = mappings.primary.get(localPath).get(compId);
String localPAddress = mappings.tertiary.get(localPath).get(Integer.parseInt(localPId));
String remotePId = mappings.primary.get(remotePath).get(compId);
String remotePAddress = mappings.tertiary.get(remotePath).get(Integer.parseInt(remotePId));
String localSId = mappings.secondary.get(localPath).get(compId);
String localSAddress = mappings.tertiary.get(localPath).get(Integer.parseInt(localSId));
String remoteSId = mappings.secondary.get(remotePath).get(compId);
String remoteSAddress = mappings.tertiary.get(remotePath).get(Integer.parseInt(remoteSId));
// now use - localPAddress, remotePAddress, localSAddress and remoteSAddress
}
}
下面是我的isEmpty和isNotEmpty方法-
public static boolean isNotEmpty(Object obj) {
return !isEmpty(obj);
}
public static boolean isEmpty(Object obj) {
if (obj == null)
return true;
if (obj instanceof Collection)
return ((Collection<?>) obj).size() == 0;
final String s = String.valueOf(obj).trim();
return s.length() == 0 || s.equalsIgnoreCase("null");
}
看看您的代码达到这一点的频率。对于一些复杂的对象及其沉重的#toString()方法,这可能会很昂贵:
final String s = String.valueOf(obj).trim();
此外,它还会创建临时垃圾,在您的测试进行计数时可能会导致垃圾收集。
这段代码在复制大文件时运行得越来越慢。我做错什么了吗? 我的Java是: OpenJDK10.0.2 2018-07-17OpenJDK运行时环境(构建10.0.2 13-Ubuntu-1ubuntu0.18.04.4) OpenJDK 64位服务器VM(构建10.0.2 13-Ubuntu-1ubuntu0.18.04.4,混合模式)
问题内容: 如果html文件是本地文件(在我的C驱动器上),则可以使用,但是如果html文件在服务器上并且图像文件是本地文件,则无法使用。这是为什么? 任何可能的解决方法? 问题答案: 如果客户端可以请求本地文件系统文件,然后使用JavaScript找出其中的内容,则将是一个安全漏洞。 解决此问题的唯一方法是在浏览器中构建扩展。Firefox扩展和IE扩展可以访问本地资源。Chrome的限制更为严
fReceiveBuffer是否存在一个视频帧? 哦,这里是我的FFMPEG初始化代码,需要打开相关视频解码器:http://paste.ubuntu.com/12529760//
我正在使用Reverfit2,我试图从我用Python制作的Web服务中请求一些数据。 实际上,它抛出了一个“java.lang.IllegalStateException”,但是当我调用GET方法时,API返回代码200,而且我还可以看到用于调试响应JSON的打印版本。问题是Call方法,因为它总是在failure时执行。 求求你,救命! 这是我的(简单的)Python Web服务,API-Pr
我试图隐藏Actionbar并改用工具栏,但如果我更改Theme.appcompat.light.NoActionBar,它确实会隐藏,但应用程序在行setContentView(r.layout.activity_main)处崩溃; 尽管它确实使用getSupportActionBar()隐藏。hide();但我不能用这个代码 Toolbar Toolbar=(Toolbar)findViewB