MySQL
- 6 ответов
- 0 вопросов
3
Вклад в тег
public static class Sensor {
private String name;
private int delta = 1;
public Sensor(String name) {
this.name = name;
}
public SensorMeasuring getSensorMeasuringTask() {
return new SensorMeasuring();
}
private class SensorMeasuring implements Callable<Integer> {
@Override
public Integer call() throws Exception {
System.out.println("Запущен сенсор: " + name + " ");
Random random = new Random();
int result = random.nextInt(10) - delta;
System.out.print("Результат: " + result);
return result;
}
}
}
executorService = Executors.newFixedThreadPool(5);
public long getResult(){
List<Callable<Integer>> measuringTasks = new ArrayList<>();
for(Sensor sensor : sensors) {
measuringTasks.add(sensor.getSensorMeasuringTask());
}
List<Future<Integer>> results;
try {
results = executorService.invokeAll(measuringTasks);
return aggregateResults(results);
} catch (InterruptedException e) {
e.printStackTrace();
}
return 0L;
}