我正在尝试对内存可视化器进行编码,该内存可视化器将使用当前内存统计信息更新图形,并且在其侧面具有数字表示。我有一个在线程中运行的更新方法,应该每秒更新一次。
问题是它没有在GUI上更新,事实上我相信代码在一次迭代后停止。我相信它与一些setText()调用有关,因为如果我将它们注释掉,那么至少图形会更新。
这是我的初始化方法
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
assert memUsage_lineChart != null : "fx:id=\"memUsage_lineChart\" was not injected: check your FXML file 'MemoryVisualization.fxml'.";
assert text_totalMem != null : "fx:id=\"text_totalMem\" was not injected: check your FXML file 'MemoryVisualization.fxml'.";
assert text_usedMem != null : "fx:id=\"text_usedMem\" was not injected: check your FXML file 'MemoryVisualization.fxml'.";
assert text_availableMem != null : "fx:id=\"text_availableMem\" was not injected: check your FXML file 'MemoryVisualization.fxml'.";
assert text_percentageUsed != null : "fx:id=\"text_percentageUsed\" was not injected: check your FXML file 'MemoryVisualization.fxml'.";
assert text_totalRuntime != null : "fx:id=\"text_totalRuntime\" was not injected: check your FXML file 'MemoryVisualization.fxml'.";
assert text_threadsUsed != null : "fx:id=\"text_threadsUsed\" was not injected: check your FXML file 'MemoryVisualization.fxml'.";
assert text_version != null : "fx:id=\"text_version\" was not injected: check your FXML file 'MemoryVisualization.fxml'.";
assert text_timeAtMaxMem != null : "fx:id=\"text_timeAtMaxMem\" was not injected: check your FXML file 'MemoryVisualization.fxml'.";
assert text_maxMemReached != null : "fx:id=\"text_maxMemReached\" was not injected: check your FXML file 'MemoryVisualization.fxml'.";
assert text_timeOfLastAssociation != null : "fx:id=\"text_timeOfLastAssociation\" was not injected: check your FXML file 'MemoryVisualization.fxml'.";
assert text_something != null : "fx:id=\"text_totalRuntime\" was not injected: check your FXML file 'MemoryVisualization.fxml'.";
assert lineChart_yAxis != null : "fx:id=\"lineChart_yAxis\" was not injected: check your FXML file 'MemoryVisualization.fxml'.";
assert lineChart_xAxis != null : "fx:id=\"lineChart_xAxis\" was not injected: check your FXML file 'MemoryVisualization.fxml'.";
runtime = 0L;
//Initializing the ArrayLists<> that will contain the series of data for each possible graph
memData = new ArrayList<XYChart.Series<Number,Number>>();
//Setting the Y-axis of the chart to change according to incoming data.
memUsage_lineChart.getYAxis().setAutoRanging(true);
//Initializes the data arrays and series for the charts
XYChart.Series<Number, Number> series_sec = new XYChart.Series<Number,Number>();
memUsage_lineChart.getData().add(series_sec);
memData.add(series_sec);
//Creates a thread that continuously runs and adds data points to the displays
ScheduledExecutorService ses = Executors.newScheduledThreadPool(1);
ses.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
runtime += 1000;
updateData();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, 0, 500, TimeUnit.MILLISECONDS);
}/*END OF INSTANTIATION*/
这是我的updateData()方法
public void updateData()
{
//Gathers the runtime memory data from the system
long maxFreeMemory = Runtime.getRuntime().maxMemory();
long usableFreeMemory= Runtime.getRuntime().maxMemory()
-Runtime.getRuntime().totalMemory()
+Runtime.getRuntime().freeMemory();
double usedPercent=(double)(Runtime.getRuntime().totalMemory()
-Runtime.getRuntime().freeMemory())/Runtime.getRuntime().maxMemory();
long usedMemory = (long)(usedPercent*maxFreeMemory);
maxFreeMemory /= 1000000;
usableFreeMemory /= 1000000;
usedMemory /= 1000000;
usedPercent *= 100;
DecimalFormat df = new DecimalFormat("0.00");
//Set the text values for the runtime statistics
text_totalMem.textProperty().set(String.valueOf(maxFreeMemory));
text_usedMem.textProperty().set(String.valueOf(usedMemory));
text_availableMem.textProperty().set(String.valueOf(usableFreeMemory));
text_percentageUsed.textProperty().set(df.format(usedPercent)+" %");
text_totalRuntime.textProperty().set((runtime/3600000)+":"+String.format("%02d",((runtime/60000)%60))+":"+String.format("%02d",((runtime%60000)/1000)));
text_threadsUsed.textProperty().set(String.valueOf(ManagementFactory.getThreadMXBean().getThreadCount()));
//Updates the axes of the real-time graphs/charts
if(runtime >= 120000 && (runtime % 12000) == 0)
{
lineChart_xAxis.setLowerBound(lineChart_xAxis.getLowerBound()+12);
lineChart_xAxis.setUpperBound(lineChart_xAxis.getUpperBound()+12);
for(int i=0; i<12; i++)
memData.get(0).getData().remove(0);
}
//Adds the data to the series to be placed on to the graphs/charts
memData.get(0).getData().add(new XYChart.Data<Number,Number>(runtime/1000,usedMemory));
}
有没有理由我的GUI卡住了,并且在运行时不会更新文本字段/图表?我知道一个事实是线程至少运行一次迭代,因为当GUI弹出时文本字段不是0(初始化)。
答案 0 :(得分:3)
使用updateData()
在FX应用程序线程上执行Platform.runLater()
方法。 (另外,我认为Thread.sleep(...)
是多余的,因为您正在使用预定的执行程序。)
ses.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
runtime += 1000;
Platform.runLater(() -> updateData());
}
}, 0, 500, TimeUnit.MILLISECONDS);