Код: Выделить всё
final int cores = Runtime.getRuntime().availableProcessors();
try (final ExecutorService threadPool = Executors.newFixedThreadPool(cores)) {
for (final YearMonth yearMonth : dataset.keySet()) {
final Runnable task = () -> {
// Call JFreeChart …
};
threadPool.submit(task);
}
threadPool.shutdown();
threadPool.awaitTermination(Long.MAX_VALUE, java.util.concurrent.TimeUnit.NANOSECONDS);
}
< /code>
При просмотре временной шкалы, чтобы проверить оптимальную пропускную способность (то есть использование процессора как можно более высокое), я заметил что-то нечетное: между двумя параллелизированными фазами (с одной темой на ядро). Была длинная фаза, где я не складировал только одну нить, и все остальные. Mre: < /p>
@Test
public void testMultiThreadingClassic() throws InterruptedException {
final int cores = Runtime.getRuntime().availableProcessors();
final CountDownLatch latch = new CountDownLatch(cores);
for (int i = 0; i < cores; i++) {
final int index = i;
new Thread(() -> {
try {
this.createFakeChart(
"Fake Chart " + index,
new File("fake-chart-%s.png".formatted(index))
);
} catch (final IOException e) {
LOG.error("Could not create fake chart", e);
System.exit(1);
} finally {
latch.countDown();
}
}).start();
}
latch.await();
}
private void createFakeChart(String title, File outputFile) throws IOException {
final TimeSeries timeSeries = new TimeSeries("Test Series");
final LocalDateTime startDate = LocalDateTime.of(2020, 1, 1, 0, 0);
final LocalDateTime endDate = LocalDateTime.of(2024, 1, 1, 0, 0);
LocalDateTime currentDate = startDate;
while (!currentDate.isAfter(endDate)) {
timeSeries.add(
new org.jfree.data.time.Day(
currentDate.getDayOfMonth(),
currentDate.getMonthValue(),
currentDate.getYear()
),
Math.random() * 100
);
currentDate = currentDate.plusDays(1);
}
final TimeSeriesCollection dataset = new TimeSeriesCollection();
dataset.addSeries(timeSeries);
final IntervalXYDataset xyBarDataset = new XYBarDataset(dataset, 24 * 60 * 60 * 1000L);
final JFreeChart chart = ChartFactory.createXYBarChart(
title,
"Day",
true,
"Random number",
xyBarDataset
);
final DateAxis dateAxis = new DateAxis();
dateAxis.setDateFormatOverride(new SimpleDateFormat("yyyy-MM-dd"));
dateAxis.setTickUnit(new DateTickUnit(DateTickUnitType.YEAR, 1));
chart.getXYPlot().setDomainAxis(dateAxis);
ChartUtils.saveChartAsPNG(
outputFile,
chart,
1000,
500
);
}
Код: Выделить всё
protected void loadFonts() {
if (discoveredAllFonts) {
return;
}
/* Use lock specific to the font system */
synchronized (this) {
// …
if (fontPath != null) {
if (! gotFontsFromPlatform()) {
registerFontsOnPath(fontPath, false,
Font2D.UNKNOWN_RANK,
false, true);
// …
}
}
// …
}
}
Подробнее здесь: https://stackoverflow.com/questions/797 ... generation