Skip to content

Commit 8435711

Browse files
author
Artem Pronchakov
committed
Add Threads ReadWriteLock example
1 parent 5c68792 commit 8435711

6 files changed

Lines changed: 125 additions & 0 deletions

File tree

.idea/modules.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package edu.javacourse.threads;
2+
3+
import java.util.concurrent.locks.ReadWriteLock;
4+
import java.util.concurrent.locks.ReentrantReadWriteLock;
5+
6+
/**
7+
* @author Artem Pronchakov | email/xmpp: artem.pronchakov@calisto.email
8+
*/
9+
public class ReadWriteLockExample {
10+
11+
public static void main(String[] args) {
12+
13+
ReadWriteLock lock = new ReentrantReadWriteLock();
14+
15+
Thread reader1 = new Thread(new Reader(lock), "reader1");
16+
Thread reader2 = new Thread(new Reader(lock), "reader2");
17+
Thread reader3 = new Thread(new Reader(lock), "reader3");
18+
19+
Thread writer1 = new Thread(new Writer(lock), "writer1");
20+
Thread writer2 = new Thread(new Writer(lock), "writer2");
21+
22+
reader1.start();
23+
reader2.start();
24+
reader3.start();
25+
26+
writer1.start();
27+
writer2.start();
28+
29+
}
30+
31+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package edu.javacourse.threads;
2+
3+
import java.util.Random;
4+
import java.util.concurrent.locks.ReadWriteLock;
5+
6+
/**
7+
* @author Artem Pronchakov | email/xmpp: artem.pronchakov@calisto.email
8+
*/
9+
public class Reader implements Runnable {
10+
11+
private ReadWriteLock lock;
12+
13+
public Reader(ReadWriteLock lock) {
14+
this.lock = lock;
15+
}
16+
17+
@Override
18+
public void run() {
19+
while (true) {
20+
lock.readLock().lock();
21+
System.out.println("Thread " + Thread.currentThread().getName() + " got lock");
22+
23+
System.out.println("\tThread " + Thread.currentThread().getName() + ": Shared data: " + SharedData.i);
24+
25+
try {
26+
Thread.sleep(new Random().nextInt(5));
27+
} catch (InterruptedException e) {
28+
e.printStackTrace();
29+
}
30+
31+
System.out.println("Thread " + Thread.currentThread().getName() + " releasing read lock");
32+
lock.readLock().unlock();
33+
}
34+
}
35+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package edu.javacourse.threads;
2+
3+
/**
4+
* @author Artem Pronchakov | email/xmpp: artem.pronchakov@calisto.email
5+
*/
6+
public class SharedData {
7+
public static int i = 0;
8+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package edu.javacourse.threads;
2+
3+
import java.util.Random;
4+
import java.util.concurrent.locks.ReadWriteLock;
5+
6+
/**
7+
* @author Artem Pronchakov | email/xmpp: artem.pronchakov@calisto.email
8+
*/
9+
public class Writer implements Runnable {
10+
11+
private ReadWriteLock lock;
12+
13+
public Writer(ReadWriteLock lock) {
14+
this.lock = lock;
15+
}
16+
17+
@Override
18+
public void run() {
19+
while (true) {
20+
lock.writeLock().lock();
21+
System.out.println("\t\t\t\t===================================================");
22+
System.out.println("\t\t\t\tThread " + Thread.currentThread().getName() + " got lock");
23+
24+
int newI = SharedData.i + 1;
25+
System.out.println("\t\t\t\t\tThread " + Thread.currentThread().getName() + ": Shared data changing to: " + newI);
26+
SharedData.i = newI;
27+
28+
System.out.println("\t\t\t\tThread " + Thread.currentThread().getName() + " releasing write lock");
29+
System.out.println("\t\t\t\t===================================================");
30+
lock.writeLock().unlock();
31+
32+
try {
33+
Thread.sleep(new Random().nextInt(40));
34+
} catch (InterruptedException e) {
35+
e.printStackTrace();
36+
}
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)