Skip to content

Commit 104bc5d

Browse files
committed
ThreadLocal
1 parent fc8e8a0 commit 104bc5d

File tree

2 files changed

+158
-0
lines changed

2 files changed

+158
-0
lines changed

src/chapter4/ThreadLocalDemo.java

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package chapter4;
2+
3+
import java.util.Random;
4+
import java.util.concurrent.*;
5+
6+
/**
7+
* Created by 13 on 2017/5/6.
8+
*/
9+
public class ThreadLocalDemo {
10+
public static final int GE_COUNT = 10000000;
11+
public static final int THREAD_COUT = 4;
12+
13+
static ExecutorService executorService = Executors.newFixedThreadPool(THREAD_COUT);
14+
public static Random random = new Random(123);
15+
16+
public static ThreadLocal<Random> randomThreadLocal = new ThreadLocal<Random>() {
17+
protected Random initialValue() {
18+
return new Random(123);
19+
}
20+
};
21+
22+
23+
public static class RandomTask implements Callable<Long> {
24+
25+
private int mode = 0;
26+
27+
public RandomTask(int mode) {
28+
this.mode = mode;
29+
}
30+
31+
public Random getRandom() {
32+
if (mode == 0) {
33+
return random;
34+
} else if (mode == 1) {
35+
return randomThreadLocal.get();
36+
} else {
37+
return null;
38+
}
39+
}
40+
41+
42+
@Override
43+
public Long call() throws Exception {
44+
long b = System.currentTimeMillis();
45+
46+
for (int i = 0; i < GE_COUNT; i++) {
47+
getRandom().nextInt();
48+
}
49+
long e = System.currentTimeMillis();
50+
System.out.println(Thread.currentThread().getName() + " spend" + (e - b) + "ms");
51+
return e - b;
52+
}
53+
}
54+
55+
public static void main(String args[]) throws ExecutionException, InterruptedException {
56+
Future<Long>[] futures = new Future[THREAD_COUT];
57+
for (int i = 0; i < THREAD_COUT; i++) {
58+
futures[i] = executorService.submit(new RandomTask(0));
59+
}
60+
61+
long totalTime = 0;
62+
63+
for (int i = 0; i < THREAD_COUT; i++) {
64+
totalTime += futures[i].get();
65+
}
66+
System.out.println("多线程访问同一个Random实例:" + totalTime + "ms");
67+
68+
//ThreadLocal的情况
69+
for (int i = 0; i < THREAD_COUT; i++) {
70+
futures[i] = executorService.submit(new RandomTask(1));
71+
}
72+
totalTime = 0;
73+
for (int i = 0; i < THREAD_COUT; i++) {
74+
totalTime += futures[i].get();
75+
}
76+
System.out.println("使用ThreadLocal包装Random实例:" + totalTime + "ms");
77+
78+
}
79+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package chapter4;
2+
3+
import java.text.ParseException;
4+
import java.text.SimpleDateFormat;
5+
import java.util.Date;
6+
import java.util.concurrent.CountDownLatch;
7+
import java.util.concurrent.ExecutorService;
8+
import java.util.concurrent.Executors;
9+
10+
/**
11+
* Created by 13 on 2017/5/6.
12+
*/
13+
public class ThreadLocalDemo_GC {
14+
static volatile ThreadLocal<SimpleDateFormat> threadLocal = new ThreadLocal<SimpleDateFormat>() {
15+
protected void finalize() throws Throwable {
16+
System.out.println(this.toString() + " is gc");
17+
}
18+
};
19+
20+
static volatile CountDownLatch countDownLatch = new CountDownLatch(100);
21+
22+
public static class ParseDate implements Runnable {
23+
int i = 0;
24+
25+
public ParseDate(int i) {
26+
this.i = i;
27+
}
28+
29+
@Override
30+
public void run() {
31+
32+
try {
33+
if (threadLocal.get() == null) {
34+
threadLocal.set(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") {
35+
protected void finalize() throws Throwable {
36+
System.out.println(this.toString() + " is gc");
37+
}
38+
});
39+
System.out.println(Thread.currentThread().getId() + " create SimpleDatFormat");
40+
}
41+
Date date = threadLocal.get().parse("2017-05-06 12:33:" + i % 60);
42+
System.out.println(i + ":" + date);
43+
} catch (ParseException e) {
44+
e.printStackTrace();
45+
} finally {
46+
countDownLatch.countDown();
47+
}
48+
49+
}
50+
}
51+
52+
53+
public static void main(String args[]) throws InterruptedException {
54+
ExecutorService executorService = Executors.newFixedThreadPool(10);
55+
for (int i = 0; i < 100; i++) {
56+
executorService.execute(new ParseDate(i));
57+
}
58+
countDownLatch.await();
59+
60+
System.out.println("mission complete!");
61+
62+
threadLocal = null;
63+
System.gc();
64+
System.out.println("first GC complete!!");
65+
66+
threadLocal = new ThreadLocal<SimpleDateFormat>();
67+
68+
countDownLatch = new CountDownLatch(100);
69+
70+
for (int i = 0; i < 100; i++) {
71+
executorService.execute(new ParseDate(i));
72+
}
73+
74+
countDownLatch.await();
75+
Thread.sleep(1000);
76+
System.gc();
77+
System.out.println("second GC complete!");
78+
}
79+
}

0 commit comments

Comments
 (0)