java “静态”模式不应该总是静态的吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4935216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Shouldn't "static" patterns always be static?
提问by Gugussee
I just found a bug in some code I didn't write and I'm a bit surprised:
我刚刚在一些我没有写的代码中发现了一个错误,我有点惊讶:
Pattern pattern = Pattern.compile("\d{1,2}.\d{1,2}.\d{4}");
Matcher matcher = pattern.matcher(s);
Despite the fact that this code fails badly on input data we get (because it tries to find dates in the 17.01.2011 format and gets back things like 10396/2011 and then crashed because it can't parse the date but that reallyain't the point of this question ; )I wonder:
尽管此代码在我们获得的输入数据上严重失败(因为它试图以 17.01.2011 格式查找日期并返回诸如 10396/2011 之类的内容,然后由于无法解析日期而崩溃,但这确实是这个问题的重点;)我想知道:
isn't one of the point of Pattern.compileto be a speed optimization (by pre-compiling regexps)?
shouldn't all "static" pattern be alwayscompiled into static pattern?
是不是Pattern.compile的重点之一是速度优化(通过预编译正则表达式)?
应该不是所有的“静态”的图案总是编译成静态模式?
There are so many examples, all around the web, where the same pattern is always recompiled using Pattern.compilethat I begin to wonder if I'm seeing things or not.
网络上有很多例子,总是使用Pattern.compile重新编译相同的模式,我开始怀疑我是否看到了东西。
Isn't (assuming that the string is static and hence not dynamically constructed):
不是(假设字符串是静态的,因此不是动态构造的):
static Pattern pattern = Pattern.compile("\d{1,2}.\d{1,2}.\d{4}");
always preferrable over a non-static pattern reference?
总是比非静态模式参考更可取?
回答by biziclop
- Yes, the whole point of pre-compiling a
Pattern
is to only do it once. - It really depends on how you're going to use it, but in general, pre-compiled patterns stored in
static
fields should be fine. (UnlikeMatcher
s, which aren't threadsafe and therefore shouldn't really be stored in fields at all, static or not.)
- 是的,预编译 a 的全部意义
Pattern
在于只执行一次。 - 这实际上取决于您将如何使用它,但一般来说,存储在
static
字段中的预编译模式应该没问题。(与Matcher
s不同,s 不是线程安全的,因此根本不应该真正存储在字段中,无论是否静态。)
The only caveat with compiling patterns in static initializers is that if the pattern doesn't compile and the static initializer throws an exception, the source of the error can be quite annoying to track down. It's a minor maintainability problem but it might be worth mentioning.
在静态初始值设定项中编译模式的唯一警告是,如果模式没有编译并且静态初始值设定项抛出异常,那么跟踪错误的来源可能会很烦人。这是一个小的可维护性问题,但可能值得一提。
回答by AlexR
first, the bug in pattern is because dot (.) matches everything. If you want to match dot (.) you have to escape it in regex:
首先,模式中的错误是因为点 (.) 匹配所有内容。如果你想匹配点 (.) 你必须在正则表达式中转义它:
Pattern pattern = Pattern.compile("\\d{1,2}\\.\\d{1,2}\\.\\d{4}");
Pattern pattern = Pattern.compile("\\d{1,2}\\.\\d{1,2}\\.\\d{4}");
Second, Pattern.compile()
is a heavy method. It is always recommended to initialize static pattern (I mean patterns that are not being changed or not generated on the fly) only once. One of the popular ways to achieve this is to put the Pattern.compile()
into static initializer.
二Pattern.compile()
是重法。始终建议只初始化一次静态模式(我的意思是不会更改或不会即时生成的模式)。实现此目的的流行方法之一是将 inPattern.compile()
放入静态初始化程序。
You can use other approach. For example using singleton pattern or using framework that creates singleton objects (like Spring).
您可以使用其他方法。例如使用单例模式或使用创建单例对象的框架(如 Spring)。
回答by Kaleb Brasee
Yes, compiling the Pattern on each use is wasteful, and defining it statically would result in better performance. See this SO threadfor a similar discussion.
是的,在每次使用时编译 Pattern 是一种浪费,静态定义它会导致更好的性能。有关类似的讨论,请参阅此 SO 线程。
回答by dogbane
Static Patterns would remain in memory as long as the class is loaded.
只要类被加载,静态模式就会保留在内存中。
If you are worried about memory and want a throw-away Pattern
that you use once in a while and that can get garbage collected when you are finished with it, then you can use a non-static Pattern
.
如果您担心内存并希望Pattern
一次性使用一次并且在完成后可以收集垃圾,那么您可以使用非静态Pattern
.
回答by Jochen Bedersdorfer
It is a classical time vs. memory trade-off. If you are compiling a Pattern only once, don't stick it in a static field. If you measured that compiling Patterns is slow, pre-compile it and put it in a static field.
这是一个经典的时间与内存的权衡。如果你只编译一个 Pattern,不要把它放在一个静态字段中。如果您发现编译 Patterns 很慢,请预编译它并将其放在静态字段中。