Skip to content

Commit 35c531a

Browse files
committed
ThreadLocal相关demo
1 parent 3ef38fc commit 35c531a

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.javacore.thread.threadlocal;
2+
3+
import java.util.Date;
4+
import java.util.concurrent.TimeUnit;
5+
6+
/**
7+
* 描述:看类里面说明
8+
* Created by bysocket on 16/3/8.
9+
*/
10+
public class SafeTask implements Runnable{
11+
/**
12+
* ThreadLocal对象不会被所有线程共享
13+
* --> 线程局部变量 <--
14+
*/
15+
private static ThreadLocal<Date> startDate = new ThreadLocal<Date>(){
16+
/**
17+
* 隐式实现初始化对象
18+
* @return
19+
*/
20+
@Override
21+
protected Date initialValue() {
22+
return new Date();
23+
}
24+
};
25+
26+
@Override
27+
public void run() {
28+
System.out.printf("Starting Thread:%s : %s\n",Thread.currentThread().getId(),startDate.get());
29+
try {
30+
TimeUnit.SECONDS.sleep((int) Math.rint(Math.random() * 10));
31+
} catch (InterruptedException e) {
32+
e.printStackTrace();
33+
}
34+
System.out.printf("Finish Thread:%s : %s\n",Thread.currentThread().getId(),startDate.get());
35+
}
36+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.javacore.thread.threadlocal;
2+
3+
import java.util.concurrent.TimeUnit;
4+
5+
/**
6+
* 描述:可以看到线程每个有不同的启动时间,但是结束时间也会不相同.
7+
* Created by bysocket on 16/3/8.
8+
*/
9+
public class SafeTest {
10+
public static void main(String[] args) {
11+
SafeTask task = new SafeTask();
12+
for (int i = 0 ; i < 10; i++) {
13+
Thread thread = new Thread(task);
14+
thread.start();
15+
16+
try {
17+
TimeUnit.SECONDS.sleep(2);
18+
} catch (InterruptedException e) {
19+
e.printStackTrace();
20+
}
21+
}
22+
}
23+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.javacore.thread.threadlocal;
2+
3+
import java.util.Date;
4+
import java.util.concurrent.TimeUnit;
5+
6+
/**
7+
* 描述:私有变量会被所有线程共享
8+
* Created by bysocket on 16/3/8.
9+
*/
10+
public class UnsafeTask implements Runnable{
11+
private Date startDate;
12+
13+
@Override
14+
public void run() {
15+
startDate = new Date();
16+
System.out.printf("Starting Thread:%s : %s\n",Thread.currentThread().getId(),startDate);
17+
try {
18+
TimeUnit.SECONDS.sleep((int) Math.rint(Math.random() * 10));
19+
} catch (InterruptedException e) {
20+
e.printStackTrace();
21+
}
22+
System.out.printf("Finish Thread:%s : %s\n",Thread.currentThread().getId(),startDate);
23+
}
24+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.javacore.thread.threadlocal;
2+
3+
import java.util.concurrent.TimeUnit;
4+
5+
/**
6+
* 描述:可以看到线程每个有不同的启动时间,但是结束时间会有相同.
7+
* Created by bysocket on 16/3/8.
8+
*/
9+
public class UnsafeTest {
10+
public static void main(String[] args) {
11+
UnsafeTask task = new UnsafeTask();
12+
for (int i = 0 ; i < 10; i++) {
13+
Thread thread = new Thread(task);
14+
thread.start();
15+
16+
try {
17+
TimeUnit.SECONDS.sleep(2);
18+
} catch (InterruptedException e) {
19+
e.printStackTrace();
20+
}
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)