`
manecocomph
  • 浏览: 3466 次
  • 性别: Icon_minigender_1
  • 来自: 上海
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

2011-2-17 不安全的多线程

阅读更多
在我们的代码的里面经常有这样的例子,为了格式化日期(如:2011-02-17), 就需要一个SimpleDateFormat类,同时,为了让整个系统共享这个格式化类,我们把这个格式化类写到一个公共的Util类里面,代码如下:
Util 类:
package com.maneco.art;

import java.text.SimpleDateFormat;

public class Util {
	public static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	
}

Util类的使用者:
package com.maneco.art;

import java.text.ParseException;

public class MultiThread implements Runnable {

	public static void main(String[] args) {
		new Thread(new MultiThread()).start();
		new Thread(new MultiThread()).start();
		new Thread(new MultiThread()).start();
		new Thread(new MultiThread()).start();
	}

	@Override
	public void run() {
		try {
			for (int i = 0; i < 10000; i++) {
				System.out.print(Util.sdf.parse("2000-01-05"));
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}
}

如上面的代码,就会产生下面的异常:
Exception in thread "Thread-0" java.lang.NumberFormatException: For input string: ""
	at java.lang.NumberFormatException.forInputString(Unknown Source)
	at java.lang.Long.parseLong(Unknown Source)
	at java.lang.Long.parseLong(Unknown Source)
	at java.text.DigitList.getLong(Unknown Source)
	at java.text.DecimalFormat.parse(Unknown Source)
	at java.text.SimpleDateFormat.subParse(Unknown Source)
	at java.text.SimpleDateFormat.parse(Unknown Source)
	at java.text.DateFormat.parse(Unknown Source)
	at com.maneco.art.MultiThread.run(MultiThread.java:18)
	at java.lang.Thread.run(Unknown Source)

原因在于:
如果是单线程,使用这个Util类是没有问题的;
如果是多线程的话,就会产生并发访问同一个实例化的SimpleDateFormat类。
JDK API中声明DateFormat类和SimpleDateFormat类都不是线程同步的,推荐对于每个线程创建一个实例。如果多线程访问的话,自己实现多线程同步;
1: 最简单的方法是在每次使用的时候,new一个新的DateFormat;效率稍许损失,不能达到系统共享同一个格式化类。最简单的编程模型。推荐。
2: 增加线程同步访问: 但是多线程的效率将损失,并且容易导致死锁。
private static final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
	public static synchronized Date formatStr(String str) throws ParseException {
		return Util.df.parse(str);
	}

下面这种线程同步是没有意义的,因为仅仅拿到reference时是线程同步的,而不是使用时同步的。
private static final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
	public static synchronized DateFormat getDateFomrater() {
		return Util.df;
	}

3: 如果对性能要求严格,可以封装一个类,在同步的代码里面对于每个线程根据线程Id去返回对应的实例类,
   效率稍微损失,不会导致死锁,系统的格式化聚集在一个访问点:
private static final Map<Long, DateFormat> DateFormaterMap = new HashMap<Long, DateFormat>();
	// return date formater by thread Id
	public static DateFormat getDateFormaterByThread() {
		Long tId = Thread.currentThread().getId();
		
		if (Util.DateFormaterMap.containsKey(tId)) {
			return Util.DateFormaterMap.get(tId);
		} else {
			// another strategy: use SimpleDateFormat's clone method with a default one
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Util.DateFormaterMap.put(tId, sdf);
			return sdf;
		}
	}

建议在写单元测试的时候,增加一项多线程测试。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics