Homework.java
package edu.mama.ls19.homework;
import java.util.Map;
import java.util.Set;
public class Homework {
/**
* 采用Thread子类创建自定义线程或采用Runnable实现类创建自定义线程,解决如下需求:
* 100车货交给4个快递员去送,送的快的多送几车,慢的少送几车,由主线程来统计送货完成后每个快递员的送货量。
*/
public static void delivery() {
//TODO 随意改造DeliveryThread或DeliveryRunnable之一,实现题目要求
DeliveryRunnable counter = new DeliveryRunnable(100);
Thread counterTask1 = new Thread(counter);
Thread counterTask2 = new Thread(counter);
Thread counterTask3 = new Thread(counter);
Thread counterTask4 = new Thread(counter);
counterTask1.start();
counterTask2.start();
counterTask3.start();
counterTask4.start();
try {
counterTask1.join();
counterTask2.join();
counterTask3.join();
counterTask4.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
//遍历map
Set<Map.Entry<String, Integer>> entrySet = counter.getMap().entrySet();
for (Map.Entry<String, Integer> entry : entrySet) {
String key = entry.getKey();
Integer value = entry.getValue();
System.out.printf("%s的送货量:%d\n", key, value);
}
}
/**
* 写两个线程,采用生产者消费者模型完成以下需求:
* 1、两辆快递车向快递站投递,每5秒投递一次共投递3次,每次分别投递5包、10包、15包。
* 2、五个快递员送件,从快递站取件送往客户家,快递员每秒送1包。
* 3、没有快递可送时,快递员可以休息;有快递需要送时,快递员不能休息。
*/
public static void producerAndConsumer() {
//TODO 随意改造Station、StationCar、StationCourier类,实现题目要求
//快递站
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();
}
public static void main(String[] args) {
// delivery();
producerAndConsumer();
}
}
DeliveryRunnable.java
package edu.mama.ls19.homework;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class DeliveryRunnable implements Runnable {
//送货量
private Integer count;
private Map<String, Integer> map = new HashMap<>();
public DeliveryRunnable(Integer count) {
this.count = count;
}
//同步锁
private final String LOCK = "ANYTHING";
@Override
public void run() {
Integer delivered = 0;
while (true) {
synchronized (LOCK) {
if (count == 0) {
break;
}
count--;
System.out.printf(Thread.currentThread().getName() + "线程已送完一车,还剩%d车货\n", count);
delivered++;
}
//等待若干时间,用以模拟小车送货路途中的耗时
try {
//假设每送一车货的时间不确定
Double cost = Math.random() * 200;
Thread.sleep(cost.intValue());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
map.put(Thread.currentThread().getName(), delivered);
}
public Map<String, Integer> getMap() {
return map;
}
}
结果
StationCar.java
package edu.mama.ls19.homework;
/**
* 快递车
*/
public class StationCar extends Thread {
//TODO
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() {
int delivered = 0;
for (int i = 1; i <= 3; i++) {
synchronized (station) {
System.out.println(getName() + ":第" + i + "次向快递站投递,送了" + delivered + "个");
if (i == 1) {
station.count += 5;
delivered += 5;
}
if (i == 2) {
station.count += 10;
delivered += 10;
}
if (i == 3) {
station.count += 15;
delivered += 15;
}
//快递站有快递让快递员干活
station.notifyAll();
}
//模拟快递车开车耗时
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(getName() + ":今日任务完成,共送了" + delivered);
}
}
StationCourier.java
package edu.mama.ls19.homework;
/**
* 快递员
*/
public class StationCourier extends Thread {
//TODO
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() {
int delivered = 0;
while (true) {
synchronized (station) {
if (station.count == 0) {
System.out.println("快递站暂时没有快递:" + getName() + "正在休息中");
//快递小哥等快递
try {
station.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
delivered++;
System.out.println(getName() + "快递小哥送了" + delivered + "个快递");
station.count--;
}
}
//模拟快递小哥送货时间
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
结果: