【问题标题】:Java multithreading becomes slowerJava多线程变得更慢
【发布时间】:2015-08-29 15:10:00
【问题描述】:

我们有运行几个线程的代码。在线程的运行事件中,我们调用了 2 个 Web 服务。当达到迭代次数 2000 时,我们遇到了性能问题。该过程以每个 Web 服务调用大约 600 毫秒的速度运行,并且随着它的继续,它可能会达到近 60 秒...

每个 Web 服务的实际执行保持一致,但端口创建变慢:

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 毫秒左右开始记录,随着进程继续运行,在 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();
}

【问题讨论】:

  • 能把代码贴在Factory类的create()中吗?
  • @sunrise:它是 JaxWsProxyFactoryBean.java(编译代码)的一部分。我现在正在测试的只是在构造函数中初始化端口一次。似乎工作得更好。不过仍在监视它。
  • 你应该知道,官方的 JAX-WS 代理类 are not threadsafe。通过您使用 JaxWsProxyFactoryBean,我将假设您正在运行 CXF,在这种情况下,答案仍然是 sometimes。根据您的设置,您找到的“解决方案”可能会导致您在其他地方出现问题。在生产系统中,更多的线程实际上并不意味着更好的性能,它实际上可能意味着更少的性能。您拥有的线程越多,...
  • ...更多上下文切换你的处理器将不得不做(除了多线程带来的所有其他开销)。

标签: java multithreading jax-ws


【解决方案1】:

对我来说,解决方案是只实例化一次 portType 而不是每次都实例化(正如我在编辑中所讨论的那样)。

我们每天进行大约 10 万笔交易,整个交易的速度保持在 1 秒以下,其中包括 2 个 Web 服务调用和几个数据库调用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-13
    • 2016-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-27
    • 1970-01-01
    相关资源
    最近更新 更多