c*******e 发帖数: 290 | 1 如果要使用是个第三方的 library, 它的设计没有考虑 thread safety.在多线程程序
中,确保线程安全的最简单方法,是不是就是将要从 library 里调用的对象,建立在
一个 ThreadLocal 的变量里?这样就万事大吉了?不然,要很费劲,去研究那
library 内部的设计。
ThreadLocal 是不是用的很广泛?概念浅显,使用也最简单,方便。 |
c*******e 发帖数: 290 | 2 这个没有任何 thread safety 的保护,但好像是安全的。为什么?
public class ThreadLocalTest {
public static void main(String args[]) throws IOException {
Thread t1 = new Thread(new Task());
Thread t2 = new Thread( new Task());
t1.start();
t2.start();
}
public static String threadSafeFormat(Date date){
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
return formatter.format(date);
}
}
class Task implements Runnable{
@Override
public void run() {
for(int i=0; i<10; i++){
System.out.println("Thread: " + Thread.currentThread().getName()
+ " Formatted Date: " + ThreadLocalTest.threadSafeFormat(new Date()) );
}
}
}
输出:
Thread: Thread-0 Formatted Date: 16/11/2013
Thread: Thread-1 Formatted Date: 16/11/2013
Thread: Thread-0 Formatted Date: 16/11/2013
Thread: Thread-1 Formatted Date: 16/11/2013
Thread: Thread-0 Formatted Date: 16/11/2013
Thread: Thread-1 Formatted Date: 16/11/2013
Thread: Thread-0 Formatted Date: 16/11/2013
Thread: Thread-1 Formatted Date: 16/11/2013
Thread: Thread-0 Formatted Date: 16/11/2013
Thread: Thread-1 Formatted Date: 16/11/2013
Thread: Thread-0 Formatted Date: 16/11/2013
Thread: Thread-1 Formatted Date: 16/11/2013
Thread: Thread-1 Formatted Date: 16/11/2013
Thread: Thread-0 Formatted Date: 16/11/2013
Thread: Thread-1 Formatted Date: 16/11/2013
Thread: Thread-0 Formatted Date: 16/11/2013
Thread: Thread-1 Formatted Date: 16/11/2013
Thread: Thread-0 Formatted Date: 16/11/2013
Thread: Thread-1 Formatted Date: 16/11/2013
Thread: Thread-0 Formatted Date: 16/11/2013 |
g**e 发帖数: 6127 | 3 becuase new SimpleDateFormat object is created every time... Your test has
nothing to do with thread local. btw use DateTimeFormat from JodaTime, it's
so much beter, and thread safe.
()
【在 c*******e 的大作中提到】 : 这个没有任何 thread safety 的保护,但好像是安全的。为什么? : public class ThreadLocalTest { : public static void main(String args[]) throws IOException { : Thread t1 = new Thread(new Task()); : Thread t2 = new Thread( new Task()); : : t1.start(); : t2.start(); : : }
|
g**e 发帖数: 6127 | 4 你读的文章的下半部分
http://javarevisited.blogspot.com/2013/01/threadlocal-memory-le
【在 c*******e 的大作中提到】 : 这个没有任何 thread safety 的保护,但好像是安全的。为什么? : public class ThreadLocalTest { : public static void main(String args[]) throws IOException { : Thread t1 = new Thread(new Task()); : Thread t2 = new Thread( new Task()); : : t1.start(); : t2.start(); : : }
|
c*******e 发帖数: 290 | 5 对,因为那个 date 是个 local variable, 每次都重新创建。所以,他那个例子完全
不适合,根本就用不上 ThreadLocal, 他那个 PerThreadFormatter 完全是多余的. 贴
出他的完整例子:
/**
*
* @author
*/
public class ThreadLocalTest {
public static void main(String args[]) throws IOException {
Thread t1 = new Thread(new Task());
Thread t2 = new Thread( new Task());
t1.start();
t2.start();
}
/*
* Thread safe format method because every thread will use its own
DateFormat
*/
public static String threadSafeFormat(Date date){
DateFormat formatter = PerThreadFormatter.getDateFormatter();
return formatter.format(date);
}
}
/*
* Thread Safe implementation of SimpleDateFormat
* Each Thread will get its own instance of SimpleDateFormat which will not
be shared between other threads. *
*/
class PerThreadFormatter {
private static final ThreadLocal dateFormatHolder =
new ThreadLocal() {
/*
* initialValue() is called
*/
@Override
protected SimpleDateFormat initialValue() {
System.out.println("Creating SimpleDateFormat for Thread : " +
Thread.currentThread().getName());
return new SimpleDateFormat("dd/MM/yyyy");
}
};
/*
* Every time there is a call for DateFormat, ThreadLocal will return
calling
* Thread's copy of SimpleDateFormat
*/
public static DateFormat getDateFormatter() {
return dateFormatHolder.get();
}
}
class Task implements Runnable{
@Override
public void run() {
for(int i=0; i<100; i++){
System.out.println("Thread: " + Thread.currentThread().getName()
+ " Formatted Date: " + ThreadLocalTest.threadSafeFormat(new Date()) );
}
}
}
s
【在 g**e 的大作中提到】 : becuase new SimpleDateFormat object is created every time... Your test has : nothing to do with thread local. btw use DateTimeFormat from JodaTime, it's : so much beter, and thread safe. : : ()
|