【问题标题】:How to get server status using multi-threads periodically如何定期使用多线程获取服务器状态
【发布时间】:2021-01-14 15:38:44
【问题描述】:

下面的代码工作正常,它连接到给定的服务器(主机、端口)并获取连接状态。

它的作用是:

  1. PollService 实现 Callable 接口并连接到服务器(主机、端口)然后返回状态。
  2. 由于这应该定期发生,它会在 while(true) 循环中无限迭代 Hashmap 条目。

问题:在服务器端,我看到它需要 2 或 3 秒才能到达线程,如果我使用 Runnable 和定期实现,它会在 1 秒内连接。看起来无限迭代 Hashmap 是一种缓慢的方法。

但是,我不能使用 Runnable,因为它不会返回我以后需要使用的连接状态。

下面是连接到服务器的ServiceMonitor类(客户端)。

package org.example;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;

public class ServicesMonitor {
  private ExecutorService scheduledExecutorService = null;
  private static Logger logger = Logger.getLogger(ServicesMonitor.class.getName());
  private final Map<ServiceType, List<ClientMonitorService>> clientMonitorServicesMap = new HashMap<>();

  public void registerInterest(ClientMonitorService clientMonitorService) {
    clientMonitorServicesMap.computeIfAbsent(clientMonitorService.getServiceToMonitor().getServiceType(), v -> new ArrayList<>()).add(clientMonitorService);
  }

  public Map<ServiceType, List<ClientMonitorService>> getClineMonitorService() {
    return clientMonitorServicesMap;
  }

  public void poll(){
    //Observable.interval(1, TimeUnit.SECONDS).st
  }
  public void pollServices() {
    scheduledExecutorService = Executors.newFixedThreadPool(clientMonitorServicesMap.size());
    try {

      while (true) {
        clientMonitorServicesMap.forEach((k, v) -> {
          Future<Boolean> val = scheduledExecutorService.submit(new PollService(k));
          try {
            boolean result = val.get();
            System.out.println("service " + k.getHost() + ":" + k.getPort() + "status is " + result);
            if (result) {
              List<ClientMonitorService> list = v.stream().filter(a -> LocalDateTime.now().getSecond() % a.getServiceToMonitor().getFreqSec() == 0)
                      .collect(Collectors.toList());
              list.stream().forEach(a -> System.out.println(a.getClientId()));
            }

          } catch (InterruptedException e) {
            e.printStackTrace();
          } catch (ExecutionException e) {
            e.printStackTrace();
          }

        });
      }
    } catch (Exception e) {
      logger.log(Level.SEVERE, e.getMessage());
    } finally {
      scheduledExecutorService.shutdown();
    }
  }
}
  • 如何通过减少连接服务器所需的时间来提高此代码的性能?
  • 如何改进这段代码?

【问题讨论】:

  • 您将列表迭代两次。尝试删除收集器并流式传输一次。
  • 测试是在没有使用响应的收集器的情况下完成的。

标签: java multithreading server client callable


【解决方案1】:

使用 get(1, TimeUnit.SECONDS); 之后我也开始看到服务器端的改进(到达线程不到 1 秒),因为我们在客户端等待的时间不超过 1 秒。

while (true) {
    clientMonitorServicesMap.forEach((k, v) -> {
      Future<Boolean> val = scheduledExecutorService.submit(new PollService(k));
      try {
        boolean result = val.get(1, TimeUnit.SECONDS);
        System.out.println("service " + k.getHost() + ":" + k.getPort() + "status is " + result);
        if (result) {
          List<ClientMonitorService> list = v.stream()
                  //.filter(a -> LocalDateTime.now().getSecond() % a.getServiceToMonitor().getFreqSec() == 0)
                  .collect(Collectors.toList());
          list.stream().forEach(a -> System.out.println(a.getClientId()));
        }

      } catch (InterruptedException e) {
       logger.log(Level.WARNING,"Interrupted -> " + k.getHost()+":"+k.getPort());
      } catch (ExecutionException e) {
        logger.log(Level.INFO,"ExecutionException exception -> "+ k.getHost()+":"+k.getPort());
      } catch (TimeoutException e) {
        logger.log(Level.INFO,"TimeoutException exception -> "+ k.getHost()+":"+k.getPort());
      }

    });
  }

【讨论】:

    猜你喜欢
    • 2014-06-18
    • 1970-01-01
    • 1970-01-01
    • 2020-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-19
    • 2017-01-21
    相关资源
    最近更新 更多