Skip to content

add ReentrantReadWriteLock on shared state #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions Concurrency/SharedState.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class SharedState {
// shared resource
private volatile int data;
// lock for the resource
ReadWriteLock rwLock = new ReentrantReadWriteLock();

private static final int READ_CYCLES = (int)1e8;
private static final int WRITE_CYCLES = (int)1e8;

public SharedState(int initialData) {
this.data = initialData;
}

/**
* Retrieves the data from a private static variable.
* Representing a shared resource
*
* @return The data value stored
*/
private int getData() {
rwLock.readLock().lock();
try {
return data;
} finally {
rwLock.readLock().unlock();
}
}

/**
* Updates the value of the private static variable 'data'.
*/
private void updateData() {
rwLock.writeLock().lock();
try {
data += 1;
} finally {
rwLock.writeLock().unlock();
}

}

public static void main(String ...args) throws InterruptedException {
final long startTime = System.nanoTime();
SharedState sharedState = new SharedState(0);
Thread readerThread = new Thread(new Runnable() {
@Override
public void run() {
for(int cycles = 0; cycles < READ_CYCLES; cycles++) {
int value = sharedState.getData();
// to keep I/O low to influence perf
if(cycles % (READ_CYCLES/10) == 0){
System.out.println("read: " + value);
System.out.flush();
}
}
}
});
Thread writerThread = new Thread(new Runnable() {
@Override
public void run() {
for(int cycles = 0; cycles < WRITE_CYCLES; cycles++) {
sharedState.updateData();
int value = sharedState.getData();
if(cycles % (WRITE_CYCLES/10) == 0){
System.out.println("post write: " + value);
System.out.flush();
}
}
}
});
readerThread.start();
writerThread.start();
readerThread.join();
writerThread.join();
final long duration = System.nanoTime() - startTime;
System.out.println("time taken(ns): " + duration);
}
}