Java wait for runnable to finish Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Java wait for runnable to finish without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I have a class that will start a Runnable with an infinite loop. At some point, I want to shut down the program, and I want to do it in a clean way (e.g. ensure that at the end of the execution the thread will flush some buffer to output file and such). How can I do it?

Note: in the comments I wrote // Version 1 and // Version 2. That is because I tried both version and none of them worked, I keep getting an InterruptedException.

Main Class:

public class MainClass {

    private static Thread myThread;
    private static MyRunnable myRunnable;
    private static Logger logger;

    public static void main( String[] args ) {
        myRunnable = new MyRunnable(logger);
        myThread = new Thread(myRunnable);

    private static void shutDown() {
        logger.debug("Shut down everything.");
        if(myRunnable != null){
            try {
                logger.debug("Runnable Stopped");
            } catch (InterruptedException e) { // Always failing here
        // Now stop logger and other things
        logger.debug("Shutdown other stuff");
        logger.debug("Shutdown logger");

Runnable class:

public class MyRunnable implements Runnable {

    private AtomicBoolean stop = new AtomicBoolean(false); // Version 1

    public boolean isStopped() { 
        return stop.get(); // Version 1
        return Thread.currentThread().isInterrupted(); // Version 2

    public void shutdown() {
        logger.debug("Stopping my runnable");
        stop.set(true); // Version 1
        Thread.currentThread().interrupt(); // Version 2

    public void run() {
            while(!isStopped()) {
                // Do things
            logger.debug("Exiting from infinite loop");
        } catch(Exception e){
            logger.error("Exception! " + e.getMessage());
        } finally {
            logger.debug("Entering in finally part");
            // Flush buffer, commit to database, ... close stuff

But the output of the shutDown function is always the same:

Shut down everything.
    at java.lang.Object.wait(Native Method)
    at java.lang.Thread.join(
    at java.lang.Thread.join(
Shutdown other stuff
Shutdown logger

If I remove the join() function, the Exception goes away, but the output is still the same.

EDIT: I just noticed that if I comment the line myRunnable.shutdown(); and I go on with the myThread.join(); line, I still get the exception, even if I am not stopping that thread. Why is that? Is it because the Tread and the Runnable are two separate things? If it is so, then how can I stop the Runnable and wait for its completion?


In “version 2”, you are interrupting the thread that is calling stopParser(), not the thread that is executing run(). Instead of calling Thread.currentThread().interrupt(), use myThread.interrupt().

I don’t know why you’d get an InterruptedException in “version 1”; you must be calling Thread.currentThread().interrupt() somewhere else. You should only do that when you have detected that the current thread has been interrupted by another thread but you can’t terminate the thread immediately.

We are here to answer your question about Java wait for runnable to finish - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji