Java-Struts Web 应用程序中的重构方法会导致争用吗?
我正在与一家公司合作,该公司使用 Struts 和 Java 来生成 Web 应用程序。最近为了整理系统,重构了很多代码。我一直在使用的一种技术是将方法的 finally 块中的流的结束语句移至实用程序类。该实用程序类是静态的,并且具有针对各种类型的流的关闭方法。通过进行此重构,每次需要关闭流时,我可以节省 5 行代码,这将某些类中的代码减少了 600 行,这非常有利。然而,我担心这样做,如果投入生产,可能会引起对这些方法的争用。
我之前只遇到过多线程编程中的争用概念,这就是为什么我不确定这是否会在这些情况下导致问题。
这可能吗?或者只是我对争用、静态方法、Web 应用程序等的误解?
提前致谢, 阿列克谢·布鲁。
I'm working with a company that produces a web application using struts with Java. I have been refactoring a lot of the code recently to tidy up the system. One technique I've been using is to move closing statements for streams, in finally blocks, of methods to a utility class. The utility class is static and has close methods for various types of streams. By doing this refactoring I save 5 lines of code every time a stream needs to be closed which has reduced code in some classes by 600 lines making it very favourable. However I'm worried by doing this that it may be possible to cause contention on those method if put into production.
I've only come across the concept of contention in multithreaded programming before which is why I'm not sure if this will cause problems in these instances.
Is this possible? or simply my misunderstanding of contention, static methods, web applications etc?
Thanks in advance,
Alexei Blue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最佳实践是关闭同一范围内的所有打开的文件处理程序。例如,当在方法中打开文件输出流时,也应该在该方法中关闭它。这也可以防止大多数并发问题(但并非总是如此)。但是,有许多例外情况不这样做,但在这种情况下跟踪和关闭所有文件处理程序会更加困难。
仅供参考,存在一种关闭方法,它完全按照您在 Apache Common IO 中描述的方式进行操作, IOUtils。这是一个流行的库,您可以考虑使用。
It is best practice to close all the open file handler within the same scope. For example, when a file outputstream is open in a method, then it should also be closed with in that method. This also prevent most of concurrency problem (but not always). However, there are many exception for not doing so but tracking and closing all file handler would be more difficult in this case.
FYI, there exists a closing method which does exactly what you describe in Apache Common IO, IOUtils. It is a popular library which you may consider using.
除非静态方法仅对其参数进行操作,并且您不会尝试多次同时关闭同一个流,否则应该没问题。
一般来说,方法可以同时执行。同步始终适用于对象,因此如果您在这些方法中使用的对象完全不同,则不应发生争用。
Unless the static methods operate solely on their parameters and you don't try to concurrently close the same stream multiple times you should be fine.
Methods in general can be executed simultaneously. Synchronzation will always work on objects and thus contention should not occur if the objects you use in those methods are completely distinct.
这是一种常见的做法,例如看看 Guava 中的 Closeables 类。顺便说一句,您应该只需要一种方法就能够关闭 Java 中的几乎所有内容:
由于所有 Streams 和 Readers 都实现 Closeable 接口,因此这一方法应该足以满足您的所有需求。
一般来说,这不会导致争用,除非您尝试从线程 A 中的流中读取数据,而线程 B 正在关闭该流。但如果是这样的话,无论是否进行重构,您都会遇到这种争论。仅此重构不会引入任何额外争用。
This is a common practice, for instance look at the Closeables class in Guava. By the way, you should only need one method to be able to close virtually anything in Java:
As all the Streams and Readers implement the Closeable interface, this one method should suffice for all your needs.
In general this will not cause contention, unless you try to read from the stream in say Thread-A, where Thread-B is closing the stream. But if that were the case, then you'd have this contention with or without this refactoring. Solely this refactoring will not introduce any additional contention.