来自URL线程的JavaFXJSON解析冻结UI
我在线程中运行此任务。问题是每次执行时它都会冻结UI。当互联网运行缓慢时,冻结时间更长。如何防止UI冻结,即使它仍在从url收集数据来自URL线程的JavaFXJSON解析冻结UI,java,javafx,javafx-8,Java,Javafx,Javafx 8,我在线程中运行此任务。问题是每次执行时它都会冻结UI。当互联网运行缓慢时,冻结时间更长。如何防止UI冻结,即使它仍在从url收集数据 Task<Void> task = new Task<Void>(){ @Override public Void call() throws Exception { while (true) { Platform.runLater(new Runnab
Task<Void> task = new Task<Void>(){
@Override
public Void call() throws Exception {
while (true) {
Platform.runLater(new Runnable() {
@Override
public void run(){
String json = null;
try {
psname = null;
PumpSites n = table.getSelectionModel().getSelectedItem();
psname = n.getPs();
if(psname == "Cubacub")
json = readUrl(""); //read json from thingspeak.com webpage
else if(psname == "Canduman")
json = readUrl("");
} catch (InterruptedIOException iioe)
{
btn1.setTextFill(Color.RED);
btn2.setTextFill(Color.RED);
btn3.setTextFill(Color.RED);
btn4.setTextFill(Color.RED);
btn5.setTextFill(Color.RED);
btn1.setText("NULL");
btn2.setText("NULL");
btn3.setText("NULL");
btn4.setText("NULL");
btn5.setText("NULL");
}
catch (IOException ioe)
{
btn1.setTextFill(Color.RED);
btn2.setTextFill(Color.RED);
btn3.setTextFill(Color.RED);
btn4.setTextFill(Color.RED);
btn5.setTextFill(Color.RED);
btn1.setText("NULL");
btn2.setText("NULL");
btn3.setText("NULL");
btn4.setText("NULL");
btn5.setText("NULL");
}
catch (Exception e1) {
btn1.setTextFill(Color.RED);
btn2.setTextFill(Color.RED);
btn3.setTextFill(Color.RED);
btn4.setTextFill(Color.RED);
btn5.setTextFill(Color.RED);
btn1.setText("NULL");
btn2.setText("NULL");
btn3.setText("NULL");
btn4.setText("NULL");
btn5.setText("NULL");
}
Gson gson = new Gson();
Page page = gson.fromJson(json, Page.class);
for (Item item : page.feeds)
{
det2 = 1;
btn1.setText(item.field1);
btn2.setText(item.field2);
btn3.setText(item.field3);
btn4.setText(item.field4);
btn5.setText(item.field5);
f2 = Float.parseFloat(item.field2);
f3 = Float.parseFloat(item.field3);
//float f5 = Float.parseFloat(item.field5);
if (f2 <= 10.0)
{
btn1.setTextFill(Color.RED);
btn2.setTextFill(Color.RED);
}
else
{
btn1.setTextFill(Color.BLUE);
btn2.setTextFill(Color.BLUE);
}
if (f3 < 0.9 || f3 > 1.2)
{
btn3.setTextFill(Color.RED);
}
else
{
btn3.setTextFill(Color.BLUE);
}
/*if (f5 > 5.0)
{
btn5.setTextFill(Color.RED);
}
else
{
btn5.setTextFill(Color.BLUE);
}*/
btn4.setTextFill(Color.BLUE);
}
if(det2 == 0)
{
btn1.setTextFill(Color.RED);
btn2.setTextFill(Color.RED);
btn3.setTextFill(Color.RED);
btn4.setTextFill(Color.RED);
btn5.setTextFill(Color.RED);
btn1.setText("NULL");
btn2.setText("NULL");
btn3.setText("NULL");
btn4.setText("NULL");
btn5.setText("NULL");
}
det2 = 0;
}
});
Thread.sleep(10000);
}
}
};
Thread th = new Thread(task);
th.setDaemon(true);
th.start();
Task Task=新任务(){
@凌驾
public Void call()引发异常{
while(true){
Platform.runLater(新的Runnable(){
@凌驾
公开募捐{
字符串json=null;
试一试{
psname=null;
PumpSites n=table.getSelectionModel().getSelectedItem();
psname=n.getPs();
如果(psname==“Cubacub”)
json=readUrl(“”;//从thingspeak.com网页读取json
else if(psname==“Canduman”)
json=readUrl(“”);
}捕获(中断异常iioe)
{
btn1.setTextFill(颜色为红色);
btn2.setTextFill(颜色为红色);
btn3.setTextFill(颜色为红色);
btn4.setTextFill(颜色为红色);
btn5.setTextFill(颜色为红色);
btn1.setText(“空”);
btn2.setText(“空”);
btn3.setText(“空”);
btn4.setText(“空”);
btn5.setText(“空”);
}
捕获(ioe异常ioe)
{
btn1.setTextFill(颜色为红色);
btn2.setTextFill(颜色为红色);
btn3.setTextFill(颜色为红色);
btn4.setTextFill(颜色为红色);
btn5.setTextFill(颜色为红色);
btn1.setText(“空”);
btn2.setText(“空”);
btn3.setText(“空”);
btn4.setText(“空”);
btn5.setText(“空”);
}
捕获(异常e1){
btn1.setTextFill(颜色为红色);
btn2.setTextFill(颜色为红色);
btn3.setTextFill(颜色为红色);
btn4.setTextFill(颜色为红色);
btn5.setTextFill(颜色为红色);
btn1.setText(“空”);
btn2.setText(“空”);
btn3.setText(“空”);
btn4.setText(“空”);
btn5.setText(“空”);
}
Gson Gson=新的Gson();
Page=gson.fromJson(json,Page.class);
用于(项目:第页提要)
{
det2=1;
btn1.setText(项目字段1);
btn2.setText(项目字段2);
btn3.setText(项目字段3);
btn4.setText(项目字段4);
btn5.setText(第5项字段);
f2=浮点值。解析浮点值(项目字段2);
f3=Float.parseFloat(item.field3);
//float f5=float.parseFloat(项字段5);
如果(f2 1.2)
{
btn3.setTextFill(颜色为红色);
}
其他的
{
btn3.setTextFill(颜色为蓝色);
}
/*如果(f5>5.0)
{
btn5.setTextFill(颜色为红色);
}
其他的
{
btn5.setTextFill(颜色:蓝色);
}*/
btn4.setTextFill(颜色为蓝色);
}
如果(det2==0)
{
btn1.setTextFill(颜色为红色);
btn2.setTextFill(颜色为红色);
btn3.setTextFill(颜色为红色);
btn4.setTextFill(颜色为红色);
btn5.setTextFill(颜色为红色);
btn1.setText(“空”);
btn2.setText(“空”);
btn3.setText(“空”);
btn4.setText(“空”);
btn5.setText(“空”);
}
det2=0;
}
});
睡眠(10000);
}
}
};
线程th=新线程(任务);
th.setDaemon(true);
th.start();
问题是每次执行时它都会冻结UI。当互联网运行缓慢时,冻结时间更长。即使UI仍在从url收集数据,如何防止它冻结?UI线程冻结,因为您仍在执行JavaFX应用程序线程()上的所有逻辑
Task<Void> task = new Task<Void>(){
@Override
public Void call() throws Exception {
while (true) {
Platform.runLater(new Runnable() {
@Override
public void run(){
String json = null;
try {
psname = null;
PumpSites n = table.getSelectionModel().getSelectedItem();
psname = n.getPs();
if(psname == "Cubacub")
json = readUrl(""); //read json from thingspeak.com webpage
else if(psname == "Canduman")
json = readUrl("");
} catch (InterruptedIOException iioe)
{
btn1.setTextFill(Color.RED);
btn2.setTextFill(Color.RED);
btn3.setTextFill(Color.RED);
btn4.setTextFill(Color.RED);
btn5.setTextFill(Color.RED);
btn1.setText("NULL");
btn2.setText("NULL");
btn3.setText("NULL");
btn4.setText("NULL");
btn5.setText("NULL");
}
catch (IOException ioe)
{
btn1.setTextFill(Color.RED);
btn2.setTextFill(Color.RED);
btn3.setTextFill(Color.RED);
btn4.setTextFill(Color.RED);
btn5.setTextFill(Color.RED);
btn1.setText("NULL");
btn2.setText("NULL");
btn3.setText("NULL");
btn4.setText("NULL");
btn5.setText("NULL");
}
catch (Exception e1) {
btn1.setTextFill(Color.RED);
btn2.setTextFill(Color.RED);
btn3.setTextFill(Color.RED);
btn4.setTextFill(Color.RED);
btn5.setTextFill(Color.RED);
btn1.setText("NULL");
btn2.setText("NULL");
btn3.setText("NULL");
btn4.setText("NULL");
btn5.setText("NULL");
}
Gson gson = new Gson();
Page page = gson.fromJson(json, Page.class);
for (Item item : page.feeds)
{
det2 = 1;
btn1.setText(item.field1);
btn2.setText(item.field2);
btn3.setText(item.field3);
btn4.setText(item.field4);
btn5.setText(item.field5);
f2 = Float.parseFloat(item.field2);
f3 = Float.parseFloat(item.field3);
//float f5 = Float.parseFloat(item.field5);
if (f2 <= 10.0)
{
btn1.setTextFill(Color.RED);
btn2.setTextFill(Color.RED);
}
else
{
btn1.setTextFill(Color.BLUE);
btn2.setTextFill(Color.BLUE);
}
if (f3 < 0.9 || f3 > 1.2)
{
btn3.setTextFill(Color.RED);
}
else
{
btn3.setTextFill(Color.BLUE);
}
/*if (f5 > 5.0)
{
btn5.setTextFill(Color.RED);
}
else
{
btn5.setTextFill(Color.BLUE);
}*/
btn4.setTextFill(Color.BLUE);
}
if(det2 == 0)
{
btn1.setTextFill(Color.RED);
btn2.setTextFill(Color.RED);
btn3.setTextFill(Color.RED);
btn4.setTextFill(Color.RED);
btn5.setTextFill(Color.RED);
btn1.setText("NULL");
btn2.setText("NULL");
btn3.setText("NULL");
btn4.setText("NULL");
btn5.setText("NULL");
}
det2 = 0;
}
});
Thread.sleep(10000);
}
}
};
Thread th = new Thread(task);
th.setDaemon(true);
th.start();
您应该这样做:
public Void call() throws Exception
{
while (true)
{
try
{
//get json
} catch(Exception e)
{
Platform.runLater(new Runnable()
{
@Override
public void run()
{
//set buttons color and text here
}
}
}
//Rest of your logic here
}
}
我们的想法是,将要从单独线程修改UI组件的所有内容都应该在
平台中处理。如果使用后台线程调用平台,则runLater
。如果使用长时间运行的Runnable
作为参数,则实际上什么都没有实现。Runnable
仍在冻结ap的JavaFX应用程序线程上运行