java 生產者消費者簡單實現demo


第一種方式 使用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);
}

}
}

}



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM