📄 carsfactory.java
字号:
package homeTask_10_carsFactory;
import java.util.Random;
public class CarsFactory {
public static class Wheel {
}
public static class Body{
private int mark;
public Body(int mark) {
this.setMark(mark);
}
public void setMark(int mark) {
this.mark = mark;
}
public int getMark() {
return mark;
}
}
public static class Engine{
}
public static class Car {
private int Carmark;
private Wheel[] wheels;
private Body body;
private Engine engine;
public Car(int Carmark, Wheel[] wheels, Body body, Engine engine) {
this.setMark(Carmark);
this.setWheels(wheels);
this.setBody(body);
this.setEngine(engine);
}
public void setMark(int Carmark) {
this.Carmark = Carmark;
}
public int getMark() {
return Carmark;
}
public void setWheels(Wheel[] wheels) {
this.wheels = wheels;
}
public Wheel[] getWheels() {
return wheels;
}
public void setBody(Body body) {
this.body = body;
}
public Body getBody() {
return body;
}
public void setEngine(Engine engine) {
this.engine = engine;
}
public Engine getEngine() {
return engine;
}
}
public static class Storehouse implements Runnable {
private BlockingQueue blockingQueueCars;
private BlockingQueue blockingQueuePassengerCars;
private BlockingQueue blockingQueueTrucks;
private BlockingQueue blockingQueueSportsCar;
public Storehouse (BlockingQueue blockingQueueCars,
BlockingQueue blockingQueuePassengerCars,
BlockingQueue blockingQueueTrucks,
BlockingQueue blockingQueueSportsCar){
this.blockingQueueCars = blockingQueueCars;
this.blockingQueuePassengerCars = blockingQueuePassengerCars;
this.blockingQueueTrucks = blockingQueueTrucks;
this.blockingQueueSportsCar = blockingQueueSportsCar;
}
@Override
public void run() {
while (true) {
Car car = (Car) blockingQueueCars.take();
Util.pause(10000);
sendCar(car);
}
}
private void sendCar(Car car) {
Car processedCar = car;
int markCar=processedCar.getMark();
System.out.println(" Receiving Car : " + processedCar+ " with mark - "+markCar);
if (processedCar.getMark()==1){
blockingQueuePassengerCars.put(car);
System.out.println(" Car was send to hangar with number - "+processedCar.getMark() );
}
if (processedCar.getMark()==2){
blockingQueueTrucks.put(car);
System.out.println(" Car was send to hangar with number - "+processedCar.getMark());
}
if (processedCar.getMark()==3){
blockingQueueSportsCar.put(car);
System.out.println(" Car was send to hangar with number - "+processedCar.getMark());
}
}
}
public static class Assembly implements Runnable {
private BlockingQueue blockingQueueCars;
private BlockingQueue blockingQueueWheels;
private BlockingQueue blockingQueueEngines;
private BlockingQueue blockingQueueBodies;
public Assembly (BlockingQueue blockingQueueCars,
BlockingQueue blockingQueueWheels,
BlockingQueue blockingQueueEngines,
BlockingQueue blockingQueueBodies) {
this.blockingQueueCars=blockingQueueCars;
this.blockingQueueWheels=blockingQueueWheels;
this.blockingQueueEngines=blockingQueueEngines;
this.blockingQueueBodies=blockingQueueBodies;
}
@Override
public void run() {
while (true) {
Car car = CreateCar();
Util.pause(1000);
blockingQueueCars.put(car);
}
}
private Car CreateCar() {
System.out.println(" Creation Of Car");
Engine processedEngine =(Engine) blockingQueueEngines.take();
System.out.println(" Car creating with Engine : "+ processedEngine);
Body processedBody = (Body) blockingQueueBodies.take();
System.out.println(" Car creating with Body : "+ processedBody);
Wheel [] processedWheels = new Wheel [4];
for (int i = 0; i < 4; i++) {
processedWheels [i] =(Wheel) blockingQueueWheels.take();
}
System.out.println(" Car creating with wheels: ");
for (int i = 0; i < processedWheels.length; i++) {
System.out.println(processedWheels [i]);
}
int markCar = processedBody.getMark();
System.out.println(" Finished creation of Car with body' s mark- "+ markCar);
return new Car(markCar,processedWheels, processedBody, processedEngine );
}
}
public static class ShopOfWheels implements Runnable {
private BlockingQueue blockingQueue;
public ShopOfWheels (BlockingQueue blockingQueue){
this.blockingQueue=blockingQueue;;
}
@Override
public void run() {
while (true) {
Wheel wheel = createWheels();
System.out.println("Wheel was created: "+ wheel);
blockingQueue.put(wheel);
}
}
private Wheel createWheels() {
Util.pause(6000);
return new Wheel() ;
}
}
public static class ShopOfEngines implements Runnable {
private BlockingQueue blockingQueue;
public ShopOfEngines (BlockingQueue blockingQueue){
this.blockingQueue=blockingQueue;
}
@Override
public void run() {
while(true){
Engine engine = createEngine();
// transmission in Assembly
System.out.println("Engine was created : "+ engine);
blockingQueue.put(engine);
}
}
private Engine createEngine() {
Util.pause(8000);
return new Engine();
}
}
public static class ShopOfBodies implements Runnable{
private BlockingQueue blockingQueue;
public ShopOfBodies ( BlockingQueue blockingQueue){
this.blockingQueue=blockingQueue;
}
@Override
public void run() {
while (true){
Body body = createBodies();
System.out.println("Body was created : "+ body+" with mark- "+body.getMark());
blockingQueue.put(body);
}
}
private Body createBodies() {
int mark = 0;
int k=new Random().nextInt(5001) + 2000;
if (2000<=k && k<4000){
// PasengerCar=1
mark=1;
}
if (4000<=k && k<6000){
// Truck=2
mark=2;
}
if (6000<=k && k<=7000){
// SportCar=3
mark=3;
}
Util.pause(k);
return new Body(mark);
}
}
public static void main(String[] args) {
BlockingQueue blockingQueueBodies = new BlockingQueue();
ShopOfBodies shopOfBodies = new ShopOfBodies(blockingQueueBodies);
new Thread(shopOfBodies).start();
Util.pause(500);
BlockingQueue blockingQueueWheels = new BlockingQueue();
ShopOfWheels shopOfWheels = new ShopOfWheels(blockingQueueWheels);
new Thread(shopOfWheels).start();
Util.pause(500);
BlockingQueue blockingQueueEngines = new BlockingQueue();
ShopOfEngines shopOfEngines = new ShopOfEngines(blockingQueueEngines);
new Thread(shopOfEngines).start();
Util.pause(500);
BlockingQueue blockingQueueCars = new BlockingQueue();
Assembly assembly = new Assembly (blockingQueueCars,blockingQueueWheels,blockingQueueEngines,blockingQueueBodies);
new Thread(assembly).start();
Util.pause(500);
BlockingQueue blockingQueuePassengerCars = new BlockingQueue();
BlockingQueue blockingQueueTrucks= new BlockingQueue();
BlockingQueue blockingQueueSportsCar= new BlockingQueue();
Storehouse storehouse = new Storehouse(blockingQueueCars,blockingQueuePassengerCars,blockingQueueTrucks,blockingQueueSportsCar);
new Thread(storehouse).start();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -