Skip to content

Commit

Permalink
ArrayBlockingQueue Class
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnasSmicius committed Jan 18, 2018
1 parent fc6efc4 commit 18e7a4a
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 43 deletions.
81 changes: 72 additions & 9 deletions ProducerConsumer/.idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 18 additions & 34 deletions ProducerConsumer/src/com/timbuchalka/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@ public class Main {
public static final String EOF = "EOF";

public static void main(String[] args) {
List<String> buffer = new ArrayList<>();
ReentrantLock bufferLock = new ReentrantLock();
ArrayBlockingQueue<String> buffer = new ArrayBlockingQueue<String>(6);

ExecutorService executorService = Executors.newFixedThreadPool(3);
ExecutorService executorService = Executors.newFixedThreadPool(5);

MyProducer producer = new MyProducer(buffer, ThreadColor.ANSI_RED, bufferLock);
MyConsumer consumer1 = new MyConsumer(buffer, ThreadColor.ANSI_PURPLE, bufferLock);
MyConsumer consumer2 = new MyConsumer(buffer, ThreadColor.ANSI_CYAN, bufferLock);
MyProducer producer = new MyProducer(buffer, ThreadColor.ANSI_RED);
MyConsumer consumer1 = new MyConsumer(buffer, ThreadColor.ANSI_PURPLE);
MyConsumer consumer2 = new MyConsumer(buffer, ThreadColor.ANSI_CYAN);

executorService.execute(producer);
executorService.execute(consumer1);
Expand All @@ -46,14 +45,13 @@ public String call() throws Exception {
}

class MyProducer implements Runnable {
private List<String> buffer;
private ArrayBlockingQueue<String> buffer;
private String color;
private ReentrantLock bufferLock;

public MyProducer(List<String> buffer, String color, ReentrantLock bufferLock) {
public MyProducer(ArrayBlockingQueue<String> buffer, String color) {
this.buffer = buffer;
this.color = color;
this.bufferLock = bufferLock;
}

public void run() {
Expand All @@ -63,62 +61,48 @@ public void run() {
for (String num : nums) {
try {
System.out.println(color + "Adding..." + num);
bufferLock.lock();
try {
buffer.add(num);
} finally {
bufferLock.unlock();
}
buffer.put(num);

Thread.sleep(random.nextInt(1000));
} catch (InterruptedException e) {
System.out.println("Producer was interrupted");
}
}

System.out.println(color + "Adding EOF and exiting...");
bufferLock.lock();
try {
buffer.add("EOF");
} finally {
bufferLock.unlock();
buffer.put("EOF");
} catch (InterruptedException e) {
}
}
}

class MyConsumer implements Runnable {
private List<String> buffer;
private ArrayBlockingQueue<String> buffer;
private String color;
private ReentrantLock bufferLock;

public MyConsumer(List<String> buffer, String color, ReentrantLock bufferLock) {
public MyConsumer(ArrayBlockingQueue<String> buffer, String color) {
this.buffer = buffer;
this.color = color;
this.bufferLock = bufferLock;
}

public void run() {

int counter = 0;

while (true) {
if (bufferLock.tryLock()) {
synchronized (buffer) {
try {
if (buffer.isEmpty()) {
continue;
}
System.out.println(color + "The counter = " + counter);
counter = 0;
if (buffer.get(0).equals(EOF)) {

if (buffer.peek().equals(EOF)) {
System.out.println(color + "Exiting");
break;
} else {
System.out.println(color + "Removed " + buffer.remove(0));
System.out.println(color + "Removed " + buffer.take());
}
} finally {
bufferLock.unlock();
} catch (InterruptedException e) {
}
} else {
counter++;
}
}
}
Expand Down

0 comments on commit 18e7a4a

Please sign in to comment.