声明:文章内容全都是自己的学习总结,如有不对的地方请大家帮忙指出。有需要沟通交流的可加我QQ群:425120333
这个系统是关于汽车装配过程中让一辆车从生成到完成装配,最后展示的整个过程。相较于前两个,这个系统用到的知识更广,是对所学相关多线程知识的练习。
代码示例如下:
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class CarBuilder {
public static void main(String[] args) {
ExecutorService service = Executors.newCachedThreadPool();
RobotPool pool = new RobotPool();
service.execute(new EngineRobot(pool));
service.execute(new WheelRobot(pool));
service.execute(new DriveTrainRobot(pool));
service.execute(new CarGene());
new CarFactory(pool, service);
service.execute(new CarShow());
//该应用不会结束,下面的语句是为了运行5秒后直接结束
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
service.shutdownNow();
}
}
class Car {
private static int count = 0;
private final int id = count++;
private boolean engine = false, wheel = false, driveTrain = false;
public void setEngine() {
engine = true;
}
public void setWheel() {
wheel = true;
}
public void setDriveTrain() {
driveTrain = true;
}
@Override
public String toString() {
return "Car_" + id + " [ engine=" + engine + " wheel=" + wheel + " driveTrain="
+ driveTrain + " ]";
}
}
class CarQueue extends ArrayBlockingQueue<Car> {
public CarQueue(int capacity) {
super(capacity);
}
private static final long serialVersionUID = 1L;
}
class CarGene implements Runnable {
static CarQueue originQueue = new CarQueue(50);
@Override
public void run() {
try {
while (!Thread.interrupted()) {
TimeUnit.MILLISECONDS.sleep(500);
Car car = new Car();
System.out.println(car);
originQueue.put(car);
}
} catch (InterruptedException e) {
System.out.println("Stop CarGene");
}
}
}
class CarFactory implements Runnable {
CyclicBarrier barrier = new CyclicBarrier(4);
static CarQueue finishQueue = new CarQueue(50);
RobotPool pool;
Car car;
public CarFactory(RobotPool pool, ExecutorService service) {
this.pool = pool;
service.execute(this);
}
@Override
public void run() {
try {
while (!Thread.interrupted()) {
car = CarGene.originQueue.take();
pool.fire(EngineRobot.class, this);
pool.fire(WheelRobot.class, this);
pool.fire(DriveTrainRobot.class, this);
barrier.await();
finishQueue.put(car);
}
} catch (Exception e) {
System.out.println("Stop CarFactory");
}
}
}
class CarShow implements Runnable {
@Override
public void run() {
try {
while (!Thread.interrupted()) {
Car car = CarFactory.finishQueue.take();
System.out.println(car);
}
} catch (InterruptedException e) {
System.out.println("Stop CarShow");
}
}
}
abstract class Robot implements Runnable {
CarFactory carFactory;
RobotPool pool;
boolean working = false;
public Robot(RobotPool pool) {
this.pool = pool;
}
public Robot setCarFactory(CarFactory carFactory) {
this.carFactory = carFactory;
return this;
}
public synchronized void startWorking() {
working = true;
notify();
}
public synchronized void powerDown() {
working = false;
carFactory = null;
pool.add(this);
try {
wait();
} catch (InterruptedException e) {
System.out.println();
}
}
@Override
public void run() {
powerDown();
try {
while (!Thread.interrupted()) {
assembly();
carFactory.barrier.await();
powerDown();
}
} catch (Exception e) {
System.out.println(this + " Stop");
}
}
@Override
public String toString() {
return this.getClass().getName();
}
public abstract void assembly();
}
class EngineRobot extends Robot {
public EngineRobot(RobotPool pool) {
super(pool);
}
@Override
public void assembly() {
System.out.println(this + "had assembly engine...");
carFactory.car.setEngine();
}
}
class WheelRobot extends Robot {
public WheelRobot(RobotPool pool) {
super(pool);
}
@Override
public void assembly() {
System.out.println(this + "had assembly wheel...");
carFactory.car.setWheel();
}
}
class DriveTrainRobot extends Robot {
public DriveTrainRobot(RobotPool pool) {
super(pool);
}
@Override
public void assembly() {
System.out.println(this + "had assembly driveTrain...");
carFactory.car.setDriveTrain();
}
}
class RobotPool {
Set<Robot> robotSet = new HashSet<Robot>();
public synchronized void add(Robot robot) {
robotSet.add(robot);
notify();
}
public synchronized void fire(Class<? extends Robot> robotType, CarFactory carFactory) {
for (Robot robot : robotSet) {
if (robot.getClass().equals(robotType)) {
robotSet.remove(robot);
robot.setCarFactory(carFactory).startWorking();
return;
}
}
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
fire(robotType, carFactory);
}
}
控制台输出:
Car_0 [ engine=false wheel=false driveTrain=false ]
thinking.ershiyi.eight.WheelRobothad assembly wheel…
thinking.ershiyi.eight.EngineRobothad assembly engine…
thinking.ershiyi.eight.DriveTrainRobothad assembly driveTrain…
Car_0 [ engine=true wheel=true driveTrain=true ]
Car_1 [ engine=false wheel=false driveTrain=false ]
thinking.ershiyi.eight.WheelRobothad assembly wheel…
thinking.ershiyi.eight.DriveTrainRobothad assembly driveTrain…
thinking.ershiyi.eight.EngineRobothad assembly engine…
Car_1 [ engine=true wheel=true driveTrain=true ]
Car_2 [ engine=false wheel=false driveTrain=false ]
thinking.ershiyi.eight.EngineRobothad assembly engine…
thinking.ershiyi.eight.DriveTrainRobothad assembly driveTrain…
thinking.ershiyi.eight.WheelRobothad assembly wheel…
Car_2 [ engine=true wheel=true driveTrain=true ]
Car_3 [ engine=false wheel=false driveTrain=false ]
thinking.ershiyi.eight.EngineRobothad assembly engine…
thinking.ershiyi.eight.DriveTrainRobothad assembly driveTrain…
thinking.ershiyi.eight.WheelRobothad assembly wheel…
Car_3 [ engine=true wheel=true driveTrain=true ]
。。。
就是类似上面的输出如果不结束,能够一直循环下去。
到这里,关于多线程的学习也是暂告一段落了。当然,这不是意味着以后就不学相关的多线程的知识。知识我个人的学习习惯是先有广度,
再有深度,所以回去学习其他知识,再回过头继续深入多线程相关知识。(使用多线程编程时一定要考虑全面,把所有可能的想进去,不然很容易出错。)
当然,每个人都有自己的习惯,想要继续深挖多线程的知识肯定是可以的,路有千千万万条,条条大路通罗马!
废话就到这里,请关注我其他方面的学习总结。
光鲜亮丽的背后,是无数个日日夜夜的汗水洒落!(共勉)