Repository navigation
Expand file tree
/
Copy pathUnsafeTest.java
More file actions
180 lines (149 loc) · 4.26 KB
/
Copy pathUnsafeTest.java
File metadata and controls
180 lines (149 loc) · 4.26 KB
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
package com.concurrent.juc.unsafe;
import sun.misc.Unsafe;
import java.lang.reflect.Field;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* 测试几种不同的同步方式的性能以及Unsafe的简单使用
*/
public class UnsafeTest {
/**
* Stupid
* 9156989
* Counter Increment spend Time :171ms
* <p>
* Synchronized
* 100000000
* Counter Increment spend Time :2522ms
* <p>
* Lock
* 100000000
* Counter Increment spend Time :3214ms
* <p>
* Atomic
* 100000000
* Counter Increment spend Time :2045ms
* <p>
* CustomUnsafe
* 100000000
* Counter Increment spend Time :15788ms
*/
public static void main(String[] args) throws InterruptedException, NoSuchFieldException {
Counter counter = new CustomUnsafeCounter();
final ExecutorService service = Executors.newFixedThreadPool(1000);
long start = System.currentTimeMillis();
for (int i = 0; i < 1000; i++) {
service.submit(new CounterRunnable(counter, 100000));
}
service.shutdown();
service.awaitTermination(1, TimeUnit.HOURS);
long end = System.currentTimeMillis();
System.out.println(counter.counter());
System.out.println("Counter Increment spend Time :" + (end - start) + "ms");
}
interface Counter {
void increment();
long counter();
}
static class SyncCounter implements Counter {
private long count = 0;
@Override
public synchronized void increment() {
count++;
}
@Override
public long counter() {
return count;
}
}
static class CustomUnsafeCounter implements Counter {
private volatile long count = 0;
private Unsafe unsafe;
private long offset;
CustomUnsafeCounter() throws NoSuchFieldException {
unsafe = getUnsafe();
offset = unsafe.objectFieldOffset(CustomUnsafeCounter.class.getDeclaredField("count"));
}
@Override
public void increment() {
long cur = count;
while (!unsafe.compareAndSwapLong(this, offset, cur, cur + 1))
cur = count;
}
@Override
public long counter() {
return count;
}
}
static class AtomicCounter implements Counter {
private volatile AtomicLong count = new AtomicLong();
@Override
public void increment() {
count.incrementAndGet();
}
@Override
public long counter() {
return count.get();
}
}
static class StupidCounter implements Counter {
private long count = 0;
@Override
public void increment() {
count++;
}
@Override
public long counter() {
return count;
}
}
static class LockCounter implements Counter {
private long count = 0;
public final Lock lock = new ReentrantLock();
@Override
public void increment() {
try {
lock.lock();
count++;
} finally {
lock.unlock();
}
}
@Override
public long counter() {
return count;
}
}
static class CounterRunnable implements Runnable {
private final Counter counter;
private final int num;
CounterRunnable(Counter counter, int num) {
this.counter = counter;
this.num = num;
}
@Override
public void run() {
for (int i = 0; i < num; i++) {
counter.increment();
}
}
}
/**
* 使用反射获取Unsafe对象
*
* @return
*/
public static Unsafe getUnsafe() {
try {
Field f = Unsafe.class.getDeclaredField("theUnsafe");
f.setAccessible(true);
return ((Unsafe) f.get(null));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}