用wait和notifyAll实现生产者消费者
import java.util.*;
public class Main {
static int size = 5;
static List<Integer> list = new ArrayList<>();
static class Consumer implements Runnable {
@Override
public void run() {
while (true) {
synchronized (list) {
if (list.size() == size) {
System.out.println("size=full");
try {
list.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
list.add(0);
System.out.println(list.size());
list.notifyAll();
}
}
}
}
static class Producer implements Runnable {
@Override
public void run() {
while (true) {
synchronized (list) {
if (list.size() == 0) {
System.out.println("size=0");
try {
list.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
list.remove(0);
System.out.println(list.size());
list.notifyAll();
}
}
}
}
public static void main(String[] args) {
new Thread(new Consumer()).start();
new Thread(new Producer()).start();
}
}
用ReentrantLock和Condition 实现生产者消费者
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Main {
static Lock lock = new ReentrantLock();
static Condition condition = lock.newCondition();
static int size = 5;
static List<Integer> list = new ArrayList<>();
static class Consumer implements Runnable {
@Override
public void run() {
while (true) {
lock.lock();
if (list.size() == 0) {
try {
condition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
list.remove(0);
System.out.println(list.size());
condition.signalAll();
lock.unlock();
}
}
}
static class Producer implements Runnable {
@Override
public void run() {
while (true) {
lock.lock();
if (list.size() == size) {
try {
condition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
list.add(0);
System.out.println(list.size());
condition.signalAll();
lock.unlock();
}
}
}
public static void main(String[] args) {
new Thread(new Consumer()).start();
new Thread(new Producer()).start();
}
}
数组实现队列
public class MyQueue<T> {
private Object[] array;
private int size;
private int head, last;
public MyQueue() {
array = new Object[10];
head = 0;
last = 0;
size = 0;
}
public MyQueue(int length) {
array = new Object[length];
head = 0;
last = 0;
size = 0;
}
public T pop() {
if (isEmpty()) {
return null;
}
T result = (T) array[head];
head = (head + 1) % array.length;
size--;
return result;
}
public void push(T t) {
if (isFull()) {
return;
}
array[last] = t;
last = (last + 1) % array.length;
size++;
}
public Object[] getArray() {
return array;
}
public int getSize() {
return size;
}
public int getHead() {
return head;
}
public int getLast() {
return last;
}
public boolean isFull() {
return array.length == size;
}
public boolean isEmpty() {
return size == 0;
}
}
实现阻塞队列
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class MyBlockingQueue<T> extends MyQueue {
private Lock lock = new ReentrantLock();
private Condition full = lock.newCondition();
private Condition empty = lock.newCondition();
public MyBlockingQueue() {
}
public MyBlockingQueue(int length) {
super(length);
}
@Override
public T pop() {
lock.lock();
try {
if (isEmpty()) {
System.out.println("空");
empty.await();
System.out.println("已存放");
}
full.signal();
return (T) super.pop();
} catch (InterruptedException e) {
e.printStackTrace();
return null;
} finally {
lock.unlock();
}
}
@Override
public void push(Object o) {
lock.lock();
try {
if (isFull()) {
System.out.println("满");
full.await();
System.out.println("已消费");
}
empty.signal();
super.push(o);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}