File tree Expand file tree Collapse file tree
src/edu/javacourse/threads Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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 >
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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 ("\t Thread " + 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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 \t Thread " + Thread .currentThread ().getName () + " got lock" );
23+
24+ int newI = SharedData .i + 1 ;
25+ System .out .println ("\t \t \t \t \t Thread " + Thread .currentThread ().getName () + ": Shared data changing to: " + newI );
26+ SharedData .i = newI ;
27+
28+ System .out .println ("\t \t \t \t Thread " + 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+ }
You can’t perform that action at this time.
0 commit comments