1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.classic.issue.lbclassic36;
15
16 import org.junit.jupiter.api.Test;
17
18 import java.text.SimpleDateFormat;
19 import java.util.Date;
20
21
22
23
24
25 public class DateFormatOriginal_tzest {
26 public static final String ISO8601_PATTERN = "yyyy-MM-dd HH:mm:ss,SSS";
27 static final long NANOS_IN_ONE_SEC = 1000 * 1000 * 1000L;
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56 @Test
57 public void testSynchronized() throws Exception {
58 SynchronizedDateFormatter formatter = new SynchronizedDateFormatter();
59 int threads = 10;
60 int iterations = 10000;
61 Thread[] formatThreads = new Thread[threads];
62 Date date = new Date();
63
64 for (int i = 0; i < threads; i++) {
65 formatThreads[i] = new DateFormatThread(formatter, date, iterations);
66 }
67 long start = System.nanoTime();
68 for (Thread thread : formatThreads) {
69 thread.start();
70 }
71 for (Thread thread : formatThreads) {
72 thread.join();
73 }
74 long end = System.nanoTime();
75 double actual = ((double) (end - start)) / NANOS_IN_ONE_SEC;
76 System.out.printf("Synchronized DateFormat: %,.4f seconds\n", actual);
77
78 }
79 @Test
80 public void testUnSynchronized() throws Exception {
81 UnsynchronizedDateFormatter formatter = new UnsynchronizedDateFormatter();
82 int threads = 10;
83 int iterations = 10000;
84 Thread[] formatThreads = new Thread[threads];
85 Date date = new Date();
86
87 for (int i = 0; i < threads; i++) {
88 formatThreads[i] = new DateFormatThread(formatter, date, iterations);
89 }
90 long start = System.nanoTime();
91 for (Thread thread : formatThreads) {
92 thread.start();
93 }
94 for (Thread thread : formatThreads) {
95 thread.join();
96 }
97 long end = System.nanoTime();
98 double actual = ((double) (end - start)) / NANOS_IN_ONE_SEC;
99 System.out.printf("Unsynchronized DateFormat: %,.4f seconds\n", actual);
100
101 }
102 @Test
103 public void testThreadLocal() throws Exception {
104 ThreadLocalDateFormatter formatter = new ThreadLocalDateFormatter();
105 int threads = 10;
106 int iterations = 10000;
107 Thread[] formatThreads = new Thread[threads];
108 Date date = new Date();
109
110 for (int i = 0; i < threads; i++) {
111 formatThreads[i] = new DateFormatThread(formatter, date, iterations);
112 }
113 long start = System.nanoTime();
114 for (Thread thread : formatThreads) {
115 thread.start();
116 }
117 for (Thread thread : formatThreads) {
118 thread.join();
119 }
120 long end = System.nanoTime();
121 double actual = ((double) (end - start)) / NANOS_IN_ONE_SEC;
122 System.out.printf("ThreadLocal DateFormat: %,.4f seconds\n", actual);
123
124 }
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151 public interface Formatter {
152 String format(Date date);
153 }
154
155 public static class SynchronizedDateFormatter implements Formatter {
156 SimpleDateFormat simpleFormat = new SimpleDateFormat(ISO8601_PATTERN);
157
158 public synchronized String format(Date date) {
159 return simpleFormat.format(date);
160 }
161 }
162
163 public static class UnsynchronizedDateFormatter implements Formatter {
164 public synchronized String format(Date date) {
165 return new SimpleDateFormat(ISO8601_PATTERN).format(date);
166 }
167 }
168
169 public static class ThreadLocalDateFormatter implements Formatter {
170 ThreadLocal<SimpleDateFormat> formatter = new ThreadLocal<SimpleDateFormat>() {
171 protected synchronized SimpleDateFormat initialValue() {
172 return new SimpleDateFormat(ISO8601_PATTERN);
173 }
174 };
175
176 public String format(Date date) {
177 return formatter.get().format(date);
178 }
179 }
180
181
182
183
184
185
186
187
188
189 public static class DateFormatThread extends Thread {
190 Formatter formatter;
191 Date date;
192 long iterCount;
193
194 public DateFormatThread(Formatter f, Date date, long iterations) {
195 this.formatter = f;
196 this.date = date;
197 this.iterCount = iterations;
198 }
199
200 public void run() {
201 for (int i = 0; i < iterCount; i++) {
202 formatter.format(this.date);
203 }
204 }
205 }
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225 }