DeliveryRunnable类
import java.util.HashMap;import java.util.Map;public class DeliveryRunnable implements Runnable { private Integer count; private final String LOCK = "ANYTHING"; //创建map集合分别存储每个线程完成的送货数 Map<String, Integer> map = new HashMap<>(); public DeliveryRunnable(Integer count) { this.count = count; } @Override public void run() { Integer data = 0; while (true) { synchronized (LOCK) { if (count == 0) { break; } count--; System.out.printf(Thread.currentThread().getName() + "线程已送完一车,还剩%d车货\n", count); data++; } try { Double cost = Math.random() * 200; Thread.sleep(cost.intValue()); } catch (InterruptedException e) { e.printStackTrace(); } } map.put(Thread.currentThread().getName(), data); } public Map<String, Integer> getMap() { return map; }}
StationCar类
public class StationCar extends Thread { private Station station; public StationCar(String name, Station station) { super(name); if (station == null) { throw new IllegalArgumentException("快递站不能为null"); } this.station = station; } @Override public void run() { for (int i = 0; i < 3; i++) { switch (i) { case 0: synchronized (station) { System.out.println(getName() + ":投递了5包快递"); station.count += 5; station.notifyAll(); } break; case 1: synchronized (station) { System.out.println(getName() + ":投递了10包快递"); station.count += 10; station.notifyAll(); } break; case 2: synchronized (station) { System.out.println(getName() + ":投递了15包快递"); station.count += 15; station.notifyAll(); } break; } try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println(getName() + ":不送了!"); }}
StationCourier类
public class StationCourier extends Thread { private Station station; public StationCourier(String name, Station station) { super(name); if (station == null) { throw new IllegalArgumentException("快递站不能为null"); } this.station = station; } @Override public void run() { while (true) { synchronized (station) { if (station.count == 0) { System.out.println(getName() + ":快递被送完了,可以休息了!"); try { station.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } else { System.out.println(getName() + ":送了1包快递"); station.count--; } } try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } }}
Station类
public class Station { public int count = 0;}
Main函数
import java.util.Map;public class Test { public static void main(String[] args) { delivery(); producerAndConsumer(); } public static void delivery() { DeliveryRunnable task = new DeliveryRunnable(100); Thread t1 = new Thread(task); Thread t2 = new Thread(task); Thread t3 = new Thread(task); Thread t4 = new Thread(task); t1.start(); t2.start(); t3.start(); t4.start(); try { t1.join(); t2.join(); t3.join(); t4.join(); } catch (InterruptedException e) { e.printStackTrace(); } //获取线程返回的集合,遍历集合获取每个子线程的送货数 Map<String, Integer> map = task.getMap(); for (Map.Entry<String, Integer> entry : map.entrySet()) { System.out.println(entry.getKey() + ": " + entry.getValue()); } } public static void producerAndConsumer() { Station station = new Station(); new StationCar("快递车1号", station).start(); new StationCar("快递车2号", station).start(); new StationCourier("快递员1号", station).start(); new StationCourier("快递员2号", station).start(); new StationCourier("快递员3号", station).start(); new StationCourier("快递员4号", station).start(); new StationCourier("快递员5号", station).start(); }}