Skip to content

Commit 135c918

Browse files
author
chenweijie
committed
添加对volatile的理解例子
1 parent b4975cc commit 135c918

4 files changed

Lines changed: 150 additions & 0 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.chen.api.util.thread.volatiledemo;
2+
3+
/**
4+
* volatile无法保证原子性
5+
*
6+
* @author : chen weijie
7+
* @Date: 2018-10-24 9:02 PM
8+
*/
9+
public class VolatileDemo {
10+
11+
12+
public volatile int inc = 0;
13+
14+
public void increase() {
15+
inc++;
16+
}
17+
18+
public static void main(String[] args) {
19+
final VolatileDemo test = new VolatileDemo();
20+
for (int i = 0; i < 10; i++) {
21+
new Thread(() -> {
22+
for (int j = 0; j < 1000; j++) {
23+
test.increase();
24+
}
25+
26+
}).start();
27+
}
28+
//保证前面的线程都执行完
29+
while (Thread.activeCount() > 1) {
30+
Thread.yield();
31+
}
32+
33+
System.out.println(test.inc);
34+
}
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.chen.api.util.thread.volatiledemo;
2+
3+
/**
4+
* synchronized 锁保证原子性
5+
*
6+
* @author : chen weijie
7+
* @Date: 2018-10-24 9:07 PM
8+
*/
9+
public class VolatileDemo1 {
10+
11+
12+
public volatile int inc = 0;
13+
14+
public synchronized void increase() {
15+
inc++;
16+
}
17+
18+
public static void main(String[] args) {
19+
final VolatileDemo1 test = new VolatileDemo1();
20+
for (int i = 0; i < 10; i++) {
21+
new Thread(() -> {
22+
for (int j = 0; j < 1000; j++) {
23+
test.increase();
24+
}
25+
26+
}).start();
27+
}
28+
//保证前面的线程都执行完
29+
while (Thread.activeCount() > 1) {
30+
Thread.yield();
31+
}
32+
33+
System.out.println(test.inc);
34+
}
35+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.chen.api.util.thread.volatiledemo;
2+
3+
import java.util.concurrent.locks.Lock;
4+
import java.util.concurrent.locks.ReentrantLock;
5+
6+
/**
7+
* lock 锁保证原子性
8+
* @author : chen weijie
9+
* @Date: 2018-10-24 9:07 PM
10+
*/
11+
public class VolatileDemo2 {
12+
13+
14+
public volatile int inc = 0;
15+
16+
Lock lock = new ReentrantLock();
17+
18+
19+
public void increase() {
20+
try {
21+
lock.lock();
22+
inc++;
23+
} finally {
24+
lock.unlock();
25+
}
26+
}
27+
28+
public static void main(String[] args) {
29+
final VolatileDemo2 test = new VolatileDemo2();
30+
for (int i = 0; i < 10; i++) {
31+
new Thread(() -> {
32+
for (int j = 0; j < 1000; j++) {
33+
test.increase();
34+
}
35+
36+
}).start();
37+
}
38+
//保证前面的线程都执行完
39+
while (Thread.activeCount() > 1) {
40+
Thread.yield();
41+
}
42+
43+
System.out.println(test.inc);
44+
}
45+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.chen.api.util.thread.volatiledemo;
2+
3+
import java.util.concurrent.atomic.AtomicInteger;
4+
5+
/**
6+
* AtomicInteger 锁保证原子性
7+
* @author : chen weijie
8+
* @Date: 2018-10-24 9:07 PM
9+
*/
10+
public class VolatileDemo3 {
11+
12+
public AtomicInteger inc = new AtomicInteger(0);
13+
14+
public void increase() {
15+
inc.incrementAndGet();
16+
}
17+
18+
public static void main(String[] args) {
19+
final VolatileDemo3 test = new VolatileDemo3();
20+
for (int i = 0; i < 10; i++) {
21+
new Thread(() -> {
22+
for (int j = 0; j < 1000; j++) {
23+
test.increase();
24+
}
25+
26+
}).start();
27+
}
28+
//保证前面的线程都执行完
29+
while (Thread.activeCount() > 1) {
30+
Thread.yield();
31+
}
32+
33+
System.out.println(test.inc);
34+
}
35+
}

0 commit comments

Comments
 (0)