Skip to content

Commit ee28fe3

Browse files
committed
终止线程
1 parent 96523bb commit ee28fe3

11 files changed

Lines changed: 639 additions & 0 deletions

File tree

pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,13 @@
88
<artifactId>thinkingInJava</artifactId>
99
<version>1.0-SNAPSHOT</version>
1010

11+
<dependencies>
12+
<!-- https://mvnrepository.com/artifact/cglib/cglib -->
13+
<dependency>
14+
<groupId>cglib</groupId>
15+
<artifactId>cglib</artifactId>
16+
<version>3.3.0</version>
17+
</dependency>
1118

19+
</dependencies>
1220
</project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package concurrency;
2+
3+
import java.util.concurrent.atomic.AtomicInteger;
4+
5+
/**
6+
* @author :chenxin
7+
* @date :Created in 2020/3/16 20:39
8+
* @modified By:
9+
* @version: $
10+
*/
11+
public class AtomicEvenGenerator extends IntGenerator {
12+
private AtomicInteger currtEvenValue = new AtomicInteger(0);
13+
14+
@Override
15+
public int next() {
16+
return currtEvenValue.addAndGet(2);
17+
}
18+
19+
public static void main(String[] args) {
20+
EvenChecker.test(new AtomicEvenGenerator());
21+
}
22+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package concurrency;
2+
3+
import java.util.Timer;
4+
import java.util.TimerTask;
5+
import java.util.concurrent.ExecutorService;
6+
import java.util.concurrent.Executors;
7+
import java.util.concurrent.atomic.AtomicInteger;
8+
9+
/**
10+
* 原子类测试
11+
*
12+
* @author :chenxin
13+
* @date :Created in 2020/3/16 20:28
14+
* @modified By:
15+
* @version: $
16+
*/
17+
public class AtomocIntegerTest implements Runnable {
18+
19+
private AtomicInteger i = new AtomicInteger(0);
20+
21+
public int getValue() {
22+
return i.get();
23+
}
24+
25+
private void evenIncrement() {
26+
i.addAndGet(2);
27+
}
28+
29+
@Override
30+
public void run() {
31+
while (true) {
32+
evenIncrement();
33+
}
34+
}
35+
36+
public static void main(String[] args) {
37+
new Timer().schedule(new TimerTask() {
38+
@Override
39+
public void run() {
40+
System.out.println("Aborting");
41+
System.exit(0);
42+
}
43+
}, 5000);
44+
final ExecutorService executorService = Executors.newCachedThreadPool();
45+
final AtomocIntegerTest atomocIntegerTest = new AtomocIntegerTest();
46+
executorService.execute(atomocIntegerTest);
47+
while (true) {
48+
int val = atomocIntegerTest.getValue();
49+
if (val % 2 != 0) {
50+
System.out.println(val);
51+
System.exit(0);
52+
}
53+
}
54+
}
55+
}
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
package concurrency;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
import java.util.concurrent.ExecutorService;
7+
import java.util.concurrent.Executors;
8+
import java.util.concurrent.TimeUnit;
9+
import java.util.concurrent.atomic.AtomicInteger;
10+
import java.util.concurrent.locks.Lock;
11+
import java.util.concurrent.locks.ReentrantLock;
12+
13+
/**
14+
* 临界区(同步代码块)学习
15+
*
16+
* @author :chenxin
17+
* @date :Created in 2020/3/16 20:44
18+
* @modified By:
19+
* @version: $
20+
*/
21+
public class CriticalSection {
22+
static void testApproaches(PairManager pman1, PairManager pman2) {
23+
ExecutorService executorService = Executors.newCachedThreadPool();
24+
PairManipulator pm1 = new PairManipulator(pman1);
25+
PairManipulator pm2 = new PairManipulator(pman2);
26+
PairChecker pcheck1 = new PairChecker(pman1);
27+
PairChecker pcheck2 = new PairChecker(pman2);
28+
executorService.execute(pm1);
29+
executorService.execute(pm2);
30+
executorService.execute(pcheck1);
31+
executorService.execute(pcheck2);
32+
try {
33+
TimeUnit.MILLISECONDS.sleep(500);
34+
} catch (InterruptedException e) {
35+
System.out.println("Sleep interrupted");
36+
}
37+
System.out.println("pm1: " + pm1 + "\npm2: " + pm2);
38+
System.exit(0);
39+
}
40+
41+
public static void main(String[] args) {
42+
PairManager1 pman1 = new PairManager1();
43+
PairManager2 pman2 = new PairManager2();
44+
testApproaches(pman1, pman2);
45+
}
46+
}
47+
48+
class Pair {
49+
private int x, y;
50+
51+
public Pair(int x, int y) {
52+
this.x = x;
53+
this.y = y;
54+
}
55+
56+
public Pair() {
57+
this(0, 0);
58+
}
59+
60+
public int getX() {
61+
return x;
62+
}
63+
64+
public int getY() {
65+
return y;
66+
}
67+
68+
public void incrementX() {
69+
x++;
70+
}
71+
72+
public void incrementY() {
73+
y++;
74+
}
75+
76+
@Override
77+
public String toString() {
78+
return "x: " + x + ", y:" + y;
79+
}
80+
81+
public class PairValuesNotEqualException extends RuntimeException {
82+
public PairValuesNotEqualException() {
83+
super("Pair values not equal: " + Pair.this);
84+
}
85+
}
86+
87+
public void checkState() {
88+
if (x != y) {
89+
throw new PairValuesNotEqualException();
90+
}
91+
}
92+
}
93+
94+
abstract class PairManager {
95+
AtomicInteger checkCounter = new AtomicInteger(0);
96+
protected Pair p = new Pair();
97+
private List<Pair> storage = Collections.synchronizedList(new ArrayList<Pair>());
98+
99+
public synchronized Pair getPair() {
100+
return new Pair(p.getX(), p.getY());
101+
}
102+
103+
protected void store(Pair p) {
104+
storage.add(p);
105+
try {
106+
TimeUnit.MILLISECONDS.sleep(50);
107+
} catch (InterruptedException e) {
108+
}
109+
110+
}
111+
112+
public abstract void increment();
113+
}
114+
115+
class PairManager1 extends PairManager {
116+
117+
@Override
118+
public synchronized void increment() {
119+
p.incrementX();
120+
p.incrementY();
121+
store(getPair());
122+
}
123+
}
124+
125+
class PairManager3 extends PairManager {
126+
127+
Lock lock = new ReentrantLock();
128+
129+
@Override
130+
public void increment() {
131+
Pair temp;
132+
lock.lock();
133+
try {
134+
p.incrementX();
135+
p.incrementY();
136+
temp = getPair();
137+
} finally {
138+
lock.unlock();
139+
}
140+
store(temp);
141+
}
142+
}
143+
144+
class PairManager2 extends PairManager {
145+
146+
@Override
147+
public void increment() {
148+
Pair temp;
149+
synchronized (this) {
150+
p.incrementX();
151+
p.incrementY();
152+
temp = getPair();
153+
}
154+
store(temp);
155+
}
156+
}
157+
158+
class PairManipulator implements Runnable {
159+
private PairManager pm;
160+
161+
public PairManipulator(PairManager pm) {
162+
this.pm = pm;
163+
}
164+
165+
public void run() {
166+
while (true) {
167+
pm.increment();
168+
}
169+
}
170+
171+
@Override
172+
public String toString() {
173+
return "Pair: " + pm.getPair() + " checkCounter = " + pm.checkCounter.get();
174+
}
175+
}
176+
177+
class PairChecker implements Runnable {
178+
private PairManager pm;
179+
180+
public PairChecker(PairManager pm) {
181+
this.pm = pm;
182+
}
183+
184+
public void run() {
185+
while (true) {
186+
pm.checkCounter.incrementAndGet();
187+
pm.getPair().checkState();
188+
}
189+
}
190+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package concurrency;
2+
3+
/**
4+
* @author :chenxin
5+
* @date :Created in 2020/3/16 22:17
6+
* @modified By:
7+
* @version: $
8+
*/
9+
public class SyncObject {
10+
public static void main(String[] args) {
11+
final DualSynch dualSynch = new DualSynch();
12+
new Thread() {
13+
14+
public void run() {
15+
dualSynch.f();
16+
}
17+
}.start();
18+
dualSynch.g();
19+
}
20+
}
21+
22+
class DualSynch {
23+
private Object syncObject = new Object();
24+
25+
public synchronized void f() {
26+
for (int i = 0; i < 5; i++) {
27+
System.out.println("f()");
28+
Thread.yield();
29+
}
30+
}
31+
32+
public void g() {
33+
synchronized (syncObject) {
34+
for (int i = 0; i < 5; i++) {
35+
System.out.println("g()");
36+
Thread.yield();
37+
}
38+
}
39+
}
40+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package concurrency;
2+
3+
import java.util.Random;
4+
import java.util.concurrent.ExecutorService;
5+
import java.util.concurrent.Executors;
6+
import java.util.concurrent.TimeUnit;
7+
8+
/**
9+
* 线程本地存储
10+
*
11+
* @author :chenxin
12+
* @date :Created in 2020/3/18 21:13
13+
* @modified By:
14+
* @version: $
15+
*/
16+
public class ThreadLocalVariableHolder {
17+
18+
private static ThreadLocal<Integer> value = new ThreadLocal<Integer>() {
19+
private Random random = new Random(47);
20+
21+
protected synchronized Integer initialValue() {
22+
return random.nextInt(10000);
23+
}
24+
};
25+
26+
public static void increment() {
27+
value.set(value.get() + 1);
28+
}
29+
30+
public static int get() {
31+
return value.get();
32+
}
33+
34+
public static void main(String[] args) throws InterruptedException {
35+
final ExecutorService executorService = Executors.newCachedThreadPool();
36+
for (int i = 0; i < 5; i++) {
37+
executorService.execute(new Accessor(i));
38+
}
39+
TimeUnit.MILLISECONDS.sleep(3);
40+
executorService.shutdown();
41+
}
42+
}
43+
44+
class Accessor implements Runnable {
45+
private final int id;
46+
47+
public Accessor(int id) {
48+
this.id = id;
49+
}
50+
51+
@Override
52+
public void run() {
53+
while (!Thread.currentThread().isInterrupted()) {
54+
ThreadLocalVariableHolder.increment();
55+
System.out.println(this);
56+
Thread.yield();
57+
}
58+
}
59+
60+
@Override
61+
public String toString() {
62+
return "#" + id + ": " + ThreadLocalVariableHolder.get();
63+
}
64+
}

0 commit comments

Comments
 (0)