DeliveryRunnable类

  1. import java.util.HashMap;
  2. import java.util.Map;
  3. public class DeliveryRunnable implements Runnable {
  4. private Integer count;
  5. private final String LOCK = "ANYTHING";
  6. //创建map集合分别存储每个线程完成的送货数
  7. Map<String, Integer> map = new HashMap<>();
  8. public DeliveryRunnable(Integer count) {
  9. this.count = count;
  10. }
  11. @Override
  12. public void run() {
  13. Integer data = 0;
  14. while (true) {
  15. synchronized (LOCK) {
  16. if (count == 0) {
  17. break;
  18. }
  19. count--;
  20. System.out.printf(Thread.currentThread().getName() + "线程已送完一车,还剩%d车货\n", count);
  21. data++;
  22. }
  23. try {
  24. Double cost = Math.random() * 200;
  25. Thread.sleep(cost.intValue());
  26. } catch (InterruptedException e) {
  27. e.printStackTrace();
  28. }
  29. }
  30. map.put(Thread.currentThread().getName(), data);
  31. }
  32. public Map<String, Integer> getMap() {
  33. return map;
  34. }
  35. }

StationCar类

  1. public class StationCar extends Thread {
  2. private Station station;
  3. public StationCar(String name, Station station) {
  4. super(name);
  5. if (station == null) {
  6. throw new IllegalArgumentException("快递站不能为null");
  7. }
  8. this.station = station;
  9. }
  10. @Override
  11. public void run() {
  12. for (int i = 0; i < 3; i++) {
  13. switch (i) {
  14. case 0:
  15. synchronized (station) {
  16. System.out.println(getName() + ":投递了5包快递");
  17. station.count += 5;
  18. station.notifyAll();
  19. }
  20. break;
  21. case 1:
  22. synchronized (station) {
  23. System.out.println(getName() + ":投递了10包快递");
  24. station.count += 10;
  25. station.notifyAll();
  26. }
  27. break;
  28. case 2:
  29. synchronized (station) {
  30. System.out.println(getName() + ":投递了15包快递");
  31. station.count += 15;
  32. station.notifyAll();
  33. }
  34. break;
  35. }
  36. try {
  37. Thread.sleep(5000);
  38. } catch (InterruptedException e) {
  39. e.printStackTrace();
  40. }
  41. }
  42. System.out.println(getName() + ":不送了!");
  43. }
  44. }

StationCourier类

  1. public class StationCourier extends Thread {
  2. private Station station;
  3. public StationCourier(String name, Station station) {
  4. super(name);
  5. if (station == null) {
  6. throw new IllegalArgumentException("快递站不能为null");
  7. }
  8. this.station = station;
  9. }
  10. @Override
  11. public void run() {
  12. while (true) {
  13. synchronized (station) {
  14. if (station.count == 0) {
  15. System.out.println(getName() + ":快递被送完了,可以休息了!");
  16. try {
  17. station.wait();
  18. } catch (InterruptedException e) {
  19. e.printStackTrace();
  20. }
  21. } else {
  22. System.out.println(getName() + ":送了1包快递");
  23. station.count--;
  24. }
  25. }
  26. try {
  27. Thread.sleep(1000);
  28. } catch (InterruptedException e) {
  29. e.printStackTrace();
  30. }
  31. }
  32. }
  33. }

Station类

  1. public class Station {
  2. public int count = 0;
  3. }

Main函数

  1. import java.util.Map;
  2. public class Test {
  3. public static void main(String[] args) {
  4. delivery();
  5. producerAndConsumer();
  6. }
  7. public static void delivery() {
  8. DeliveryRunnable task = new DeliveryRunnable(100);
  9. Thread t1 = new Thread(task);
  10. Thread t2 = new Thread(task);
  11. Thread t3 = new Thread(task);
  12. Thread t4 = new Thread(task);
  13. t1.start();
  14. t2.start();
  15. t3.start();
  16. t4.start();
  17. try {
  18. t1.join();
  19. t2.join();
  20. t3.join();
  21. t4.join();
  22. } catch (InterruptedException e) {
  23. e.printStackTrace();
  24. }
  25. //获取线程返回的集合,遍历集合获取每个子线程的送货数
  26. Map<String, Integer> map = task.getMap();
  27. for (Map.Entry<String, Integer> entry : map.entrySet()) {
  28. System.out.println(entry.getKey() + ": " + entry.getValue());
  29. }
  30. }
  31. public static void producerAndConsumer() {
  32. Station station = new Station();
  33. new StationCar("快递车1号", station).start();
  34. new StationCar("快递车2号", station).start();
  35. new StationCourier("快递员1号", station).start();
  36. new StationCourier("快递员2号", station).start();
  37. new StationCourier("快递员3号", station).start();
  38. new StationCourier("快递员4号", station).start();
  39. new StationCourier("快递员5号", station).start();
  40. }
  41. }