第一种方式 使用BlockingQueue 阻塞队列
public class Threads {
public static void main(String[] args) {
final ArrayBlockingQueue<Integer> queue=new ArrayBlockingQueue<>(10);
produce produce=new produce(queue);
consumer consumer=new consumer(queue);
consumer.start();
produce.start();
}
static class produce extends Thread{
final ArrayBlockingQueue<Integer> integerArrayBlockingQueue;
public produce(ArrayBlockingQueue<Integer> integerArrayBlockingQueue) {
this.integerArrayBlockingQueue = integerArrayBlockingQueue;
}
@Override
public void run() {
while (true){
Integer random=new Random().nextInt(10);
integerArrayBlockingQueue.add(random);
System.out.println("shen chan shu ju"+random);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
static class consumer extends Thread{
final ArrayBlockingQueue<Integer> integerArrayBlockingQueue;
public consumer(ArrayBlockingQueue<Integer> integerArrayBlockingQueue) {
this.integerArrayBlockingQueue = integerArrayBlockingQueue;
}
@Override
public void run() {
while (true){
try {
Integer element=integerArrayBlockingQueue.take();
System.out.println("xiao fei shu ju "+element);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
第二种方法利用 wait()和 notifyAll()
public class Threads {
private static String lock = "lock";
public static void main(String[] args) {
final List<Integer> list = new ArrayList<>(10);
final Integer max = 10;
produce produce1 = new produce(list, max);
produce produce2 = new produce(list, max);
consumer consumer = new consumer(list, max);
consumer.start();
produce1.start();
produce2.start();
}
static class produce extends Thread {
final List<Integer> list;
final Integer max;
public produce(List<Integer> list, Integer max) {
this.list = list;
this.max = max;
}
@Override
public void run() {
while (true) {
synchronized (lock) {
while (list.size() > max) {
try {
lock.wait();
System.out.println("生产数据已满 线程" + Thread.currentThread().getName() + "已停止");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
int random = new Random().nextInt(10);
list.add(random);
lock.notifyAll();
System.out.println("线程" + Thread.currentThread().getName() + "正在生产数据" + random);
}
}
}
}
static class consumer extends Thread {
final List<Integer> list;
final Integer max;
public consumer(List<Integer> list, Integer max) {
this.list = list;
this.max = max;
}
@Override
public void run() {
while (true) {
synchronized (lock) {
while (list.isEmpty()) {
try {
lock.wait();
System.out.println("消费数据已空 线程" + Thread.currentThread().getName() + "已停止");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
int random = list.remove(0);
lock.notifyAll();
System.out.println("线程" + Thread.currentThread().getName() + "正在消费数据" + random);
}
}
}
}
}
第三种方法 ReentrantLock await() 和 signal() 实现
public class Threads {
private Lock lock=new ReentrantLock();
final Condition notfull=lock.newCondition();
final Condition notempty=lock.newCondition();
public static void main(String[] args) {
final List<Integer> list = new ArrayList<>(10);
final Integer max = 10;
Threads threads = new Threads();
produce produce1 = threads.new produce(list, max);
produce produce2 = threads.new produce(list, max);
consumer consumer = threads.new consumer(list, max);
consumer.start();
produce1.start();
produce2.start();
}
class produce extends Thread {
final List<Integer> list;
final Integer max;
public produce(List<Integer> list, Integer max) {
this.list = list;
this.max = max;
}
@Override
public void run() {
while (true) {
lock.lock();
while (list.size() > max) {
try {
notfull.await();
System.out.println("生产数据已满 线程" + Thread.currentThread().getName() + "已停止");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
int random = new Random().nextInt(10);
list.add(random);
notempty.signal();
System.out.println("线程" + Thread.currentThread().getName() + "正在生产数据" + random);
}
}
}
class consumer extends Thread {
final List<Integer> list;
final Integer max;
public consumer(List<Integer> list, Integer max) {
this.list = list;
this.max = max;
}
@Override
public void run() {
while (true) {
lock.lock();
while (list.isEmpty()) {
try {
notempty.await();
System.out.println("消费数据已空 线程" + Thread.currentThread().getName() + "已停止");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
int random = list.remove(0);
notfull.signal();
System.out.println("线程" + Thread.currentThread().getName() + "正在消费数据" + random);
}
}
}
}