Redian新闻
>
ThreadLocal 的一个 use case
avatar
ThreadLocal 的一个 use case# Java - 爪哇娇娃
b*n
1
填写I-130表的时候配偶的地址用中文还是用拼音?
avatar
s*t
2
父亲,我又回忆起往日的时光
回忆起那灰黑色的田野 回忆起水牛沉默的眼睛
回忆起新翻的泥土 回忆起
你烟头明灭不定的光
当我蜷缩在角落中 呻吟
当我的肋骨 在异国冷漠的目光下
一根一根暴露出
它们脆弱的本性
啊 父亲 我隔着辽阔的大洋和群山呼唤你
让我重新骑回你宽厚结实的肩上
让我们重新坐回岸边 父亲
让我们面对故乡的河流 遥看远方
多么安全 澄清
让我重新沉浸于你所保护的童年中
让我们无所顾忌地游戏
啊 父亲 我生命回溯的源头
夕阳流出柔情的地方
请重新为我灵魂找回支撑的图腾
让我活得像人 死的像神
让我重新坚守于
这命运经年累月的艰辛之中 让我安息于
众神永恒的阴影和光芒
avatar
s*a
3
有没有书友可以给一个非凡TXT电子书论坛 邀请码?
谢谢。
avatar
m*y
4
我有一个production db server, 就装了sql server 2005 Enterprise Edition (包括
BI) , 是没有internet access的,但是从firewall block list有很多这个db server
企图access internet的entries. 不知道那位朋友知道怎么把这个企图access
internet的程序找出来? 谢谢!!!!!!
avatar
c*e
5
如果要使用是个第三方的 library, 它的设计没有考虑 thread safety.在多线程程序
中,确保线程安全的最简单方法,是不是就是将要从 library 里调用的对象,建立在
一个 ThreadLocal 的变量里?这样就万事大吉了?不然,要很费劲,去研究那
library 内部的设计。
ThreadLocal 是不是用的很广泛?概念浅显,使用也最简单,方便。
avatar
S*t
6
avatar
a9
7
windows update?

server

【在 m******y 的大作中提到】
: 我有一个production db server, 就装了sql server 2005 Enterprise Edition (包括
: BI) , 是没有internet access的,但是从firewall block list有很多这个db server
: 企图access internet的entries. 不知道那位朋友知道怎么把这个企图access
: internet的程序找出来? 谢谢!!!!!!

avatar
c*e
8
这个没有任何 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
avatar
a*7
9
兄弟总算写一个不是那么哲的了,这是俺的泰普,稀饭~~

【在 s**t 的大作中提到】
: 父亲,我又回忆起往日的时光
: 回忆起那灰黑色的田野 回忆起水牛沉默的眼睛
: 回忆起新翻的泥土 回忆起
: 你烟头明灭不定的光
: 当我蜷缩在角落中 呻吟
: 当我的肋骨 在异国冷漠的目光下
: 一根一根暴露出
: 它们脆弱的本性
: 啊 父亲 我隔着辽阔的大洋和群山呼唤你
: 让我重新骑回你宽厚结实的肩上

avatar
g*e
10
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();
:
: }

avatar
s*t
11
谢谢你啊

【在 S*******t 的大作中提到】

avatar
g*e
12
你读的文章的下半部分
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();
:
: }

avatar
s*t
13
呵呵,没有体。乱写一气
avatar
c*e
14
对,因为那个 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.
:
: ()

avatar
S*t
15
自由体:)
avatar
m*g
16
re

【在 s**t 的大作中提到】
: 父亲,我又回忆起往日的时光
: 回忆起那灰黑色的田野 回忆起水牛沉默的眼睛
: 回忆起新翻的泥土 回忆起
: 你烟头明灭不定的光
: 当我蜷缩在角落中 呻吟
: 当我的肋骨 在异国冷漠的目光下
: 一根一根暴露出
: 它们脆弱的本性
: 啊 父亲 我隔着辽阔的大洋和群山呼唤你
: 让我重新骑回你宽厚结实的肩上

avatar
c*1
17
你的诗里目前最喜欢这首,呵呵,很棒!
avatar
s*t
18
谢谢你的称赞阿。

【在 a*********7 的大作中提到】
: 兄弟总算写一个不是那么哲的了,这是俺的泰普,稀饭~~
avatar
s*t
19
多谢多谢。呵呵

【在 c**********1 的大作中提到】
: 你的诗里目前最喜欢这首,呵呵,很棒!
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。