2017-02-14 41 views
0

我已经尝试了三种方法,但我无法获得任何工作。我从这个帖子的顶部答案中拿了很多我的代码How to know if other threads have finished?我在那个列表中紧跟第5条。这是我的问题:从我的主线程中,我需要能够检测按下按钮时辅助线程是否正在运行。检测一个线程是否正在运行java fx

1日法

NotifyThread task = null; 

@FXML 
private void handleButtonPress(ActionEvent event){ 
if (thread == null){ 
    NotifyThread thread = new Thread2(1,0); 
} 
else{ 
    //task is already running 
    thread.end() 
} 

与上面的代码的问题是,线程总是空。而不会终止现有线程的它创建另一个运行旁边(不好)

第二个方法

if (thread.isAlive()) 

第三届方法

if(thread.getState.equals(RUNNABLE)) 

这两种方法给我运行时异常

回答

1

您需要

thread = new Thread2(1,0); 

而不是

NotifyThread thread = new Thread2(1,0); 
+0

好的。我刚刚离开我发布的链接,他说NotifyThread thread1 = new OneOfYourThreads(); –

相关问题