Skip to content

Commit d2e8f8d

Browse files
author
cicadasmile
committed
Java并发编程:Lock机制下API用法详解
1 parent 0b326fe commit d2e8f8d

File tree

4 files changed

+273
-0
lines changed

4 files changed

+273
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.multy.thread.block06lock02;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.concurrent.TimeUnit;
6+
import java.util.concurrent.locks.Condition;
7+
import java.util.concurrent.locks.Lock;
8+
import java.util.concurrent.locks.ReentrantLock;
9+
10+
public class LockAPI01 {
11+
12+
private static Lock lock = new ReentrantLock() ;
13+
private static Condition condition1 = lock.newCondition() ;
14+
private static Condition condition2 = lock.newCondition() ;
15+
16+
public static void main(String[] args) throws Exception {
17+
List<String> dataList = new ArrayList<>() ;
18+
ReadList readList = new ReadList(dataList);
19+
WriteList writeList = new WriteList(dataList);
20+
new Thread(readList).start();
21+
TimeUnit.SECONDS.sleep(2);
22+
new Thread(writeList).start();
23+
}
24+
// 读数据线程
25+
static class ReadList implements Runnable {
26+
private List<String> dataList ;
27+
public ReadList (List<String> dataList){
28+
this.dataList = dataList ;
29+
}
30+
@Override
31+
public void run() {
32+
lock.lock();
33+
try {
34+
if (dataList.size() != 2){
35+
System.out.println("Read wait...");
36+
condition1.await();
37+
}
38+
System.out.println("ReadList WakeUp...");
39+
for (String element:dataList){
40+
System.out.println("ReadList:"+element);
41+
}
42+
condition2.signalAll();
43+
} catch (InterruptedException e){
44+
e.fillInStackTrace() ;
45+
} finally {
46+
lock.unlock();
47+
}
48+
}
49+
}
50+
// 写数据线程
51+
static class WriteList implements Runnable {
52+
private List<String> dataList ;
53+
public WriteList (List<String> dataList){
54+
this.dataList = dataList ;
55+
}
56+
@Override
57+
public void run() {
58+
lock.lock();
59+
try {
60+
dataList.add("Java") ;
61+
dataList.add("C++") ;
62+
condition1.signalAll();
63+
System.out.println("Write over...");
64+
condition2.await();
65+
System.out.println("Write WakeUp...");
66+
} catch (InterruptedException e){
67+
e.fillInStackTrace() ;
68+
} finally {
69+
lock.unlock();
70+
}
71+
}
72+
}
73+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package com.multy.thread.block06lock02;
2+
3+
import java.util.concurrent.ExecutorService;
4+
import java.util.concurrent.Executors;
5+
import java.util.concurrent.locks.Condition;
6+
import java.util.concurrent.locks.ReentrantLock;
7+
8+
public class LockAPI02 {
9+
public static void main(String[] args) {
10+
PrintInfo printInfo = new PrintInfo() ;
11+
ExecutorService service = Executors.newFixedThreadPool(3);
12+
service.execute(new PrintA(printInfo));
13+
service.execute(new PrintB(printInfo));
14+
service.execute(new PrintC(printInfo));
15+
}
16+
}
17+
class PrintA implements Runnable {
18+
private PrintInfo printInfo ;
19+
public PrintA (PrintInfo printInfo){
20+
this.printInfo = printInfo ;
21+
}
22+
@Override
23+
public void run() {
24+
printInfo.printA ();
25+
}
26+
}
27+
class PrintB implements Runnable {
28+
private PrintInfo printInfo ;
29+
public PrintB (PrintInfo printInfo){
30+
this.printInfo = printInfo ;
31+
}
32+
@Override
33+
public void run() {
34+
printInfo.printB ();
35+
}
36+
}
37+
class PrintC implements Runnable {
38+
private PrintInfo printInfo ;
39+
public PrintC (PrintInfo printInfo){
40+
this.printInfo = printInfo ;
41+
}
42+
@Override
43+
public void run() {
44+
printInfo.printC ();
45+
}
46+
}
47+
class PrintInfo {
48+
// 控制下个执行的线程
49+
private String info = "A";
50+
private ReentrantLock lock = new ReentrantLock();
51+
// 三个线程,三个控制条件
52+
Condition conditionA = lock.newCondition();
53+
Condition conditionB = lock.newCondition();
54+
Condition conditionC = lock.newCondition();
55+
public void printA (){
56+
try {
57+
lock.lock();
58+
while (!info.equals("A")) {
59+
conditionA.await();
60+
}
61+
System.out.print("A");
62+
info = "B";
63+
conditionB.signalAll();
64+
} catch (InterruptedException e) {
65+
e.printStackTrace();
66+
} finally {
67+
lock.unlock();
68+
}
69+
}
70+
public void printB (){
71+
try {
72+
lock.lock();
73+
while (!info.equals("B")) {
74+
conditionB.await();
75+
}
76+
System.out.print("B");
77+
info = "C";
78+
conditionC.signalAll();
79+
} catch (InterruptedException e) {
80+
e.printStackTrace();
81+
} finally {
82+
lock.unlock();
83+
}
84+
}
85+
public void printC (){
86+
try {
87+
lock.lock();
88+
while (!info.equals("C")) {
89+
conditionC.await();
90+
}
91+
System.out.print("C");
92+
info = "A";
93+
conditionA.signalAll();
94+
} catch (InterruptedException e) {
95+
e.printStackTrace();
96+
} finally {
97+
lock.unlock();
98+
}
99+
}
100+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.multy.thread.block06lock02;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import java.util.concurrent.locks.Lock;
6+
import java.util.concurrent.locks.ReadWriteLock;
7+
import java.util.concurrent.locks.ReentrantReadWriteLock;
8+
9+
public class LockAPI03 {
10+
public static void main(String[] args) throws Exception {
11+
DataMap dataMap = new DataMap() ;
12+
Thread read = new Thread(new GetRun(dataMap)) ;
13+
Thread write = new Thread(new PutRun(dataMap)) ;
14+
write.start();
15+
Thread.sleep(2000);
16+
read.start();
17+
}
18+
}
19+
class GetRun implements Runnable {
20+
private DataMap dataMap ;
21+
public GetRun (DataMap dataMap){
22+
this.dataMap = dataMap ;
23+
}
24+
@Override
25+
public void run() {
26+
System.out.println("GetRun:"+dataMap.get("myKey"));
27+
}
28+
}
29+
class PutRun implements Runnable {
30+
private DataMap dataMap ;
31+
public PutRun (DataMap dataMap){
32+
this.dataMap = dataMap ;
33+
}
34+
@Override
35+
public void run() {
36+
dataMap.put("myKey","myValue");
37+
}
38+
}
39+
class DataMap {
40+
Map<String,String> dataMap = new HashMap<>() ;
41+
ReadWriteLock rwLock = new ReentrantReadWriteLock() ;
42+
Lock readLock = rwLock.readLock() ;
43+
Lock writeLock = rwLock.writeLock() ;
44+
45+
// 读取数据
46+
public String get (String key){
47+
readLock.lock();
48+
try{
49+
return dataMap.get(key) ;
50+
} finally {
51+
readLock.unlock();
52+
}
53+
}
54+
// 写入数据
55+
public void put (String key,String value){
56+
writeLock.lock();
57+
try{
58+
dataMap.put(key,value) ;
59+
System.out.println("执行写入结束...");
60+
Thread.sleep(10000);
61+
} catch (Exception e) {
62+
System.out.println("Exception...");
63+
} finally {
64+
writeLock.unlock();
65+
}
66+
}
67+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.multy.thread.block06lock02;
2+
3+
import java.util.concurrent.locks.LockSupport;
4+
5+
public class LockAPI04 {
6+
public static void main(String[] args) throws Exception {
7+
OrderPay orderPay = new OrderPay("UnPaid") ;
8+
Thread orderThread = new Thread(orderPay) ;
9+
orderThread.start();
10+
Thread.sleep(3000);
11+
orderPay.changeState("Pay");
12+
LockSupport.unpark(orderThread);
13+
}
14+
}
15+
class OrderPay implements Runnable {
16+
// 支付状态
17+
private String orderState ;
18+
public OrderPay (String orderState){
19+
this.orderState = orderState ;
20+
}
21+
public synchronized void changeState (String orderState){
22+
this.orderState = orderState ;
23+
}
24+
@Override
25+
public void run() {
26+
if (orderState.equals("UnPaid")){
27+
System.out.println("订单待支付..."+orderState);
28+
LockSupport.park(orderState);
29+
}
30+
System.out.println("orderState="+orderState);
31+
System.out.println("订单准备发货...");
32+
}
33+
}

0 commit comments

Comments
 (0)