-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSynchronizedSample.java
324 lines (270 loc) · 8.12 KB
/
SynchronizedSample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package com.catherine.singleton;
/**
* synchronized 示范
*
* @author Catherine
*
*/
public class SynchronizedSample {
private static int p1;
private static int p2;
private static int p3;
private static int p4;
private static int p5;
private static class SynchronizedSampleHolder {
private static SynchronizedSample syncInstance = new SynchronizedSample();
}
public static SynchronizedSample getInstance() {
return SynchronizedSampleHolder.syncInstance;
}
/**
* 没有synchronized,静态变量的值就会同时被两个线程给修改,因为在{@link #increaseNoSync}
* 里的逻辑是先读值在重新赋值, 当下读到的值可能已经被另一个线程给修改。
*/
public void testStaticParamsAsUsual() {
System.out.println("Run an ordinary method with a static integer in 2 threads.");
try {
Thread t1 = new Thread(r1);
t1.start();
Thread t2 = new Thread(r1);
t2.start();
// Wait for threads to die.
t1.join();
t2.join();
analyze(p1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* 用synchronized修饰{@link #increase},线程锁的对象就是{@link #increase}
* ,由于一个对象只有一把锁,只要被一个线程获取锁,其它线程就不能获取该对象的其他synchronized实例方法。
*/
public void testStaticParams() {
System.out.println("Run a synchronized method with a static integer in 2 threads.");
try {
Thread t1 = new Thread(r2);
t1.start();
Thread t2 = new Thread(r2);
t2.start();
// Wait for threads to die.
t1.join();
t2.join();
analyze(p2);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* 同{@link #testStaticParams},逻辑都一样,但是如果同样的方法({@link #increaseP2}和
* {@link #increaseP3}完全一样)是来自不同的对象,该方法的锁也会不一样(因为一个对象只有一把锁)。
*/
public void testStaticParamsFromDifferentObj() {
System.out.println("Run a synchronized method with a static integer in 2 threads from 2 objects.");
try {
SynchronizedSample ss1 = new SynchronizedSample();
SynchronizedSample ss2 = new SynchronizedSample();
Thread t1 = new Thread(ss1.r3);
t1.start();
Thread t2 = new Thread(ss2.r3);
t2.start();
// Wait for threads to die.
t1.join();
t2.join();
analyze(p3);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void testStaticMethod() {
testStaticMethod1();
testStaticMethod2();
}
public void testStaticMethodAndSyncCodes() {
testStaticMethod3();
testStaticMethod4();
}
/**
* 同{@link #testStaticParams},逻辑都一样,用静态方法 {@link #staticIncrease}
* ,即使来自不同的对象,该方法的锁也会一样。 <br>
* <br>
* 用此方法保证在整个应用里,同时只会有一个线程执行该方法。
*/
public void testStaticMethod1() {
System.out.println("Run a static synchronized method with a static integer in 2 threads from 2 objects.");
try {
SynchronizedSample ss1 = new SynchronizedSample();
SynchronizedSample ss2 = new SynchronizedSample();
Thread t1 = new Thread(ss1.r4);
t1.start();
Thread t2 = new Thread(ss2.r4);
t2.start();
// Wait for threads to die.
t1.join();
t2.join();
analyze(p4);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* 模拟对数据库的两种操作,先呼叫{@link #testStaticMethod1}
* 进行累计,呼叫此方法进行累乘,预期结果是能先执行完累计再执行累乘,但是同步锁只有针对指定的那一个方法,所以两个方法来自不同锁并不会排程。 <br>
* <br>
* 呼叫不同的静态方法同时进行数据的修改,同步锁无法保证一次只有(一个线程)执行一个方法。
*/
public void testStaticMethod2() {
System.out.println("Run 2 static synchronized method with a static integer in 2 threads from 2 objects.");
try {
SynchronizedSample ss1 = new SynchronizedSample();
SynchronizedSample ss2 = new SynchronizedSample();
Thread t1 = new Thread(ss1.r5);
t1.start();
Thread t2 = new Thread(ss2.r5);
t2.start();
// Wait for threads to die.
t1.join();
t2.join();
analyze(p4);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* 同{@link #testStaticMethod1},逻辑都一样,但是在做synchronized时是同步代码块而不是整个方法, 好处有两点:
* <br>
* 1. 只需要同步synchronized指定的代码块,提升效能;<br>
* 2. synchronized的锁自行指定,也就是只要指定同一把锁,就可以实现“呼叫不同的静态方法同时进行数据的修改,保证一次只有(一个线程)
* 执行一个方法。”,让来自不同对象的方法能在多线程中排队实现。
*/
public void testStaticMethod3() {
System.out.println(
"Run a static method with a static integer which is synchronized a part of codes in 2 threads from 2 objects.");
try {
SynchronizedSample ss1 = new SynchronizedSample();
SynchronizedSample ss2 = new SynchronizedSample();
Thread t1 = new Thread(ss1.r6);
t1.start();
Thread t2 = new Thread(ss2.r6);
t2.start();
// Wait for threads to die.
t1.join();
t2.join();
analyze(p5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* 同{@link #testStaticMethod2},逻辑都一样,但是在做synchronized时是同步代码块而不是整个方法, 好处有两点:
* <br>
* 1. 只需要同步synchronized指定的代码块,提升效能;<br>
* 2. synchronized的锁自行指定,也就是只要指定同一把锁,就可以实现“呼叫不同的静态方法同时进行数据的修改,保证一次只有(一个线程)
* 执行一个方法。”,让来自不同对象的方法能在多线程中排队实现。
*/
public void testStaticMethod4() {
System.out.println(
"Run 2 static method with a static integer which are synchronized a part of codes in 2 threads from 2 objects.");
try {
SynchronizedSample ss1 = new SynchronizedSample();
SynchronizedSample ss2 = new SynchronizedSample();
Thread t1 = new Thread(ss1.r7);
t1.start();
Thread t2 = new Thread(ss2.r7);
t2.start();
// Wait for threads to die.
t1.join();
t2.join();
analyze(p5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void increaseNoSync() {
for (int i = 0; i < 1000000; i++) {
p1++;
}
}
private synchronized void increaseP2() {
for (int i = 0; i < 1000000; i++) {
p2++;
}
}
private synchronized void increaseP3() {
for (int i = 0; i < 1000000; i++) {
p3++;
}
}
private static synchronized void staticIncrease() {
for (int i = 0; i < 1000000; i++) {
p4++;
}
}
private static synchronized void staticMutiply() {
for (int i = 0; i < 10; i++) {
p4 *= 2;
}
}
private static void staticIncreaseAndSyncCodes() {
synchronized (SynchronizedSample.getInstance()) {
for (int i = 0; i < 1000000; i++) {
p5++;
}
}
}
private static void staticMutiplyAndSyncCodes() {
synchronized (SynchronizedSample.getInstance()) {
for (int i = 0; i < 5; i++) {
p5 *= 2;
}
}
}
protected Runnable r1 = new Runnable() {
@Override
public void run() {
increaseNoSync();
}
};
protected Runnable r2 = new Runnable() {
@Override
public void run() {
increaseP2();
}
};
protected Runnable r3 = new Runnable() {
@Override
public void run() {
increaseP3();
}
};
protected Runnable r4 = new Runnable() {
@Override
public void run() {
staticIncrease();
}
};
protected Runnable r5 = new Runnable() {
@Override
public void run() {
staticMutiply();
}
};
protected Runnable r6 = new Runnable() {
@Override
public void run() {
staticIncreaseAndSyncCodes();
}
};
protected Runnable r7 = new Runnable() {
@Override
public void run() {
staticMutiplyAndSyncCodes();
}
};
private void analyze(int num) {
if (num % 2000000 != 0)
System.out.println(String.format("--> Result=%d, failed to synchronize.", num));
else
System.out.println(String.format("--> Result=%d, synchronized successfully.", num));
}
}