记录器应该始终是最终的和静态的吗?
可以从多个线程访问类。在这种情况下记录器也必须是最终的和静态的吗? 谢谢。
Class can be accessed from many threads. Must be logger in this case also be final and static?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
所有主要的 java 日志记录包(
java.util.logging
、log4j
等)都是同步且线程安全的。即使从多个线程调用该类,每个类的private final static
记录器的标准模式也很好。All major java logging packages (
java.util.logging
,log4j
, etc.) are synchronized and thread safe. The standard pattern of aprivate final static
logger per class is fine even if the class is called from multiple threads.是的,记录器应该是静态的和最终的。也最好是私人的。每个类只需要一个记录器实例,除非您要动态更改日志首选项,否则最好将其最终确定。
Logger 是线程安全的,您不必担心线程。
Yes the logger should be static and final. Also preferably private. There needs be only one logger instance per class and also unless you are going to change the log preference dynamically, it is better to make it final.
Logger are thread safe and you do not have to worry about threading.
使记录器成为最终的和/或静态的不会以任何方式影响记录器使用的线程安全性。如果从多个线程使用记录器实例,请确保您使用的是线程安全的记录器。
一般来说,记录器应该是私有静态最终的,但不要假设这使其成为线程安全的。最常见的日志框架是线程安全的,因此如果您使用其中之一,您应该会很好。
Making the logger final and or static will not in any way affect the thread-safety of the use of the logger. If the logger instance is being used from multiple threads than ensure you are using a thread-safe logger.
In general the logger should be private static final but do not assume that this makes it thread-safe. Most common logging frameworks are thread-safe so if you are using one of these you should be good.