File tree Expand file tree Collapse file tree 11 files changed +484
-0
lines changed
03concurrency/0301/src/main/java/java0/conc0302 Expand file tree Collapse file tree 11 files changed +484
-0
lines changed Original file line number Diff line number Diff line change 1+
2+ package java0 .conc0302 .lock ;
3+
4+ import java .util .concurrent .locks .ReentrantLock ;
5+
6+ public class Count {
7+
8+ final ReentrantLock lock = new ReentrantLock ();
9+
10+ public void get () {
11+ // final ReentrantLock lock = new ReentrantLock();
12+ try {
13+ lock .lock ();
14+ System .out .println (Thread .currentThread ().getName () + " get begin" );
15+ Thread .sleep (1000 );
16+ System .out .println (Thread .currentThread ().getName () + " get end" );
17+ lock .unlock ();
18+ } catch (InterruptedException e ) {
19+ e .printStackTrace ();
20+ }
21+ }
22+
23+ public void put () {
24+ // final ReentrantLock lock = new ReentrantLock();
25+ try {
26+ lock .lock ();
27+ System .out .println (Thread .currentThread ().getName () + " put begin" );
28+ Thread .sleep (1000 );
29+ System .out .println (Thread .currentThread ().getName () + " put end" );
30+ lock .unlock ();
31+ } catch (InterruptedException e ) {
32+ e .printStackTrace ();
33+ }
34+ }
35+ }
Original file line number Diff line number Diff line change 1+
2+ package java0 .conc0302 .lock ;
3+
4+ import java .util .concurrent .locks .ReentrantReadWriteLock ;
5+
6+ public class Count2 {
7+
8+ private final ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock ();
9+
10+ public void get () {
11+ rwLock .readLock ().lock ();
12+ try {
13+ System .out .println (Thread .currentThread ().getName () + " get begin" );
14+ Thread .sleep (1000 );
15+ System .out .println (Thread .currentThread ().getName () + " get end" );
16+ } catch (InterruptedException e ) {
17+ e .printStackTrace ();
18+ } finally {
19+ rwLock .readLock ().unlock ();
20+ }
21+ }
22+
23+ public void put () {
24+ rwLock .writeLock ().lock ();
25+ try {
26+ System .out .println (Thread .currentThread ().getName () + " put begin" );
27+ Thread .sleep (1000 );
28+ System .out .println (Thread .currentThread ().getName () + " put end" );
29+ } catch (InterruptedException e ) {
30+ e .printStackTrace ();
31+ } finally {
32+ rwLock .writeLock ().unlock ();
33+ }
34+ }
35+ }
Original file line number Diff line number Diff line change 1+
2+ package java0 .conc0302 .lock ;
3+
4+ public class Count3 {
5+
6+ private byte [] lock1 = new byte [1 ];
7+ private byte [] lock2 = new byte [1 ];
8+
9+ public int num = 0 ;
10+
11+ public void add () {
12+ synchronized (lock1 ) {
13+ try {
14+ Thread .sleep (1000 );
15+ } catch (InterruptedException e ) {
16+ e .printStackTrace ();
17+ }
18+ synchronized (lock2 ) {
19+ num += 1 ;
20+ }
21+ System .out .println (Thread .currentThread ().getName () + "_" + num );
22+ }
23+ }
24+
25+ public void lockMethod () {
26+ synchronized (lock2 ) {
27+ try {
28+ Thread .sleep (1000 );
29+ } catch (InterruptedException e ) {
30+ e .printStackTrace ();
31+ }
32+ synchronized (lock1 ) {
33+ num += 1 ;
34+ }
35+ System .out .println (Thread .currentThread ().getName () + "_" + num );
36+ }
37+ }
38+
39+
40+ }
Original file line number Diff line number Diff line change 1+
2+ package java0 .conc0302 .lock ;
3+
4+ public class LockMain {
5+
6+ public static void main (String [] args ) {
7+ Count3 count3 = new Count3 ();
8+ ThreadA threadA = new ThreadA (count3 );
9+ threadA .setName ("线程A" );
10+ threadA .start ();
11+
12+ ThreadB threadB = new ThreadB (count3 );
13+ threadB .setName ("线程B" );
14+ threadB .start ();
15+
16+ }
17+
18+ }
Original file line number Diff line number Diff line change 1+
2+ package java0 .conc0302 .lock ;
3+
4+ import java .util .concurrent .Semaphore ;
5+ import java .util .concurrent .locks .Lock ;
6+ import java .util .concurrent .locks .ReentrantLock ;
7+
8+ public class ObjectCache <T > {
9+
10+ public interface ObjectFactory <T > {
11+ T makeObject ();
12+ }
13+
14+ class Node {
15+ T obj ;
16+ Node next ;
17+ }
18+
19+ final int capacity ;
20+ final ObjectFactory <T > factory ;
21+ final Lock lock = new ReentrantLock ();
22+ final Semaphore semaphore ;
23+ private Node head ;
24+ private Node tail ;
25+
26+ public ObjectCache (int capacity , ObjectFactory <T > factory ) {
27+ this .capacity = capacity ;
28+ this .factory = factory ;
29+ this .semaphore = new Semaphore (this .capacity );
30+ this .head = null ;
31+ this .tail = null ;
32+ }
33+
34+ public T getObject () throws InterruptedException {
35+ semaphore .acquire ();
36+ return getNextObject ();
37+ }
38+
39+ private T getNextObject () {
40+ lock .lock ();
41+ try {
42+ if (head == null ) {
43+ return factory .makeObject ();
44+ } else {
45+ Node ret = head ;
46+ head = head .next ;
47+ if (head == null ) tail = null ;
48+ ret .next = null ;//help GC
49+ return ret .obj ;
50+ }
51+ } finally {
52+ lock .unlock ();
53+ }
54+ }
55+
56+ private void returnObjectToPool (T t ) {
57+ lock .lock ();
58+ try {
59+ Node node = new Node ();
60+ node .obj = t ;
61+ if (tail == null ) {
62+ head = tail = node ;
63+ } else {
64+ tail .next = node ;
65+ tail = node ;
66+ }
67+
68+ } finally {
69+ lock .unlock ();
70+ }
71+ }
72+
73+ public void returnObject (T t ) {
74+ returnObjectToPool (t );
75+ semaphore .release ();
76+ }
77+ }
Original file line number Diff line number Diff line change 1+
2+ package java0 .conc0302 .lock ;
3+
4+ public class ReentrantLockDemo {
5+
6+ public static void main (String [] args ) {
7+ final Count count = new Count ();
8+
9+ for (int i = 0 ; i < 2 ; i ++) {
10+ new Thread () {
11+ public void run () {
12+ count .get ();
13+ }
14+ }.start ();
15+ }
16+
17+ for (int i = 0 ; i < 2 ; i ++) {
18+ new Thread () {
19+ public void run () {
20+ count .put ();
21+ }
22+ }.start ();
23+ }
24+ }
25+ }
Original file line number Diff line number Diff line change 1+
2+ package java0 .conc0302 .lock ;
3+
4+ public class ReentrantReadWriteLockDemo {
5+
6+ public static void main (String [] args ) {
7+ final Count2 count = new Count2 ();
8+
9+ for (int i = 0 ; i < 5 ; i ++) {
10+ new Thread () {
11+ public void run () {
12+ count .get ();
13+ }
14+ }.start ();
15+ }
16+
17+ for (int i = 0 ; i < 5 ; i ++) {
18+ new Thread () {
19+ public void run () {
20+ count .put ();
21+ }
22+ }.start ();
23+ }
24+ }
25+
26+ /**
27+ *
28+ * Thread-0 get begin
29+ Thread-1 get begin
30+ Thread-2 get begin
31+ Thread-3 get begin
32+ Thread-4 get begin
33+ Thread-0 get end
34+ Thread-1 get end
35+ Thread-2 get end
36+ Thread-4 get end
37+ Thread-3 get end
38+ Thread-5 put begin
39+ Thread-5 put end
40+ Thread-6 put begin
41+ Thread-6 put end
42+ Thread-7 put begin
43+ Thread-7 put end
44+ Thread-8 put begin
45+ Thread-8 put end
46+ Thread-9 put begin
47+ Thread-9 put end
48+ *
49+ */
50+ // 读锁不互斥、写锁互斥
51+ }
Original file line number Diff line number Diff line change 1+
2+ package java0 .conc0302 .lock ;
3+
4+ import java .util .HashMap ;
5+ import java .util .Map ;
6+ import java .util .concurrent .locks .ReentrantReadWriteLock ;
7+
8+ public class ReentrantReadWriteLockDemo2 {
9+
10+ private final Map <String , Object > map = new HashMap <>();
11+
12+ private final ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock ();
13+
14+ public Object readWrite (String key ) {
15+ Object value = null ;
16+ System .out .println ("1.首先开启读锁去缓存中取数据" );
17+ rwLock .readLock ().lock ();
18+ try {
19+ value = map .get (key );
20+ if (value == null ) {
21+ System .out .println ("2.数据不存在,则释放读锁,开启写锁" );
22+ rwLock .readLock ().unlock ();
23+ rwLock .writeLock ().lock ();
24+ try {
25+ if (value == null ) {
26+ value = "aaaa" ;
27+ }
28+ } finally {
29+ System .out .println ("3.释放写锁" );
30+ rwLock .writeLock ().unlock ();
31+ }
32+ System .out .println ("4.开启读锁" );
33+ rwLock .readLock ().lock ();
34+ }
35+ } finally {
36+ System .out .println ("5.释放读锁" );
37+ rwLock .readLock ().unlock ();
38+ }
39+ return value ;
40+ }
41+
42+ public static void main (String [] args ) {
43+ ReentrantReadWriteLockDemo2 demo2 = new ReentrantReadWriteLockDemo2 ();
44+ demo2 .readWrite ("wangwei" );
45+ }
46+
47+ }
Original file line number Diff line number Diff line change 1+
2+ package java0 .conc0302 .lock ;
3+
4+ public class ThreadA extends Thread {
5+ private Count3 count3 ;
6+
7+ public ThreadA (Count3 count3 ) {
8+ this .count3 = count3 ;
9+ }
10+
11+ public void run () {
12+ count3 .add ();
13+ }
14+
15+ }
Original file line number Diff line number Diff line change 1+
2+ package java0 .conc0302 .lock ;
3+
4+ public class ThreadB extends Thread {
5+ private Count3 count3 ;
6+
7+ public ThreadB (Count3 count3 ) {
8+ this .count3 = count3 ;
9+ }
10+
11+ public void run () {
12+ count3 .lockMethod ();
13+ }
14+
15+ }
You can’t perform that action at this time.
0 commit comments