Skip to content

Instantly share code, notes, and snippets.

@shade34321
Created September 3, 2017 05:02
Show Gist options
  • Save shade34321/bad135f3a6855413dad0ec9fb9f26ca1 to your computer and use it in GitHub Desktop.
Save shade34321/bad135f3a6855413dad0ec9fb9f26ca1 to your computer and use it in GitHub Desktop.
Reentrant lock excercise
import java.util.List;
import java.util.ArrayList;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class data_buffer<K> {
private ReentrantLock rwlock;
private Condition empty;
private Condition full;
private List<K> data;
private int max_size;
private int current_size;
// Defaults to size 10 due to requirements from class
public data_buffer() {
this(10);
}
public data_buffer(int s) {
max_size = s;
current_size = 0;
rwlock = new ReentrantLock(true);
empty = rwlock.newCondition();
full = rwlock.newCondition();
data = new ArrayList<K>();
}
public int getSize() {
return current_size;
}
public int getMax_size() {
return max_size;
}
public boolean isFull() { return current_size == max_size;}
public boolean isEmpty() { return current_size == 0; };
public void write(K k) {
if (k == null) {
throw new IllegalArgumentException("Value can't be null....");
}
rwlock.lock();
try {
while (isFull()) {
empty.await();
}
if (current_size < max_size) {
data.add(k);
} else {
throw new ArrayIndexOutOfBoundsException();
}
full.signalAll();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
rwlock.unlock();
}
}
public K read() {
K val = null;
rwlock.lock();
try {
while (isEmpty()) {
full.await();
}
if (current_size > 0) {
val = data.remove(0);
} else {
throw new ArrayIndexOutOfBoundsException();
}
empty.signalAll();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
rwlock.unlock();
}
return val;
}
}
public class main {
public static void main(String[] args) throws InterruptedException {
data_buffer<Integer> b = new data_buffer<Integer>();
Reader reader[] = new Reader[10];
Writer writer = new Writer(b);
Thread wthread = new Thread(writer, "writer");
Thread rthread[] = new Thread[10];
for (int i = 0; i < 10; i++) {
reader[i] = new Reader(b);
rthread[i] = new Thread(reader[i], "reader_" + i);
}
wthread.start();
for (int i = 0; i < 10; i++){
rthread[i].start();
}
}
}
public class Reader implements Runnable {
private data_buffer<Integer> buffer;
public Reader(data_buffer<Integer> buffer) {
this.buffer = buffer;
}
@Override
public void run() {
try {
while (!buffer.isEmpty()) {
System.out.printf("Read: %d\n", buffer.read());
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
import java.util.Random;
public class Writer implements Runnable {
private data_buffer<Integer> buffer;
public Writer(data_buffer<Integer> buffer) {
this.buffer = buffer;
}
@Override
public void run() {
try {
for (int i = 0; i < 10; i++) {
Random rand = new Random();
int randomNum = rand.nextInt(1001);
buffer.write(randomNum);
System.out.printf("Wrote: %d\n", randomNum);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment