gpt4 book ai didi

java - 测试 SimpleDateFormat 的多线程调用

转载 作者:行者123 更新时间:2023-12-01 17:22:37 26 4
gpt4 key购买 nike

我想测试使用 SimpleDateFormat 作为静态类变量的修复。我正在得到

以前我的代码是

public class Abc{
private static SimpleDateFormat dateformatter;

public static String method1(final Calendar calendar) {
String thePattern = "ddMMM";
dateformatter = new SimpleDateFormat(thePattern, Locale.US);
sPars = dateformatter.format(calendar.getTime());
//Something

}

public static String method2(final Calendar calendar) {
String thePattern = "ddMMyyyy";
dateformatter = new SimpleDateFormat(thePattern, Locale.US);
sPars = dateformatter.format(calendar.getTime());
//Something

}
}

对于这个,我遇到了异常

java.lang.ArrayIndexOutOfBoundsException: 965
at sun.util.calendar.BaseCalendar.getCalendarDateFromFixedDate(BaseCalendar.java:454)
at java.util.GregorianCalendar.computeFields(GregorianCalendar.java:2333)
at java.util.GregorianCalendar.computeFields(GregorianCalendar.java:2248)
at java.util.Calendar.setTimeInMillis(Calendar.java:1140)
at java.util.Calendar.setTime(Calendar.java:1106)
at java.text.SimpleDateFormat.format(SimpleDateFormat.java:955)
at java.text.SimpleDateFormat.format(SimpleDateFormat.java:948)
at java.text.DateFormat.format(DateFormat.java:336)

现在我已将其更改为:

public class Abc{  
public static String method1(final Calendar calendar) {
String thePattern = "ddMMM";
SimpleDateFormat dateformatter = new SimpleDateFormat(thePattern, Locale.US);
sPars = dateformatter.format(calendar.getTime());
//Something
}

public static String method2(final Calendar calendar) {
String thePattern = "ddMMyyyy";
SimpleDateFormat dateformatter = new SimpleDateFormat(thePattern, Locale.US);
sPars = dateformatter.format(calendar.getTime());
//Something
}
}

我如何测试第二个是否是正确的修复?JUnits 有没有办法测试多线程?

最佳答案

SimpleDateFormat 不是线程安全的,如 javadoc 中所述。 :

Date formats are not synchronized. It is recommended to create separate format instances for each thread. If multiple threads access a format concurrently, it must be synchronized externally.

但是在这里你甚至搞乱了引用,因为:

dateformatter = new SimpleDateFormat(thePattern, Locale.US);

由于 dateformatter 是静态的,因此它的内容在线程之间共享。

在本地创建一个新实例(就像您之后所做的那样)是一个好方法。本地实例不在线程之间共享,并且不会干扰多线程。

只是不要忘记删除private static SimpleDateFormat dateformatter;以避免混淆。

您还可以使用ThreadLocal如果您只想为每个线程创建 1 个实例,则使用变量:

private static final ThreadLocal<SimpleDateFormat> dateformatter1 =
new ThreadLocal<SimpleDateFormat>() {
@Override protected SimpleDateFormat initialValue() {
return new SimpleDateFormat("ddMMM");
}
};

private static final ThreadLocal<SimpleDateFormat> dateformatter2 =
new ThreadLocal<SimpleDateFormat>() {
@Override protected SimpleDateFormat initialValue() {
return new SimpleDateFormat("ddMMyyyy");
}
};

public static String method1(final Calendar calendar) {
SimpleDateFormat dateformatter = dateformatter1.get();
sPars = dateformatter.format(calendar.getTime());
//Something
}

public static String method2(final Calendar calendar) {
SimpleDateFormat dateformatter = dateformatter2.get();
sPars = dateformatter.format(calendar.getTime());
//Something
}

关于java - 测试 SimpleDateFormat 的多线程调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17444969/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com