package com.muyu.vehicle; import com.muyu.common.ThreadPool; import com.muyu.domain.Vehicle; import com.muyu.utils.VehicleUtils; import com.muyu.vehicle.core.LocalContainer; import com.muyu.vehicle.model.VehicleData; import com.muyu.vehicle.model.properties.MqttProperties; import lombok.extern.log4j.Log4j2; import java.lang.reflect.Array; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.concurrent.CountDownLatch; import static java.lang.Thread.sleep; /** * @author DongZl * @description: * @Date 2023-11-17 上午 09:02 */ @Log4j2 public class Test { public static CountDownLatch countDownLatch; public static void main(String[] args) throws InterruptedException { log.info("开始生成VIN"); long genVinStartTime = System.currentTimeMillis(); List list = new ArrayList<>(); for (int i = 0; i < 100; i++) { list.add(VehicleUtils.genVin()); } log.info("生成VIN结束:[{}MS]", System.currentTimeMillis()-genVinStartTime); log.info("开始创建车辆"); long initVehicleStartTime = System.currentTimeMillis(); list.forEach(Test::init); log.info("创建车辆结束:[{}MS]", System.currentTimeMillis()-initVehicleStartTime); list.forEach(vin -> { VehicleInstance vehicleIns = LocalContainer.getVehicleInstance(vin); vehicleIns.initVehicleThread(); vehicleIns.startSend(); }); // countDownLatch.await(); // ThreadPool.shutdown(); } public static void init(String vin){ Vehicle vehicle = Vehicle.builder() .vin(vin) .createTime(new Date()) .remainingBattery(new BigDecimal("45000")) .batteryLevel(new BigDecimal("50000")) .build(); VehicleInstance vehicleInstance = new VehicleInstance( MqttProperties.builder() .broker("tcp://fluxmq.muyu.icu:1883") .topic("test") .clientId(vin) .build() ); vehicleInstance.initCline(); vehicleInstance.setVehicle(vehicle); vehicleInstance.setVehicleData(VehicleData.vehicleBuild(vehicle)); LocalContainer.setVehicleInstance(vehicleInstance); } } class TestThread implements Runnable{ private String vin ; public TestThread(String vin) { this.vin = vin; } /** * When an object implementing interface {@code Runnable} is used * to create a thread, starting the thread causes the object's * {@code run} method to be called in that separately executing * thread. *

* The general contract of the method {@code run} is that it may * take any action whatsoever. * * @see Thread#run() */ @Override public void run() { VehicleInstance vehicleIns = LocalContainer.getVehicleInstance(vin); vehicleIns.initVehicleThread(); // try { // sleep(3000); // } catch (InterruptedException e) { // throw new RuntimeException(e); // } vehicleIns.startSend(); /*try { sleep(3000); } catch (InterruptedException e) { throw new RuntimeException(e); } vehicleIns.pauseSend(); try { sleep(3000); } catch (InterruptedException e) { throw new RuntimeException(e); } vehicleIns.startSend();*/ // try { // sleep(10000); // } catch (InterruptedException e) { // throw new RuntimeException(e); // } // vehicleIns.stopSend(); // // Test.countDownLatch.countDown(); } }