A. Pecora
A. Pecora

Reputation: 135

Kill and Clean a thread that calls a method?

This is what I am trying to do:

This class method is a method that calls a scheduleAtFixedRate and also contains a while that checks for a condition. This loops until it found the condition.

How can I make this whole process controlled by the user (in a nice and efficient way)?

Optimally I want to do this:

If I call the method directly from the UI, it freezes it (because of the while loop).

Now, I tried calling the method from inside a new Thread's run() method, and this works, but when stopping and calling the thread again, IllegalThreadStateException is thrown.

I also tried if (thread.isAlive()) { mainThread = new Thread(mainThread);} But this also doesn't work (it throws the same error as before).

What can I do? I am trying not to make the method that I call a thread/runnable, because it would fill half of it with try/catches (making the code pretty much unreadable).

EDIT: This is what I came up with. As said It doesn't work the second time. Missing variables are simple and can be inferred.

UI CODE:

//Other code
 //Thread
mainThread = new Thread(new Runnable() {
     @Override
     public void run() {
         try
                {
                    magicalManager.execute(2);
                }
            catch (Exception e){}
     }

});



ActionListener StartStopAL = new ActionListener()
    {

        public void actionPerformed(ActionEvent actionEvent)
            {

                if (startButtonStatus == false) {
                    startButton.setText("Start");
                    startButtonStatus = true;

                            mainThread.interrupt();
                            TaskScheduler.getScheduledExecutorService().shutdown();

                }
                else if (startButtonStatus == true) {
                    startButton.setText("Stop");
                    startButtonStatus = false;
                    if (mainThread.isAlive()) {
                    mainThread = new Thread(mainThread);
                    }
                    mainThread.start();

                }

            }
    };

//Add action listener to startButton
//Other code

Class Code:

public class magicalManager 
{
    public static void execute(int ID) throws Exception {
        //run scheduleAtFixedRate
        while(...)
        {
            if (...){
            break;
            }
        }
        return
    }
    public static void KillManager(){
    //kill scheduleAtFixedRate
    }
}

Upvotes: 1

Views: 124

Answers (1)

Nick D
Nick D

Reputation: 651

Once a thread's run() method has completed, the thread can never be restarted . In fact, at that point the thread moves into a dead state. In the dead state, the thread can never be restarted.

The Thread object might still be on the heap, as a living object that you can call other methods on (if appropriate), but the Thread object has permanently lost it's "threadness". In other words, there is no longer a separate call stack, and the Thread object is no longer a thread. It's just an object, at that point, like all other objects.

But, there are design patterns for making a pool of threads that you can keep using to perform different jobs. But you don't do it by restarting a dead thread.

Upvotes: 1

Related Questions