我有一个 Java REST 应用程序,其中一个端点总是处理ConcurrentMap. 我正在做负载测试,当负载测试开始增加时真的很糟糕。
为了提高应用程序的效率,我可以实施哪些策略?
我应该使用 Jetty 线程,因为它是我正在使用的服务器吗?还是主要是代码?或两者?
成为瓶颈的方法如下。
基本上我需要从给定文件中读取一些行。我不能将它存储在数据库中,所以我想出了一个用 Map 进行处理的方法。但是,我知道对于大文件,不仅需要很长时间才能到达线路,而且我冒着 Map 在有很多条目时会消耗大量内存的风险......
dict是ConcurrentMap。
public String getLine(int lineNr) throws IllegalArgumentException {
if (lineNr > nrLines) {
throw new IllegalArgumentException();
}
if (dict.containsKey(lineNr)) {
return dict.get(lineNr);
}
synchronized (this) {
try (Stream<String> st = Files.lines(doc.toPath())
Optional<String> optionalLine = st.skip(lineNr - 1).findFirst();
if (optionalLine.isPresent()) {
dict.put(lineNr, optionalLine.get());
} else {
nrLines = nrLines > lineNr ? lineNr : nrLines;
throw new IllegalArgumentException();
}
} catch (IOException e) {
e.printStackTrace();
}
return cache.get(lineNr);
}
ibeautiful
绝地无双
相关分类