Tomcat升级后并行流不设置Thread.contextClassLoader

Parallel stream doesn#39;t set Thread.contextClassLoader after tomcat upgrade(Tomcat升级后并行流不设置Thread.contextClassLoader)

本文介绍了Tomcat升级后并行流不设置Thread.contextClassLoader的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 tomcat 从 8.5.6 升级到 8.5.28 并行流后停止为线程提供 contextClassLoader:

After tomcat upgrade from 8.5.6 to 8.5.28 parallel stream stopped supplying Threads with contextClassLoader:

因为它 Warmer::run 无法在其中加载类.

Because of it Warmer::run can't load classes in it.

warmers.parallelStream().forEach(Warmer::run);

您知道 Tomcat 为新线程的 contextClassLoaders 提供了什么吗?

Do you have any ideas what Tomcat was supplying for contextClassLoaders for new Threads?

ParallelStream 在最新的 Tomcat 中使用 ForkJoinPool.

ParallelStream uses ForkJoinPool in newest Tomcat.

推荐答案

Common ForkJoin pool 存在问题,可能导致内存泄漏以及应用程序能够从其他上下文加载类和资源/应用程序(如果您的 tomcat 是多租户,则可能存在安全漏洞).请参阅此 Tomcat Bugzilla 报告.

Common ForkJoin pool is problematic and could be responsible for memory leaks and for applications being able to load classes and resources from other contexts/applications (potential security leak if your tomcat is multi tenant). See this Tomcat Bugzilla Report.

在 Tomcat 8.5.11 他们已对上述问题进行了修复通过引入 SafeForkJoinWorkerThreadFactory.java

In Tomcat 8.5.11 they had applied fix to the above issues by introducing SafeForkJoinWorkerThreadFactory.java

为了让您的代码正常工作,您可以执行以下操作,这会将显式 ForkJoin 及其 worker thread factory 提供给 Stream.parallel() 执行.

In order for your code to work, you can do the following, which will supply explicit ForkJoin and its worker thread factory to the Stream.parallel() execution.

ForkJoinPool forkJoinPool = new ForkJoinPool(NO_OF_WORKERS);
forkJoinPool.execute(() -> warmers.parallelStream().forEach(Warmer::run));

这篇关于Tomcat升级后并行流不设置Thread.contextClassLoader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Tomcat升级后并行流不设置Thread.contextClassLoader

基础教程推荐