Java 多线程变得更慢
Java multithreading becomes slower
我们有 运行 几个线程的代码。在线程的 运行 事件中,我们调用了 2 个 Web 服务。当达到迭代次数 2000 时,我们遇到了性能问题。每个 Web 服务调用的过程 运行 大约需要 600 毫秒,并且随着它的继续,它可以达到近 60 秒...
每个网络服务的实际执行保持一致但端口创建变慢:
long preStartTime = System.currentTimeMillis();
ServicePortType winPort = (ServicePortType) this.getConnector().getFactory().create();
long preEndTime = System.currentTimeMillis();
LOGGER.debug("@@@Web Service Prep work (1st service) took => " + (preEndTime - preStartTime) + "ms");
这将在大约 80 毫秒开始时记录,并且随着过程的继续 运行ning,在 2000 次迭代时它可以达到 50 秒:
(iteration 1) @@@Web Service Prep work (1st service) took => 80ms
(iteration 2000) @@@Web Service Prep work (1st service) took => 524421ms
这是连接器设置:
@Override
public void init(Properties prop) {
LOGGER.info("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
LOGGER.info("@@@@@@ Starting HTTPConnector @@@@@@@@@@@@@@");
LOGGER.info("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
try {
factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(ServicePortType.class);
LOGGER.debug("@@@URL : " + prop.getProperty("service.url"));
factory.setAddress(prop.getProperty("service.url"));
factory.getInInterceptors().add(new LoggingInInterceptor());
factory.getOutInterceptors().add(new LoggingOutInterceptor());
} catch (Exception ex) {
LOGGER.error(ex);
}
LOGGER.info("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
LOGGER.info("@@@@@@ End HTTPConnector @@@@@@@@@@@@@@@@@@@");
LOGGER.info("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
}
有人可以指导我吗?
编辑
我将每次调用的这部分更改为静态,并且只创建一次。现在性能不错,不知道对其他有没有影响。
来自这里:
ServicePortType winPort = (ServicePortType) this.getConnector().getFactory().create();
为此:
private static UVSInterfaceExtendPortType winPort;
if (winPort == null)
{
winPort = (UVSInterfaceExtendPortType) this.getConnector().getFactory().create();
}
我的解决方案是只实例化 portType 一次,而不是每次都实例化(如我编辑中所讨论的)。
我们每天进行大约 10 万笔交易,整个交易的速度保持在一秒以下,其中包括 2 个 Web 服务调用和几个数据库调用。
我们有 运行 几个线程的代码。在线程的 运行 事件中,我们调用了 2 个 Web 服务。当达到迭代次数 2000 时,我们遇到了性能问题。每个 Web 服务调用的过程 运行 大约需要 600 毫秒,并且随着它的继续,它可以达到近 60 秒...
每个网络服务的实际执行保持一致但端口创建变慢:
long preStartTime = System.currentTimeMillis();
ServicePortType winPort = (ServicePortType) this.getConnector().getFactory().create();
long preEndTime = System.currentTimeMillis();
LOGGER.debug("@@@Web Service Prep work (1st service) took => " + (preEndTime - preStartTime) + "ms");
这将在大约 80 毫秒开始时记录,并且随着过程的继续 运行ning,在 2000 次迭代时它可以达到 50 秒:
(iteration 1) @@@Web Service Prep work (1st service) took => 80ms
(iteration 2000) @@@Web Service Prep work (1st service) took => 524421ms
这是连接器设置:
@Override
public void init(Properties prop) {
LOGGER.info("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
LOGGER.info("@@@@@@ Starting HTTPConnector @@@@@@@@@@@@@@");
LOGGER.info("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
try {
factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(ServicePortType.class);
LOGGER.debug("@@@URL : " + prop.getProperty("service.url"));
factory.setAddress(prop.getProperty("service.url"));
factory.getInInterceptors().add(new LoggingInInterceptor());
factory.getOutInterceptors().add(new LoggingOutInterceptor());
} catch (Exception ex) {
LOGGER.error(ex);
}
LOGGER.info("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
LOGGER.info("@@@@@@ End HTTPConnector @@@@@@@@@@@@@@@@@@@");
LOGGER.info("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
}
有人可以指导我吗?
编辑
我将每次调用的这部分更改为静态,并且只创建一次。现在性能不错,不知道对其他有没有影响。
来自这里:
ServicePortType winPort = (ServicePortType) this.getConnector().getFactory().create();
为此:
private static UVSInterfaceExtendPortType winPort;
if (winPort == null)
{
winPort = (UVSInterfaceExtendPortType) this.getConnector().getFactory().create();
}
我的解决方案是只实例化 portType 一次,而不是每次都实例化(如我编辑中所讨论的)。
我们每天进行大约 10 万笔交易,整个交易的速度保持在一秒以下,其中包括 2 个 Web 服务调用和几个数据库调用。