主要完成任务:
-
1.
read read
并行化 -
2.
read write
不允许 -
3.
write write
不允许
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
public class ReaderWorker extends Thread { private final SharedData data; public ReaderWorker(SharedData data) { this .data = data; } @Override public void run() { while ( true ) { try { char [] readBuf = data.read(); System.out.println(Thread.currentThread().getName() + " reads " + String.valueOf(readBuf)); } catch (InterruptedException e) { e.printStackTrace(); } } } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
public class ReadWriteLock { /** * 当前有几个线程 在对它进行读操作 */ private int readingReaders = 0 ; /** * 当前有几个线程 等待读操作 */ private int waitingReaders = 0 ; /** * 当前有几个线程 正在写操作 */ private int writingWriters = 0 ; /** * 当前有几个线程 正在写操作 */ private int waitingWriters = 0 ; /** * 偏向于写 */ private boolean preferWriter = true ; public ReadWriteLock() { this ( true ); } public ReadWriteLock( boolean preferWriter) { this .preferWriter = preferWriter; } public synchronized void readLock() throws InterruptedException { this .waitingReaders++; try { /** * 让写的线程先运行 */ while (writingWriters > 0 ||(preferWriter&&waitingWriters> 0 )) { this .wait(); } this .readingReaders++; } finally { this .waitingReaders--; } } public synchronized void readUnLock() { this .readingReaders--; this .notifyAll(); } public synchronized void writeLock() throws InterruptedException { this .waitingWriters++; try { while (readingReaders > 0 || writingWriters > 0 ) { this .wait(); } this .writingWriters++; } finally { this .waitingWriters--; } } public synchronized void writeUnlock() { this .writingWriters--; this .notifyAll(); } } public class SharedData { private final char [] buffer; private final ReadWriteLock lock = new ReadWriteLock(); public SharedData( int size) { this .buffer = new char [size]; for ( int i = 0 ; i < size; i++) { this .buffer[i] = '*' ; } } public char [] read() throws InterruptedException { try { lock.readLock(); return this .doRead(); } finally { lock.readUnLock(); } } public void write( char c) throws InterruptedException { try { lock.writeLock(); this .doWrite(c); } finally { lock.writeUnlock(); } } private void doWrite( char c) { for ( int i = 0 ; i < buffer.length; i++) { buffer[i] = c; slowly( 10 ); } } private char [] doRead() { char [] newBuf = new char [buffer.length]; for ( int i = 0 ; i < buffer.length; i++) { newBuf[i] = buffer[i]; } slowly( 50 ); return newBuf; } private void slowly( int millisecond) { try { Thread.sleep(millisecond); } catch (InterruptedException e) { e.printStackTrace(); } } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
public class WriterWorker extends Thread { private static final Random random = new Random(System.currentTimeMillis()); private final SharedData data; private final String filter; private int index = 0 ; public WriterWorker(SharedData data, String filter) { this .data = data; this .filter = filter; } @Override public void run() { try { while ( true ) { char c = nextChar(); data.write(c); Thread.sleep(random.nextInt( 1000 )); } } catch (InterruptedException e) { e.printStackTrace(); } } private char nextChar() { char c = filter.charAt(index); index++; if (index >= filter.length()) index = 0 ; return c; } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
/** * * ReadWriteLock */ public class ReadWriteLockClient { public static void main(String[] args) { final SharedData sharedData = new SharedData( 10 ); new ReaderWorker(sharedData).start(); new ReaderWorker(sharedData).start(); new ReaderWorker(sharedData).start(); new ReaderWorker(sharedData).start(); new ReaderWorker(sharedData).start(); new WriterWorker(sharedData, "123456" ).start(); new WriterWorker(sharedData, "abcdef" ).start(); } } |
结果:
Thread-0 reads **********
Thread-1 reads **********
Thread-2 reads **********
Thread-3 reads **********
Thread-1 reads aaaaaaaaaa
Thread-3 reads aaaaaaaaaa
Thread-0 reads aaaaaaaaaa
Thread-2 reads aaaaaaaaaa
Thread-1 reads aaaaaaaaaa
Thread-2 reads aaaaaaaaaa
Thread-0 reads aaaaaaaaaa
Thread-3 reads aaaaaaaaaa
Thread-1 reads aaaaaaaaaa
Thread-3 reads aaaaaaaaaa
Thread-0 reads aaaaaaaaaa
Thread-2 reads aaaaaaaaaa
Thread-2 reads aaaaaaaaaa
Thread-0 reads aaaaaaaaaa
Thread-1 reads aaaaaaaaaa
Thread-3 reads aaaaaaaaaa
Thread-2 reads aaaaaaaaaa
Thread-0 reads aaaaaaaaaa
Thread-1 reads aaaaaaaaaa
Thread-3 reads aaaaaaaaaa
Thread-0 reads bbbbbbbbbb
Thread-1 reads bbbbbbbbbb
Thread-2 reads bbbbbbbbbb
Thread-3 reads bbbbbbbbbb
Thread-0 reads bbbbbbbbbb
Thread-1 reads bbbbbbbbbb
Thread-3 reads bbbbbbbbbb
Thread-2 reads bbbbbbbbbb
Thread-0 reads bbbbbbbbbb
Thread-1 reads bbbbbbbbbb
Thread-2 reads bbbbbbbbbb
Thread-3 reads bbbbbbbbbb
Thread-0 reads bbbbbbbbbb
Thread-1 reads bbbbbbbbbb
Thread-3 reads bbbbbbbbbb
Thread-2 reads bbbbbbbbbb
Thread-1 reads 3333333333
Thread-2 reads 3333333333
Thread-3 reads 3333333333
...... 省略
到此这篇关于Java多线程之读写锁分离设计模式的文章就介绍到这了,更多相关Java多线程 读写锁分离内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://juejin.cn/post/7021759134072569887