根据javadocs描述,DateFormat类是非线程安全的。通过多线程并发测试,也证实了这一点。
通过此次测试得出的一些经验:
- 经多线程并发UT验证,DateFormat的format一直都正确运行(基于StringBuffer实现),但parse经常出问题(未使用任何并发技术)
- 即使DateFormat的parse运行正常结束,最终结果也可能不对!
- DateFormatThreadLocal、DateTimeFormatterWrapper与FastDateFormatWrapper都正确运行
- 通过500并发量+5个线程的性能测试来看,对于format操作,DateTimeFormatterWrapper 最优,FastDateFormatWrapper 次之,DateFormatThreadLocal 最差;对于parse操作,三者差距微乎其微
/** * 可格式化的日期接口定义。 * * @author Bert Lee * @version 2014-8-16 */ public interface DateFormattable { /** * 默认日期格式 */ String PATTERN = "yyyy-MM-dd"; /** * Formats a Date into a date/time string. * * @param date * @return * @see java.text.DateFormat#format(Date) */ String format(Date date); /** * Parses text from the beginning of the given string to produce a date. * * @param source * @return * @throws ParseException * @see java.text.DateFormat#parse(String) */ Date parse(String source) throws ParseException; }
/** * {@link DateFormat} wrapper. * * @author Bert Lee * @version 2014-8-16 */ public class DateFormatWrapper implements DateFormattable { private final DateFormat format; public DateFormatWrapper() { this(PATTERN); } public DateFormatWrapper(String pattern) { this.format = new SimpleDateFormat(pattern); } /** * <font color="red">经多线程UT验证,format都正确运行(基于StringBuffer实现)!</font> */ @Override public String format(Date date) { return this.format.format(date); } /** * <font color="red">经多线程UT验证,parse经常出问题(未使用任何并发技术)!</font> */ @Override public Date parse(String source) throws ParseException { return this.format.parse(source); } }
/** * Date parse task. * * @author Bert Lee * @version 2014-8-16 */ public class DateParseCallable implements Callable<Date> { private DateFormattable formatter; private String source; public DateParseCallable(DateFormattable formatter, String source) { this.formatter = formatter; this.source = source; } @Override public Date call() throws Exception { return this.formatter.parse(source); } }
/** * Concurrent thread {@link java.util.concurrent.Executor Executor} wrapper. * * @author Bert Lee * @version 2014-8-16 */ public class ConcurrentThreadExecutorWrapper<T> { private ExecutorService executor; private Callable<T> task; private List<Future<T>> results; private TestScale runTimes; public ConcurrentThreadExecutorWrapper(Callable<T> task) { this(task, TestScale.BASIC); } public ConcurrentThreadExecutorWrapper(Callable<T> task, TestScale runTimes) { // pool with 5 threads this.executor = Executors.newFixedThreadPool(5); this.task = task; this.runTimes = runTimes; } public void run() { results = new ArrayList<Future<T>>(); // perform 10 times date conversions for (int i = 0; i < runTimes.getValue(); i++) { results.add(executor.submit(task)); } executor.shutdown(); } public void printResults() throws Exception { this.run(); // look at the results for (Future<T> result : results) { out.println(result.get()); } } }
/** * Test for {@link ConcurrentThreadExecutorWrapper}. * <p> * 参考并优化实现了<a href="http://stackoverflow.com/questions/4021151/java-dateformat-is-not-threadsafe-what-does-this-leads-to"> * “Java DateFormat is not thread-safe” what does this leads to?</a> * * @author Bert Lee * @version 2014-8-16 */ public class DateFormatExecutorTest { // 日期转换格式 private static final String pattern = "yyyy-MM-dd HH:mm"; /* * 经多线程UT验证,DateFormat的format一直都正确运行(基于StringBuffer实现),但parse经常出问题(未使用任何并发技术)! * 即使DateFormat的parse运行正常结束,最终结果也可能不对! * * DateFormatThreadLocal、DateTimeFormatterWrapper与FastDateFormatWrapper都正确运行。 */ @Test(dataProvider = "parse", groups = "parse") public void parse(DateFormattable formatter, String source) throws Exception { Callable<Date> task = new DateParseCallable(formatter, source); ConcurrentThreadExecutorWrapper<Date> executor = new ConcurrentThreadExecutorWrapper<Date>(task); out.println(formatter.getClass().getSimpleName() + " parse result:"); executor.printResults(); } @DataProvider(name = "parse") protected static final Object[][] parseTestData() { Object[][] testData = new Object[][] { // { new DateFormatWrapper(pattern), "2014-08-16 08:23:07" }, // 经常报错,即使运行结束,最终结果也可能不对! { new DateFormatThreadLocal(pattern), "2014-08-16 08:23:07" }, { new DateTimeFormatterWrapper(pattern), "2014-08-16 08:23" }, { new FastDateFormatWrapper(pattern), "2014-08-16 08:23:07" }, }; return testData; } @Test(dataProvider = "format", groups = "format") public void format(DateFormattable formatter) throws Exception { Date date = new Date(); Callable<String> task = new DateFormatCallable(formatter, date); ConcurrentThreadExecutorWrapper<String> executor = new ConcurrentThreadExecutorWrapper<String>(task); out.println(formatter.getClass().getSimpleName() + " format result:"); executor.printResults(); } @DataProvider(name = "format") protected static final Object[][] formatTestData() { Object[][] testData = new Object[][] { { new DateFormatWrapper(pattern) }, { new DateFormatThreadLocal(pattern) }, { new DateTimeFormatterWrapper(pattern) }, { new FastDateFormatWrapper(pattern) }, }; return testData; } }
相关推荐
java 使用DateFormat类获取系统的当前时间 java 使用DateFormat类获取系统的当前时间
Java中DateFormat类.pdf 学习资料 复习资料 教学资源
本Java精华内容深入Java API、嵌套类和内部类、与时间有关的类Date,DateFormat,Calendar、文件与流、Java变量类型间的相互转换、Java与Web、用连接池提高Servlet访问数据库的效率、Java扩展、应用服务器的集群策略及...
里面主要封装了 "日期转字符串","字符串转日期","获取当前日期的本周一是几号","获取当前日期周的最后一天","根据日期区间获取月份列表","根据日期获取年度中的周索引","根据年份获取年中周列表","获取某年的第几周...
调用该类中的方法,实现字符串于日期类型之间的转换,以及包括数据库中的日期类型,避免了代码的重复.
主要介绍了Java在并发环境中SimpleDateFormat多种解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
个人资源
java代码-使用java解决比较日期,Date类DateFormat类的源代码 ——学习参考资料:仅用于个人学习使用!
用于转换时间格式, ... DateFormat dateFormat = new DateFormat(); 二. String timeString=dateFormat.DateFormat("yyyy-MM-dd HH:m:s",time); 参数1:填写需要的格式 参数2:填写需要改变的时间
里面包含多个常用的java工具类,date和string的互换,java数据导出到excel,zip文件的解压缩、文件下载、md5加密、3des加密等各种常用java工具类,支持jdk1.4以上版本。有需要的,可以联系我,本人热衷于工具类的...
1.1.4 与时间有关的类Date,DateFormat,Calendar 1.2 深入理解嵌套类和内部类 1.2.1 什么是嵌套类及内部类? 1.2.2 静态嵌套类 1.2.3 在外部类中定义内部类 1.2.4 在方法中定义内部类 1.2.5 匿名内部类 1.2.6 内部类...
java获取当前时间:用java.util.Date类来实现,并结合java.text.DateFormat类来实现时间的格式化,利用System.currentTimeMillis()也可以获取
主要介绍了Java多线程编程中使用DateFormat类,文中介绍了三种相关的并发使用方法,需要的朋友可以参考下
用java.util.Date类来实现,并结合java.text.DateFormat类来实现时间的格式化
DateFormatManager dateformat = new DateFormatManager( new char[] { DateString.CENTURY, DateString.MONTH, DateString.DAY, DateString.YEAR, DateString.WEEK, DateString.HOUR_OF_DAY, DateString.MINUTE,...
程序是有关DateFormat类的使用,用于两种日期格式的转换“yyyy-mm-dd”——“MMMM dd,yyyy”
能够使用Date, Calendar, Locale, DateFormat,NumberFormat类创建、改变和显示日期、数字和货币对象。 理解正则表达式,学会使用String.matches方法,Pattern和Matcher类进行模式匹配。 学会...
Date Format with "-"
import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import javax.swing.filechooser.FileFilter; 实验后的结果验证 总结体会 通过此次实验,我...