java - 为什么tomcat最新代码在main方法中使用同步
问题描述
查看Tomcat的最新代码,在main方法中找到,它在初始化引导实例时使用了同步。
synchronized (daemonLock) {
if (daemon == null) {
// Don't set daemon until init() has completed
Bootstrap bootstrap = new Bootstrap();
try {
bootstrap.init();
} catch (Throwable t) {
handleThrowable(t);
t.printStackTrace();
return;
}
daemon = bootstrap;
} else {
// When running as a service the call to stop will be on a new
// thread so make sure the correct class loader is used to
// prevent a range of class not found exceptions.
Thread.currentThread().setContextClassLoader(daemon.catalinaLoader);
}
}
解决方案
TL;DR:Mark Thomas 减少了来自静态代码分析工具 SpotBugs 的警告消息。
正如您在 Tomcat SVN 日志中看到的那样,同步是在修订版 1826336 中引入的:https ://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/startup/Bootstrap.java?r1=1826335&r2 =1826336&
用 评论Fix some more SpotBugs warnings
。
当在 Bootstrap.java 的修订版 1826335 上运行 SpotBugs 并激活所有模块和最大灵敏度/详细度时,我可能已经复制了他已修复的警告:
Bug: Incorrect lazy initialization of static field Bootstrap.daemon in Bootstrap.main(String[])
This method contains an unsynchronized lazy initialization of a non-volatile static field. Because the compiler or processor may reorder instructions, threads are not guaranteed to see a completely initialized object, if the method can be called by multiple threads. You can make the field volatile to correct the problem. For more information, see the Java Memory Model web site.
Rank: Of Concern (17), confidence: Low
Pattern: LI_LAZY_INIT_STATIC
Type: LI, Category: MT_CORRECTNESS (Multithreaded correctness)
推荐阅读
- snakemake - Snakemake 如何设计空输出文件的下游规则?
- python - 我如何在熊猫数据框中找到英语以外的其他语言
- python - 如何使用 BeautifulSoup 在 HTML 中提取折扣价
- javascript - 茉莉花:如何订购规格运行
- python - 查看或缩放参数
- laravel - 雄辩的关系选择返回空数组
- c - 使用 UPX 解压 C 打包脚本后没有输出
- python - python RuntimeError:字典在迭代期间改变大小 - 在 scipy 导入期间随机弹出
- r - 使用 For 循环、dplyr 和 R 闪亮的 Excel 文件下载器
- docker - docker 容器用户/组:你真的可以在 heroku 中创建和使用它们吗?