package com.zz.producerandconsumer;
import java.util.LinkedList;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Bylock {
private static Integer num = 1;
private static final int MAX = 5;
private static int id = 0;
public static void main(String[] args) {
LinkedList<Integer> buffer = new LinkedList<>();
Lock mutex = new ReentrantLock();
Condition isFull = mutex.newCondition();
Condition isEmpty = mutex.newCondition();
int cnt = 0;
while (cnt++ < 4) {
new Thread(new Runnable() {
@Override
public void run() {
for (int x = 1; x <= 3; x++) {
mutex.lock();
try {
while (buffer.size() == MAX) {
isFull.await();
}
buffer.offer(num);
System.out.println("id = " + (++id) + " Producer-" + Thread.currentThread().getId()
+ " produde " + (num++) + " " + x + "th.");
System.out.println("the buffer: " + buffer);
isEmpty.signalAll();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
mutex.unlock();
}
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
int cnt1 = 0;
while (cnt1++ < 3) {
new Thread(new Runnable() {
@Override
public void run() {
for (int x = 1; x <= 4; x++) {
mutex.lock();
try {
while (buffer.isEmpty()) {
isEmpty.await();
}
System.out.println("id = " + (++id) + " Consumer-" + Thread.currentThread().getId()
+ " consume " + buffer.peek() + " " + x + "th.");
buffer.poll();
System.out.println("the buffer: " + buffer);
isFull.signalAll();
} catch (Exception e) {
} finally {
mutex.unlock();
}
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
}
}
package com.zz.producerandconsumer;
import java.util.LinkedList;
public class BySynchronized {
private static final int MAXSISE = 5;
private static LinkedList<Integer> buffer = new LinkedList<>();
private static int id = 1;
private static int num = 1;
private static Object mutex = new Object();
public static void main(String[] args) {
int cnt = 0;
while (cnt++ < 4) {
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 1; i <= 3; i++) {
synchronized (mutex) {
while (buffer.size() == MAXSISE) {
try {
mutex.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
buffer.offer(num);
System.out.printf("ID=%02d", id);
System.out.print(" P=" + Thread.currentThread().getId());
System.out.printf(" p:%02d", num);
System.out.printf(" " + i + "th");
System.out.println(" buffer:" + buffer);
num++;
id++;
mutex.notifyAll();
}
}
}
}).start();
}
int cnt1 = 0;
while (cnt1++ < 3) {
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 1; i <= 4; i++) {
synchronized (mutex) {
while (buffer.isEmpty()) {
try {
mutex.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.printf("ID=%02d", id);
System.out.print(" C=" + Thread.currentThread().getId());
System.out.printf(" c:%02d", buffer.peek());
System.out.printf(" " + i + "th");
buffer.poll();
System.out.println(" buffer:" + buffer);
id++;
mutex.notifyAll();
}
}
}
}).start();
}
}
}
package com.zz.producerandconsumer;
import java.util.LinkedList;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
public class BySemaphore {
private static final int MAXSIZE = 8;
private static int id = 1;
private static int num = 1;
private static LinkedList<Integer> buffer = new LinkedList<>();
private static Semaphore mutex = new Semaphore(1);
private static Semaphore full = new Semaphore(0);
private static Semaphore empty = new Semaphore(MAXSIZE);
public static void main(String[] args) {
int cnt = 0;
while (cnt++ < 3) {
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 1; i <= 4; i++) {
try {
full.acquire();
mutex.acquire();
System.out.printf("ID=%02d", id);
System.out.print(" C=" + Thread.currentThread().getId());
System.out.printf(" c:%02d", buffer.peek());
System.out.printf(" " + i + "th");
buffer.poll();
System.out.println(" buffer:" + buffer);
id++;
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
mutex.release();
empty.release();
}
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
int cnt1 = 0;
while (cnt1++ < 4) {
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 1; i <= 3; i++) {
try {
empty.acquire();
mutex.acquire();
buffer.offer(num);
System.out.printf("ID=%02d", id);
System.out.print(" P=" + Thread.currentThread().getId());
System.out.printf(" p:%02d", num);
System.out.printf(" " + i + "th");
System.out.println(" buffer:" + buffer);
num++;
id++;
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
mutex.release();
full.release();
}
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
}
}