由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - ThreadLocal 的一个 use case
相关主题
新手问一个多线程的问题倒霉的Swing代码总是导致Exception, fatal error
问一个blocking IO的程序FrameWork of Thread application 1
新手求教 BufferedReader.readLine()ZT: 关于性爱的多线程问题研究(一)
请教:怎么把synchronize的method改成用thread 但thread safe呢?问个Thread 的问题,java certificate里的
How to make code thread-safe?应该怎么答?Can Java thread return a value?
DATE TIME In RDBMS能这么 create thread 吗?
输入中文导致的死 机implements runable 和 extends thread
eclipse 怪问题java的接口runnable
相关话题的讨论汇总
话题: thread话题: date话题: formatted
进入Java版参与讨论
1 (共1页)
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.
:
: ()

1 (共1页)
进入Java版参与讨论
相关主题
java的接口runnableHow to make code thread-safe?应该怎么答?
线程问题。DATE TIME In RDBMS
另一个入门问题。输入中文导致的死 机
请问关于用threadPoolExecutor实现threadpool的问题?eclipse 怪问题
新手问一个多线程的问题倒霉的Swing代码总是导致Exception, fatal error
问一个blocking IO的程序FrameWork of Thread application 1
新手求教 BufferedReader.readLine()ZT: 关于性爱的多线程问题研究(一)
请教:怎么把synchronize的method改成用thread 但thread safe呢?问个Thread 的问题,java certificate里的
相关话题的讨论汇总
话题: thread话题: date话题: formatted