0

I have tried three approaches and I couldn't get any to work. I took a lot of my code from the top answer to this post How to know if other threads have finished? I followed #5 on that list pretty closely. Here is my issue: From my main thread I need to be able to detect if the secondary thread is running when a button is pressed.

1st Approach

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()
}

The problem with the above code is that thread is always null. Instead of ending the existing thread it creates another one that runs alongside (bad)

2nd Approach

if (thread.isAlive())

3rd Approach

if(thread.getState.equals(RUNNABLE))

Both of these methods give me runtime exceptions

Community
  • 1
  • 1
Omega Collision
  • 125
  • 2
  • 15

1 Answers1

1

You need

  thread = new Thread2(1,0);

Instead of

  NotifyThread thread = new Thread2(1,0);
James_D
  • 201,275
  • 16
  • 291
  • 322