我在travis-ci的持续集成构建中遇到了一些奇怪的线程问题,并得到:
testThatDifferentArgumentsCanBeParsedConcurrently(se.softhouse.jargo.concurrency.ConcurrencyTest) Time elapsed: 0.479 sec <<< FAILURE!
org.junit.ComparisonFailure: expected:<... greeting phrase to [
]greet new connection...> but was:<... greeting phrase to []greet new connection...>:<... greeting phrase to []greet new connection...>
它似乎只对 openjdk 而不是 oraclejdk 出错。
这是我使用 BreakIterator 的代码:
/**
* Wraps lines where <a
* href="http://docs.oracle.com/javase/tutorial/i18n/text/line.html">appropriate</a> (as defined
* by {@code locale}).
*
* @param value the value to separate with {@link StringsUtil#NEWLINE new lines}
* @param startingIndex the index where each line starts, useful for a fixed-size table for
* instance
* @param maxLineLength how long each line are allowed to be
*/
public static StringBuilder wrap(CharSequence value, int startingIndex, int maxLineLength, Locale locale)
{
String textToSplit = value.toString();
StringBuilder result = new StringBuilder(textToSplit.length());
// TODO(jontejj): is this not thread safe?
BreakIterator boundary = BreakIterator.getLineInstance(locale);
boundary.setText(textToSplit);
int start = boundary.first();
int end = boundary.next();
int lineLength = startingIndex;
while(end != BreakIterator.DONE)
{
String word = textToSplit.substring(start, end);
lineLength = lineLength + word.length();
if(lineLength >= maxLineLength)
{
result.append(NEWLINE);
lineLength = startingIndex;
}
result.append(word);
start = end;
end = boundary.next();
}
return result;
}
似乎 java.text.BreakIterator 没有完成它的工作,并且将线拉长了比它应该有的更多字符。
我检查了 BreakIterator 和 RuleBasedBreakIterator 的 openjdk 版本,但没有发现任何明显的线程安全问题。RuleBasedBreakIterator 中有一些数组没有被深度克隆,但我看不到它们被修改了,所以应该是安全的。我找不到 oraclejdk 源代码。我是否错误地理解了它们之间不应该像以前那样大的差异?
我试图通过针对 BreakIterator 使用的特定测试来隔离问题,但到目前为止还没有取得成果。
很抱歉这种尴尬的问题,但我处于停顿状态,所以我想也许 stackoverflow 上的某个人可以提供帮助。