Skip to content

Commit a87e23c

Browse files
committed
结束多线程Lock锁内容。
1 parent 41b3312 commit a87e23c

10 files changed

Lines changed: 291 additions & 0 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package cn.byhieg.threadtutorial.char04;
2+
3+
import java.util.concurrent.locks.ReentrantLock;
4+
5+
/**
6+
* Created by byhieg on 17/1/27.
7+
* Mail to byhieg@gmail.com
8+
*/
9+
public class HoldCountService {
10+
private ReentrantLock lock = new ReentrantLock();
11+
12+
public void serviceMethod1(){
13+
try {
14+
lock.lock();
15+
System.out.println("ServiceMethod1 getHoldCount=" + lock.getHoldCount());
16+
serviceMethod2();
17+
}finally {
18+
lock.unlock();
19+
}
20+
}
21+
22+
public void serviceMethod2(){
23+
try {
24+
lock.lock();
25+
System.out.println("ServiceMethod2 getHoldCount=" + lock.getHoldCount());
26+
}finally {
27+
lock.unlock();
28+
}
29+
}
30+
31+
32+
33+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package cn.byhieg.threadtutorial.char04;
2+
3+
import java.util.concurrent.locks.ReentrantReadWriteLock;
4+
5+
/**
6+
* Created by byhieg on 17/1/28.
7+
* Mail to byhieg@gmail.com
8+
*/
9+
public class ReadReadService {
10+
private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
11+
12+
13+
public void read(){
14+
try{
15+
try{
16+
lock.readLock().lock();
17+
System.out.println("获得读锁" + Thread.currentThread().getName() +
18+
" " + System.currentTimeMillis());
19+
Thread.sleep(1000 * 10);
20+
}finally {
21+
lock.readLock().unlock();
22+
}
23+
}catch (InterruptedException e){
24+
e.printStackTrace();
25+
}
26+
}
27+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package cn.byhieg.threadtutorial.char04;
2+
3+
import java.util.concurrent.locks.ReentrantReadWriteLock;
4+
5+
/**
6+
* Created by byhieg on 17/1/28.
7+
* Mail to byhieg@gmail.com
8+
*/
9+
public class ReadWriteService {
10+
private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
11+
12+
public void read(){
13+
try{
14+
try{
15+
lock.readLock().lock();
16+
System.out.println("获得读锁" + Thread.currentThread().getName()
17+
+ " " + System.currentTimeMillis());
18+
Thread.sleep(1000 * 10);
19+
}finally {
20+
lock.readLock().unlock();
21+
}
22+
}catch (InterruptedException e){
23+
e.printStackTrace();
24+
}
25+
}
26+
27+
public void write(){
28+
try{
29+
try{
30+
lock.writeLock().lock();
31+
System.out.println("获得写锁" + Thread.currentThread().getName()
32+
+ " " + System.currentTimeMillis());
33+
Thread.sleep(1000 * 10);
34+
}finally {
35+
lock.writeLock().unlock();
36+
}
37+
}catch (InterruptedException e){
38+
e.printStackTrace();
39+
}
40+
}
41+
42+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package cn.byhieg.threadtutorial.char04;
2+
3+
import java.util.concurrent.locks.ReentrantReadWriteLock;
4+
5+
/**
6+
* Created by byhieg on 17/1/28.
7+
* Mail to byhieg@gmail.com
8+
*/
9+
public class WriteReadService {
10+
11+
private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
12+
13+
public void read(){
14+
try{
15+
try{
16+
lock.readLock().lock();
17+
System.out.println("获得读锁" + Thread.currentThread().getName()
18+
+ " " + System.currentTimeMillis());
19+
Thread.sleep(1000 * 10);
20+
}finally {
21+
lock.readLock().unlock();
22+
}
23+
}catch (InterruptedException e){
24+
e.printStackTrace();
25+
}
26+
}
27+
28+
public void write(){
29+
try{
30+
try{
31+
lock.writeLock().lock();
32+
System.out.println("获得写锁" + Thread.currentThread().getName()
33+
+ " " + System.currentTimeMillis());
34+
Thread.sleep(1000 * 10);
35+
}finally {
36+
lock.writeLock().unlock();
37+
}
38+
}catch (InterruptedException e){
39+
e.printStackTrace();
40+
}
41+
}
42+
43+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package cn.byhieg.threadtutorial.char04;
2+
3+
import java.util.concurrent.locks.ReentrantReadWriteLock;
4+
5+
/**
6+
* Created by byhieg on 17/1/28.
7+
* Mail to byhieg@gmail.com
8+
*/
9+
public class WriteWriteService {
10+
private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
11+
12+
public void write(){
13+
try{
14+
try{
15+
lock.writeLock().lock();
16+
System.out.println("获得写锁" + Thread.currentThread().getName() +
17+
" " +System.currentTimeMillis());
18+
Thread.sleep(1000 * 10);
19+
}finally {
20+
lock.writeLock().unlock();
21+
}
22+
}catch (InterruptedException e){
23+
e.printStackTrace();
24+
}
25+
}
26+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package cn.byhieg.threadtutorialtest.char04test;
2+
3+
import cn.byhieg.threadtutorial.char04.HoldCountService;
4+
import junit.framework.TestCase;
5+
6+
/**
7+
* Created by byhieg on 17/1/27.
8+
* Mail to byhieg@gmail.com
9+
*/
10+
public class HoldCountServiceTest extends TestCase {
11+
public void testServiceMethod1() throws Exception {
12+
13+
HoldCountService service = new HoldCountService();
14+
service.serviceMethod1();
15+
16+
Thread.sleep(1000 * 5);
17+
}
18+
19+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package cn.byhieg.threadtutorialtest.char04test;
2+
3+
import cn.byhieg.threadtutorial.char04.ReadReadService;
4+
import junit.framework.TestCase;
5+
6+
/**
7+
* Created by byhieg on 17/1/28.
8+
* Mail to byhieg@gmail.com
9+
*/
10+
public class ReadReadServiceTest extends TestCase {
11+
public void testRead() throws Exception {
12+
ReadReadService service = new ReadReadService();
13+
Thread a = new Thread(service::read);
14+
a.setName("A");
15+
16+
Thread b = new Thread(service::read);
17+
b.setName("B");
18+
19+
a.start();
20+
b.start();
21+
22+
}
23+
24+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package cn.byhieg.threadtutorialtest.char04test;
2+
3+
import cn.byhieg.threadtutorial.char04.ReadWriteService;
4+
import junit.framework.TestCase;
5+
6+
/**
7+
* Created by byhieg on 17/1/28.
8+
* Mail to byhieg@gmail.com
9+
*/
10+
public class ReadWriteServiceTest extends TestCase {
11+
public void testRead() throws Exception {
12+
13+
ReadWriteService service = new ReadWriteService();
14+
Thread a = new Thread(service::read);
15+
a.setName("AA");
16+
a.start();
17+
Thread.sleep(1000);
18+
19+
Thread b = new Thread(service::write);
20+
b.setName("BB");
21+
22+
23+
b.start();
24+
Thread.sleep(1000 * 30);
25+
}
26+
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package cn.byhieg.threadtutorialtest.char04test;
2+
3+
import cn.byhieg.threadtutorial.char04.WriteReadService;
4+
import cn.byhieg.threadtutorial.char04.WriteWriteService;
5+
import junit.framework.TestCase;
6+
7+
/**
8+
* Created by byhieg on 17/1/28.
9+
* Mail to byhieg@gmail.com
10+
*/
11+
public class WriteReadServiceTest extends TestCase {
12+
public void testWrite() throws Exception {
13+
14+
WriteReadService service = new WriteReadService();
15+
Thread a = new Thread(service::write);
16+
a.setName("A");
17+
a.start();
18+
Thread.sleep(1000);
19+
20+
Thread b = new Thread(service::read);
21+
b.setName("B");
22+
b.start();
23+
24+
Thread.sleep(1000 * 30);
25+
}
26+
27+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package cn.byhieg.threadtutorialtest.char04test;
2+
3+
import cn.byhieg.threadtutorial.char04.WriteWriteService;
4+
import junit.framework.TestCase;
5+
6+
/**
7+
* Created by byhieg on 17/1/28.
8+
* Mail to byhieg@gmail.com
9+
*/
10+
public class WriteWriteServiceTest extends TestCase {
11+
public void testWrite() throws Exception {
12+
WriteWriteService service = new WriteWriteService();
13+
Thread a = new Thread(service::write);
14+
a.setName("A");
15+
Thread b = new Thread(service::write);
16+
b.setName("B");
17+
18+
a.start();
19+
b.start();
20+
Thread.sleep(1000 * 30);
21+
}
22+
23+
}

0 commit comments

Comments
 (0)