Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

End Java threads after a while statement has been run

I am having an issue ending threads once my program my has finished. I run a threaded clock object and it works perfectly but I need to end all threads when the time ´==´ one hour that bit seems to work I just need to know how to end them. Here is an example of the code I have and this is the only thing that runs in the run method apart from one int defined above this code.

    @Override
    public void run()
    {
        int mins = 5;
        while(clock.getHour() != 1)
        {
            EnterCarPark();
            if(clock.getMin() >= mins)
            {
                System.out.println("Time: " + clock.getTime() + " " + entryPoint.getRoadName() + ": " + spaces.availablePermits() + " Spaces");
                mins += 5;
            }
        }
    }

But when you keep watching the threads that are running in the debug mode of netbeans they keep running after an hour has passed not sure how to fix this. I have tried the interrupt call but it seems to do nothing.

enter image description here

like image 544
bobthemac Avatar asked Jan 23 '15 13:01

bobthemac


2 Answers

There are two ways to stop a thread in a nice way, and one in an evil way.

For all you need access to the object of the thread (or in the first case a Runnable class that is executed on that thread).

So your first task is to make sure you can access a list of all threads you want to stop. Also notice that you need to make sure you are using threadsafe communication when dealing with objects used by several threads!

Now you have the following options

Interrupt mechanisme

Call Thread.interrupt() on each thread. This will throw an InterruptedException on the thread if you are in a blocking function. Otherwise it will only set the isInterrupted() flag, so you have to check this as well. This is a very clean and versatile way that will try to interrupt blocking functions by this thread. However many people don't understand how to nicely react to the InterruptedException, so it could be more prone to bugs.

isRunning flag

Have a boolean 'isRunning' in your thread. The while loop calls a function 'stopRunning()' that sets this boolean to false. In your thread you periodically read this boolean and stop execution when it is set to false. This boolean needs to be threadsafe, this could be done by making it volatile (or using synchronized locking).

This also works well when you have a Runnable, which is currently the advised way of running tasks on Threads (because you can easily move Runnables to Threadpools etc.

Stop thread (EVIL)

A third and EVIL and deprecated way is to call Thread.stop(). This is very unsafe and will likely lead to unexpected behavior, don't do this!

like image 55
Thirler Avatar answered Sep 22 '22 10:09

Thirler


Make sure that the loop inside every thread finishes - if it does in all the threads, it does not make sense that there are prints in the output. Just note that what you are checking in each loop condition check if the current hour is not 1 PM, not if an hour has not passed.

Also, your threads garbage collected, which means that the Garbage Collector is responsible for their destruction after termination - but in that case they should not output anything.

like image 26
Nick Louloudakis Avatar answered Sep 19 '22 10:09

Nick Louloudakis