时间:2021-05-19
CloseableHttpClient httpClient = HttpClients.custom() .setConnectionManager(connectionManager) .setMaxConnTotal(400) .setMaxConnPerRoute(150) .evictExpiredConnections() .build();
evictExpiredConnections 这个配置作用:
设置一个定时线程,定时清理闲置连接,可以将这个定时时间设置为 keep alive timeout 时间的一半以保证超时前回收
每个httpClient 对象都会有自己独立的定时线程
这样如果应用中httpClient对象很多,就会导致上图中线程太多
源码中,如果设置了evictExpiredConnections 会有下面一段逻辑
List<Closeable> closeablesCopy = closeables != null ? new ArrayList<Closeable>(closeables) : null; if (!this.connManagerShared) { if (closeablesCopy == null) { closeablesCopy = new ArrayList<Closeable>(1); } final HttpClientConnectionManager cm = connManagerCopy; if (evictExpiredConnections || evictIdleConnections) { final IdleConnectionEvictor connectionEvictor = new IdleConnectionEvictor(cm, maxIdleTime > 0 ? maxIdleTime : 10, maxIdleTimeUnit != null ? maxIdleTimeUnit : TimeUnit.SECONDS, maxIdleTime, maxIdleTimeUnit); closeablesCopy.add(new Closeable() { @Override public void close() throws IOException { connectionEvictor.shutdown(); try { connectionEvictor.awaitTermination(1L, TimeUnit.SECONDS); } catch (final InterruptedException interrupted) { Thread.currentThread().interrupt(); } } }); connectionEvictor.start(); } closeablesCopy.add(new Closeable() { @Override public void close() throws IOException { cm.shutdown(); } }); }IdleConnectionEvictor 对象是
public final class IdleConnectionEvictor { private final HttpClientConnectionManager connectionManager; private final ThreadFactory threadFactory; private final Thread thread; private final long sleepTimeMs; private final long maxIdleTimeMs; private volatile Exception exception; public IdleConnectionEvictor( final HttpClientConnectionManager connectionManager, final ThreadFactory threadFactory, final long sleepTime, final TimeUnit sleepTimeUnit, final long maxIdleTime, final TimeUnit maxIdleTimeUnit) { this.connectionManager = Args.notNull(connectionManager, "Connection manager"); this.threadFactory = threadFactory != null ? threadFactory : new DefaultThreadFactory(); this.sleepTimeMs = sleepTimeUnit != null ? sleepTimeUnit.toMillis(sleepTime) : sleepTime; this.maxIdleTimeMs = maxIdleTimeUnit != null ? maxIdleTimeUnit.toMillis(maxIdleTime) : maxIdleTime; this.thread = this.threadFactory.newThread(new Runnable() { @Override public void run() { try { while (!Thread.currentThread().isInterrupted()) { Thread.sleep(sleepTimeMs); connectionManager.closeExpiredConnections(); if (maxIdleTimeMs > 0) { connectionManager.closeIdleConnections(maxIdleTimeMs, TimeUnit.MILLISECONDS); } } } catch (final Exception ex) { exception = ex; } } }); }会出现一个线程,这个线程里面就是去关闭超时不用的闲置链接
到此这篇关于关于HttpClient 引发的线程太多导致FullGc的问题的文章就介绍到这了,更多相关HttpClient 引发的线程太多导致FullGc内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
Java多线程线程同步与死锁1.线程同步多线程引发的安全问题一个非常经典的案例,银行取钱的问题。假如你有一张银行卡,里面有5000块钱,然后你去银行取款2000
zarchiver解压文件出错的原因: 1、网络传输状况不好,如断线过多,开的线程过多,服务器人太多导致不能连接太多等。 2、站点提供的的RAR压缩包本来就
这篇文章主要介绍了如何解决线程太多导致socket连接池出现的问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参
线程通信用来保证线程协调运行,一般在做线程同步的时候才需要考虑线程通信的问题。1、传统的线程通信通常利用Objeclt类提供的三个方法:wait()导致当前线程
问题使用HTTPClient请求HTTPS的API时出现Thecertificatecannotbeverifieduptoatrustedcertificat