Skip to content

Commit a2f98d7

Browse files
author
chenweijie
committed
添加锁相关的操作
1 parent e55d8b4 commit a2f98d7

File tree

41 files changed

+1460
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1460
-0
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package com.chen.api.util.thread.study.chapter4.conditionExecute;
2+
3+
import com.chen.api.util.thread.study.chapter2.throwExceptionNoLock.ThreadB;
4+
5+
import java.util.concurrent.locks.Condition;
6+
import java.util.concurrent.locks.ReentrantLock;
7+
8+
/**
9+
* 使用condition实现顺序执行
10+
*
11+
* @author : chen weijie
12+
* @Date: 2018-05-21 01:11
13+
*/
14+
public class Run {
15+
volatile private static int nextPrintWho = 1;
16+
private static ReentrantLock lock = new ReentrantLock();
17+
final private static Condition conditionA = lock.newCondition();
18+
final private static Condition conditionB = lock.newCondition();
19+
final private static Condition conditionC = lock.newCondition();
20+
21+
22+
public static void main(String[] args) {
23+
24+
Thread threadA = new Thread() {
25+
26+
@Override
27+
public void run() {
28+
29+
try {
30+
lock.lock();
31+
while (nextPrintWho != 1) {
32+
conditionA.await();
33+
}
34+
35+
for (int i = 0; i < 3; i++) {
36+
System.out.println("threadA:" + (i + 1));
37+
}
38+
39+
nextPrintWho = 2;
40+
conditionB.signalAll();
41+
42+
43+
} catch (InterruptedException e) {
44+
e.printStackTrace();
45+
} finally {
46+
lock.unlock();
47+
}
48+
}
49+
};
50+
51+
Thread threadB = new Thread() {
52+
53+
54+
@Override
55+
public void run() {
56+
lock.lock();
57+
58+
try {
59+
while (nextPrintWho != 2) {
60+
conditionB.await();
61+
}
62+
63+
for (int i = 0; i < 3; i++) {
64+
System.out.println("threadB:" + (i + 1));
65+
}
66+
nextPrintWho = 3;
67+
conditionC.signalAll();
68+
} catch (InterruptedException e) {
69+
e.printStackTrace();
70+
} finally {
71+
lock.unlock();
72+
}
73+
}
74+
};
75+
76+
Thread threadC = new Thread() {
77+
78+
@Override
79+
public void run() {
80+
81+
try {
82+
lock.lock();
83+
while (nextPrintWho != 3) {
84+
conditionC.await();
85+
}
86+
for (int i = 0; i < 3; i++) {
87+
System.out.println("threadC:" + (i + 1));
88+
}
89+
90+
nextPrintWho = 1;
91+
conditionA.signalAll();
92+
93+
} catch (InterruptedException e) {
94+
e.printStackTrace();
95+
} finally {
96+
lock.unlock();
97+
}
98+
}
99+
100+
};
101+
102+
103+
Thread[] aArray = new Thread[5];
104+
Thread[] bArray = new Thread[5];
105+
Thread[] cArray = new Thread[5];
106+
107+
for (int i = 0; i < 5; i++) {
108+
aArray[i] = new Thread(threadA);
109+
bArray[i] = new Thread(threadB);
110+
cArray[i] = new Thread(threadC);
111+
aArray[i].start();
112+
bArray[i].start();
113+
cArray[i].start();
114+
}
115+
116+
117+
}
118+
119+
120+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.chen.api.util.thread.study.chapter4.conditionTest;
2+
3+
import java.util.concurrent.locks.Condition;
4+
import java.util.concurrent.locks.ReentrantLock;
5+
6+
/**
7+
* @author : chen weijie
8+
* @Date: 2018-05-19 01:59
9+
*/
10+
public class MyService {
11+
12+
private ReentrantLock lock = new ReentrantLock();
13+
14+
private Condition condition = lock.newCondition();
15+
16+
private boolean hashValue = false;
17+
18+
public void set() {
19+
try {
20+
lock.lock();
21+
while (hashValue) {
22+
condition.await();
23+
}
24+
System.out.println("打印AAAA");
25+
hashValue = true;
26+
condition.signal();
27+
28+
} catch (InterruptedException e) {
29+
e.printStackTrace();
30+
} finally {
31+
lock.unlock();
32+
}
33+
}
34+
35+
36+
public void get() {
37+
try {
38+
lock.lock();
39+
while (!hashValue) {
40+
condition.await();
41+
}
42+
System.out.println("打印BBBB");
43+
hashValue = false;
44+
condition.signal();
45+
} catch (InterruptedException e) {
46+
e.printStackTrace();
47+
} finally {
48+
lock.unlock();
49+
}
50+
}
51+
52+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.chen.api.util.thread.study.chapter4.conditionTest;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2018-05-19 02:08
6+
*/
7+
public class Run {
8+
9+
public static void main(String[] args) {
10+
11+
MyService myService = new MyService();
12+
ThreadA threadA = new ThreadA(myService);
13+
threadA.start();
14+
15+
ThreadB threadB = new ThreadB(myService);
16+
threadB.start();
17+
}
18+
19+
20+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.chen.api.util.thread.study.chapter4.conditionTest;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2018-05-19 02:06
6+
*/
7+
public class ThreadA extends Thread {
8+
9+
10+
11+
private MyService service;
12+
13+
public ThreadA (MyService service){
14+
15+
this.service =service;
16+
17+
}
18+
19+
20+
@Override
21+
public void run(){
22+
for (int i = 0; i < 100; i++) {
23+
service.set();
24+
}
25+
26+
}
27+
28+
29+
30+
31+
32+
33+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.chen.api.util.thread.study.chapter4.conditionTest;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2018-05-19 02:06
6+
*/
7+
public class ThreadB extends Thread {
8+
9+
10+
private MyService service;
11+
12+
public ThreadB(MyService service) {
13+
this.service = service;
14+
}
15+
16+
17+
@Override
18+
public void run() {
19+
for (int i = 0; i < 100; i++) {
20+
service.get();
21+
}
22+
23+
}
24+
25+
26+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.chen.api.util.thread.study.chapter4.conditionTestMoreMethod;
2+
3+
import java.util.concurrent.locks.Lock;
4+
import java.util.concurrent.locks.ReentrantLock;
5+
6+
/**
7+
* @author : chen weijie
8+
* @Date: 2018-05-17 00:11
9+
*/
10+
public class MyService {
11+
12+
13+
private Lock lock = new ReentrantLock();
14+
15+
16+
public void methodA() {
17+
18+
try {
19+
lock.lock();
20+
System.out.println("methodA begin ThreadName= " + Thread.currentThread().getName() + ",time =" + System.currentTimeMillis());
21+
Thread.sleep(5000);
22+
System.out.println("methodA end ThreadName= " + Thread.currentThread().getName() + ",time =" + System.currentTimeMillis());
23+
} catch (InterruptedException e) {
24+
e.printStackTrace();
25+
} finally {
26+
lock.unlock();
27+
}
28+
}
29+
30+
31+
public void methodB() {
32+
33+
try {
34+
lock.lock();
35+
System.out.println("methodB begin ThreadName= " + Thread.currentThread().getName() + ",time =" + System.currentTimeMillis());
36+
Thread.sleep(5000);
37+
System.out.println("methodB end ThreadName= " + Thread.currentThread().getName() + ",time =" + System.currentTimeMillis());
38+
} catch (InterruptedException e) {
39+
e.printStackTrace();
40+
} finally {
41+
lock.unlock();
42+
}
43+
44+
45+
}
46+
47+
48+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.chen.api.util.thread.study.chapter4.conditionTestMoreMethod;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2018-05-17 00:17
6+
*/
7+
public class Run {
8+
9+
public static void main(String[] args) {
10+
11+
MyService service = new MyService();
12+
13+
ThreadA threadA = new ThreadA(service);
14+
ThreadAA threadAA = new ThreadAA(service);
15+
16+
threadA.setName("a");
17+
threadAA.setName("aa");
18+
19+
ThreadB threadB = new ThreadB(service);
20+
ThreadBB threadBB = new ThreadBB(service);
21+
threadB.setName("b");
22+
threadBB.setName("bb");
23+
24+
threadA.start();
25+
threadAA.start();
26+
threadB.start();
27+
threadBB.start();
28+
29+
30+
}
31+
32+
33+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.chen.api.util.thread.study.chapter4.conditionTestMoreMethod;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2018-05-17 00:14
6+
*/
7+
public class ThreadA extends Thread {
8+
9+
private MyService service;
10+
11+
public ThreadA(MyService service) {
12+
this.service = service;
13+
}
14+
15+
16+
@Override
17+
public void run() {
18+
service.methodA();
19+
}
20+
21+
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.chen.api.util.thread.study.chapter4.conditionTestMoreMethod;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2018-05-17 00:14
6+
*/
7+
public class ThreadAA extends Thread {
8+
9+
private MyService service;
10+
11+
public ThreadAA(MyService service) {
12+
this.service = service;
13+
}
14+
15+
16+
@Override
17+
public void run() {
18+
service.methodA();
19+
}
20+
21+
22+
}

0 commit comments

Comments
 (0)