多线程synchronize下仍然出现争抢资源的问题

it2025-03-26  4

public class Test1 { public static void main(String[] args) { AppleBox ab = new AppleBox(); Producer p = new Producer(ab); Producer p2= new Producer(ab); Consumer c = new Consumer(ab); Thread t = new Thread(p); t.setName("生产者一*****"); t.start(); Thread t2 = new Thread(p2); t2.setName("生产者二&&&&&"); t2.start(); Thread t3 = new Thread(c); t3.setName("消费一#####"); t3.start(); } } class Producer implements Runnable { AppleBox appleBox; public Producer(AppleBox appleBox) { this.appleBox = appleBox; } @Override public void run() { // 生产10个苹果存到appleBox中 for (int i = 0; i < 5; i++) { Apple a = new Apple(i); appleBox.deposite(a); try { Thread.sleep((int) (Math.random() * 1000)); } catch (InterruptedException e) { e.printStackTrace(); } } } } class Consumer implements Runnable { AppleBox appleBox; public Consumer(AppleBox appleBox) { this.appleBox = appleBox; } @Override public void run() { for (int i = 0; i < 10; i++) { Apple a = appleBox.withdraw(); try { Thread.sleep((int) (Math.random() * 1000)); } catch (InterruptedException e) { e.printStackTrace(); } } } } class AppleBox { int index = 0; Apple[] apples = new Apple[5]; public synchronized void deposite(Apple apple) { // 判断apples是否满了,满了,则不能再存 while(index >= apples.length) { try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } apples[index] = apple; index++; System.out.println(Thread.currentThread().getName()+"生产了"+apple); // 没满,就存 this.notifyAll(); } public synchronized Apple withdraw() { while(index == 0) { try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } Apple a = apples[index - 1]; index--; this.notifyAll(); System.out.println(Thread.currentThread().getName()+"消费了"+a); return a; } } class Apple { int id; Apple(int id) { this.id = id; } public String toString() { return "apple " + id; } }

输出结果

为什么出现了 “生产者二&&&&&生产了apple 1 生产者一*****生产了apple 1”

最新回复(0)