Homework.java

    1. package edu.mama.ls19.homework;
    2. import java.util.Map;
    3. import java.util.Set;
    4. public class Homework {
    5. /**
    6. * 采用Thread子类创建自定义线程或采用Runnable实现类创建自定义线程,解决如下需求:
    7. * 100车货交给4个快递员去送,送的快的多送几车,慢的少送几车,由主线程来统计送货完成后每个快递员的送货量。
    8. */
    9. public static void delivery() {
    10. //TODO 随意改造DeliveryThread或DeliveryRunnable之一,实现题目要求
    11. DeliveryRunnable counter = new DeliveryRunnable(100);
    12. Thread counterTask1 = new Thread(counter);
    13. Thread counterTask2 = new Thread(counter);
    14. Thread counterTask3 = new Thread(counter);
    15. Thread counterTask4 = new Thread(counter);
    16. counterTask1.start();
    17. counterTask2.start();
    18. counterTask3.start();
    19. counterTask4.start();
    20. try {
    21. counterTask1.join();
    22. counterTask2.join();
    23. counterTask3.join();
    24. counterTask4.join();
    25. } catch (InterruptedException e) {
    26. e.printStackTrace();
    27. }
    28. //遍历map
    29. Set<Map.Entry<String, Integer>> entrySet = counter.getMap().entrySet();
    30. for (Map.Entry<String, Integer> entry : entrySet) {
    31. String key = entry.getKey();
    32. Integer value = entry.getValue();
    33. System.out.printf("%s的送货量:%d\n", key, value);
    34. }
    35. }
    36. /**
    37. * 写两个线程,采用生产者消费者模型完成以下需求:
    38. * 1、两辆快递车向快递站投递,每5秒投递一次共投递3次,每次分别投递5包、10包、15包。
    39. * 2、五个快递员送件,从快递站取件送往客户家,快递员每秒送1包。
    40. * 3、没有快递可送时,快递员可以休息;有快递需要送时,快递员不能休息。
    41. */
    42. public static void producerAndConsumer() {
    43. //TODO 随意改造Station、StationCar、StationCourier类,实现题目要求
    44. //快递站
    45. Station station = new Station();
    46. //快递车
    47. new StationCar("快递车1",station).start();
    48. new StationCar("快递车2",station).start();
    49. //快递员
    50. new StationCourier("快递小哥1号",station).start();
    51. new StationCourier("快递小哥2号",station).start();
    52. new StationCourier("快递小哥3号",station).start();
    53. new StationCourier("快递小哥4号",station).start();
    54. new StationCourier("快递小哥5号",station).start();
    55. }
    56. public static void main(String[] args) {
    57. // delivery();
    58. producerAndConsumer();
    59. }
    60. }

    DeliveryRunnable.java

    1. package edu.mama.ls19.homework;
    2. import java.util.HashMap;
    3. import java.util.List;
    4. import java.util.Map;
    5. public class DeliveryRunnable implements Runnable {
    6. //送货量
    7. private Integer count;
    8. private Map<String, Integer> map = new HashMap<>();
    9. public DeliveryRunnable(Integer count) {
    10. this.count = count;
    11. }
    12. //同步锁
    13. private final String LOCK = "ANYTHING";
    14. @Override
    15. public void run() {
    16. Integer delivered = 0;
    17. while (true) {
    18. synchronized (LOCK) {
    19. if (count == 0) {
    20. break;
    21. }
    22. count--;
    23. System.out.printf(Thread.currentThread().getName() + "线程已送完一车,还剩%d车货\n", count);
    24. delivered++;
    25. }
    26. //等待若干时间,用以模拟小车送货路途中的耗时
    27. try {
    28. //假设每送一车货的时间不确定
    29. Double cost = Math.random() * 200;
    30. Thread.sleep(cost.intValue());
    31. } catch (InterruptedException e) {
    32. e.printStackTrace();
    33. }
    34. }
    35. map.put(Thread.currentThread().getName(), delivered);
    36. }
    37. public Map<String, Integer> getMap() {
    38. return map;
    39. }
    40. }

    结果
    image.png
    StationCar.java

    1. package edu.mama.ls19.homework;
    2. /**
    3. * 快递车
    4. */
    5. public class StationCar extends Thread {
    6. //TODO
    7. private Station station;
    8. public StationCar(String name,Station station) {
    9. super(name);
    10. if (station == null){
    11. throw new IllegalArgumentException("快递站不能为null");
    12. }
    13. this.station = station;
    14. }
    15. @Override
    16. public void run() {
    17. int delivered = 0;
    18. for (int i = 1; i <= 3; i++) {
    19. synchronized (station) {
    20. System.out.println(getName() + ":第" + i + "次向快递站投递,送了" + delivered + "个");
    21. if (i == 1) {
    22. station.count += 5;
    23. delivered += 5;
    24. }
    25. if (i == 2) {
    26. station.count += 10;
    27. delivered += 10;
    28. }
    29. if (i == 3) {
    30. station.count += 15;
    31. delivered += 15;
    32. }
    33. //快递站有快递让快递员干活
    34. station.notifyAll();
    35. }
    36. //模拟快递车开车耗时
    37. try {
    38. Thread.sleep(5000);
    39. } catch (InterruptedException e) {
    40. e.printStackTrace();
    41. }
    42. }
    43. System.out.println(getName() + ":今日任务完成,共送了" + delivered);
    44. }
    45. }

    StationCourier.java

    1. package edu.mama.ls19.homework;
    2. /**
    3. * 快递员
    4. */
    5. public class StationCourier extends Thread {
    6. //TODO
    7. private Station station;
    8. public StationCourier(String name,Station station) {
    9. super(name);
    10. if (station == null){
    11. throw new IllegalArgumentException("快递站不能为null");
    12. }
    13. this.station = station;
    14. }
    15. @Override
    16. public void run() {
    17. int delivered = 0;
    18. while (true) {
    19. synchronized (station) {
    20. if (station.count == 0) {
    21. System.out.println("快递站暂时没有快递:" + getName() + "正在休息中");
    22. //快递小哥等快递
    23. try {
    24. station.wait();
    25. } catch (InterruptedException e) {
    26. e.printStackTrace();
    27. }
    28. } else {
    29. delivered++;
    30. System.out.println(getName() + "快递小哥送了" + delivered + "个快递");
    31. station.count--;
    32. }
    33. }
    34. //模拟快递小哥送货时间
    35. try {
    36. Thread.sleep(1000);
    37. } catch (InterruptedException e) {
    38. e.printStackTrace();
    39. }
    40. }
    41. }
    42. }

    结果:
    image.png