这是顺序循环:
private boolean canSpawnVehicle(SpawnPoint spawnPoint) {
// TODO: can be made much faster.
Rectangle2D noVehicleZone = spawnPoint.getNoVehicleZone();
for(VehicleSimView vehicle : vinToVehicles.values()) {
if (vehicle.getShape().intersects(noVehicleZone)) {
return false;
}
}
return true;
}
这是我对执行程序服务和可调用实现类进行线程化的尝试:
private boolean canSpawnVehicle(SpawnPoint spawnPoint) throws ExecutionException, InterruptedException {
// TODO: can be made much faster.
Rectangle2D noVehicleZone = spawnPoint.getNoVehicleZone();
ExecutorService executor = Executors.newFixedThreadPool(6);
Future<Boolean> future;
for (VehicleSimView vehicle : vinToVehicles.values()) {
future = executor.submit(new CanSpawnThread(vehicle, noVehicleZone));
if(!future.get()){
return false;
}
}
executor.shutdown();
return true;
}
这是线程类:
公共类 CanSpawnThread 实现 Callable {
private VehicleSimView vehicle;
private Rectangle2D noVehicleZone;
public CanSpawnThread(VehicleSimView vehicle, Rectangle2D noVehicleZone){
this.vehicle = vehicle;
this.noVehicleZone = noVehicleZone;
}
public Boolean call() {
boolean can = true;
if (vehicle.getShape().intersects(noVehicleZone)){
can = false;
}
return can;
}
}
哆啦的时光机
扬帆大鱼
精慕HU
相关分类