[英]Making SimpleDateFormat thread safe
我有很多线程处理Trade
对象,我使用RowMapper
将数据库列映射到Trade
对象。
我知道SimpleDateFormat
在任何 Java 中都不是线程安全的。 结果,我在startDate
得到了一些不可预测的结果。 例如,我在startDate
也看到了endDate
日期。
这是我的代码:
public class ExampleTradeMapper {
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd-MMM-yyyy");
public void map(Trade trade, ResultSet rs, int rowNum) throws SQLException {
trade.setStartDate(getFormattedDate(rs.getDate("START_DATE")));
trade.setEndDate(getFormattedDate(rs.getDate("END_DATE")));
trade.setDescription(rs.getString("DESCRIPTION"));
}
private String getFormattedDate(Date date) {
try {
if (date != null)
return DATE_FORMAT.format(date).toUpperCase();
else
return null;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
public class SomeRowMapper extends TradeMapper implements RowMapper<Trade> {
@Override
public Trade mapRow(ResultSet rs, int rowNum) throws SQLException {
Trade trade = new Trade();
map(trade, rs, rowNum);
return trade;
}
}
对于这个应用程序,我的核心池大小约为 20,最大约为 50。这些线程有时可以处理来自数据库的大约 100 条交易记录。
使此日期格式化线程安全的最佳方法是什么? 我应该使用FastDateFormat直接替换吗?
有没有更好的替代方法来使这个线程安全?
不要使用字符串,而是使用通过 JDBC 4.2 或更高版本与数据库交换的java.time对象(特别是LocalDate
)。
myResultSet.getObject( // Exchange modern java.time objects with your database.
"START_DATE" ,
LocalDate.class
) // Returns a `LocalDate` object.
.format( // Generate a `String` representing textually the content of this `LocalDate`.
DateTimeFormatter.ofPattern( "dd-MMM-uuuu" , Locale.US )
)
2018 年 1 月 23 日
作为不可变对象, java.time对象在设计上是线程安全的。 您可以缓存java.time对象以跨线程使用。
使 SimpleDateFormat 线程安全
别。
使用现代java.time类,这些类多年前取代了麻烦的旧日期时间类,例如SimpleDateFormat
、 java.util.Date
、 java.sql.Date
和Calendar
。
java.time类被设计为线程安全的。 他们使用不可变对象模式,根据原始值返回新对象,而不是“变异”(改变)原始对象。
我认为没有理由在您的示例代码中使用字符串:不在您的数据库访问代码中,不在您的业务对象 ( Trade
) 中。
从 JDBC 4.2 开始,我们可以与数据库交换java.time对象。 对于类似于 SQL 标准DATE
类型的数据库列,请使用类LocalDate
。 LocalDate
类表示没有时间和时区的仅日期值。
myPreparedStatement.setObject( … , myLocalDate ) ;
恢复。
LocalDate myLocalDate = myResultSet.getObject( … , LocalDate.class ) ;
您的Trade
类应该将成员变量startDate
和endDate
作为LocalDate
对象,而不是字符串。
public class Trade {
private LocalDate startDate ;
private LocalDate endDate ;
…
// Getters
public LocalDate getStartDate() {
return this.startDate ;
}
public LocalDate getEndDate() {
return this.endDate;
}
public Period getPeriod() { // Number of years-months-days elapsed.
return Period.between( this.startDate , this.endDate ) ;
}
// Setters
public void setStartDate( LocalDate startDateArg ) {
this.startDate = startDateArg ;
}
public void setEndDate( LocalDate endDateArg ) {
this.endDate = endDateArg ;
}
@Override
public toString() {
"Trade={ " + "startDate=" + this.startDate.toString() …
}
…
}
不需要字符串,不需要格式化模式。
要将日期时间值交换或存储为文本,请使用标准ISO 8601格式而不是您的问题中看到的自定义格式。
java.time类在解析/生成字符串时默认使用 ISO 8601 格式。 所以不需要指定格式模式。
LocalDate ld = LocalDate.parse( "2018-01-23" ) ; // January 23, 2018.
String s = ld.toString() ; // Outputs 2018-01-23.
为了在用户界面中呈现,让java.time自动本地化。 要本地化,请指定:
FormatStyle
确定字符串的长度或缩写。Locale
确定:
例子:
Locale l = Locale.CANADA_FRENCH ;
DateTimeFormatter f =
DateTimeFormatter.ofLocalizedDate( FormatStyle.FULL )
.withLocale( l ) ;
String output = ld.format( f ) ;
狂欢节 2018 年 1 月 23 日
DateTimeFormatter
类在设计上是线程安全的,作为不可变对象。 您可以持有一个跨线程使用的实例。
java.time框架内置于 Java 8 及更高版本中。 这些类取代麻烦的老传统日期时间类,如java.util.Date
, Calendar
,和SimpleDateFormat
。
现在处于维护模式的Joda-Time项目建议迁移到java.time类。
要了解更多信息,请参阅Oracle 教程。 并在 Stack Overflow 上搜索许多示例和解释。 规范是JSR 310 。
您可以直接与您的数据库交换java.time对象。 使用符合JDBC 4.2或更高版本的JDBC 驱动程序。 不需要字符串,不需要java.sql.*
类。
从哪里获得 java.time 类?
ThreeTen-Extra项目用额外的类扩展了 java.time。 该项目是未来可能添加到 java.time 的试验场。 您可以在这里找到一些有用的类,比如Interval
, YearWeek
, YearQuarter
,和更多。
您可以将其ThreadLocal
。 池中的每个线程都将拥有自己的格式化程序。
private static final ThreadLocal<SimpleDateFormat> DATE_FORMAT = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat("dd-MMM-yyyy");
}
};
在这里您可以看到以线程安全的方式使用日期格式的最快方法。 因为您有 3 种方法可以做到:
DateFormat.getDateInstance()
synchronized
完整代码示例:
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class SimpleDateFormatThreadExample {
private static String FORMAT = "dd-M-yyyy hh:mm:ss";
private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat(FORMAT);
public static void main(String[] args) {
final String dateStr = "02-1-2018 06:07:59";
ExecutorService executorService = Executors.newFixedThreadPool(10);
Runnable task = new Runnable() {
@Override
public void run() {
parseDate(dateStr);
}
};
Runnable taskInThread = new Runnable() {
@Override
public void run() {
try {
ConcurrentDateFormatAccess concurrentDateFormatAccess = new ConcurrentDateFormatAccess();
System.out.println("Successfully Parsed Date " + concurrentDateFormatAccess.convertStringToDate(dateStr));
// don't forget to use CLEAN because the classloader with keep date format !
concurrentDateFormatAccess.clean();
} catch (ParseException e) {
e.printStackTrace();
}
}
};
for (int i = 0; i < 100; i++) {
executorService.submit(task);
// remove this comment to use thread safe way !
// executorService.submit(taskInThread);
}
executorService.shutdown();
}
private static void parseDate(String dateStr) {
try {
Date date = simpleDateFormat.parse(dateStr);
System.out.println("Successfully Parsed Date " + date);
} catch (ParseException e) {
System.out.println("ParseError " + e.getMessage());
} catch (Exception e) {
e.printStackTrace();
}
}
public static class ConcurrentDateFormatAccess {
private ThreadLocal<DateFormat> df = new ThreadLocal<DateFormat>() {
@Override
public DateFormat get() {
return super.get();
}
@Override
protected DateFormat initialValue() {
return new SimpleDateFormat(FORMAT);
}
@Override
public void remove() {
super.remove();
}
@Override
public void set(DateFormat value) {
super.set(value);
}
};
public void clean() {
df.remove();
}
public Date convertStringToDate(String dateString) throws ParseException {
return df.get().parse(dateString);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.