0

I know that you can interrupt a thread created from say a runnable class, but how can I interrupt this thread I created from a method? Using a volatile boolean isn't working for me, so I assume either there is a better way to do this, or I need to somehow interrupt it. I don't want to interrupt all threads, just this one.

I created a method that kicks off a Thread like this:

public static void StartSyncThread() {
        new Thread() {
            public void run() {
                    isRunning = true;  // Set to true so while loop will start

                    while (isRunning) {
                        ...
                        }

            } // Close run()
        }.start();

    }

....

public static void KillSyncThread() {
    isRunning = false;
}
Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
Ducksauce88
  • 640
  • 3
  • 12
  • 26
  • This should work. Even though you did not provide the `isRunning` declaration, it will compile only if `isRunning` is declared as a `boolean` static to the enclosing class. – Danny Daglas Aug 18 '15 at 23:18
  • It doesn't work. Even if the boolean is volatile. When I call kill it keeps running. – Ducksauce88 Aug 18 '15 at 23:19
  • Can you provide the `isRunning` declaration? I'm assuming it's declared static to the enclosing class. Maybe you accidentally put a `boolean` in from of the `isRunning = true;` line, and the `run()` method is not picking up changes to some other `isRunning` variable? – Danny Daglas Aug 18 '15 at 23:21
  • `public static volatile boolean isRunning = true` – Ducksauce88 Aug 18 '15 at 23:24
  • 1
    Please provide an MCVE. – Sotirios Delimanolis Aug 18 '15 at 23:55
  • Um...I'm not sure what else to provide. Put a system.out inside the while loop. This isn't a very difficult example. I'm not asking to debug my code, I'm asking how to interpret this thread considering its not in a runnable class.... – Ducksauce88 Aug 18 '15 at 23:58
  • [Your example works fine.](http://ideone.com/lNl05l) But I've potentially made a lot of assumptions. Post an MCVE that reflects **exactly** what you have. Only then can we see why it's not working. – Sotirios Delimanolis Aug 19 '15 at 00:46
  • I'm not able to post the code inside the while loop, but `KillSyncThread()` is called from an exterior class statically. – Ducksauce88 Aug 19 '15 at 01:15
  • It doesn't have to be the same, it has to be representative. – Sotirios Delimanolis Aug 19 '15 at 01:37

2 Answers2

1

If you keep a reference to the thread:

private static Thread myThread;

public static void StartSyncThread() {
    myThread = new Thread() {
        public void run() {    
            while (!Thread.currentThread().isInterrupted()) {
                        ...
            }

        } // Close run()
    }
    myThread.start();
}

then you can call

public static void killSyncThread() {
    if (myThread != null && myThread.isAlive()) {
        myThread.interrupt();
    }
}

to cancel it. This way you can get rid of the static isRunning flag, interrupt() sets a built-in flag that's equivalent to that, plus it will wake the thread up from sleeping or waiting.

If you really did declare the variable as volatile then its updated value should be visible across threads. Could be this is a scoping problem where the flag you're testing isn't the same as what you're setting? With global mutable state it seems likely it could get convoluted fast. Make a small example and verify for yourself that setting the flag or interrupting the thread works, then with that confidence you can look for the real problem.

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
0

It is possible with your code that the thread will not actually start before you call KillSyncThread. If this is the case then the thread will set isRunning to true and then continue to run forever. I would just set isRunning to true before you start the thread.

It would also fail to stop if the code in the

...

has either an infinite inner loop or a call to a blocking function such as a socket read.

WillShackleford
  • 6,918
  • 2
  • 17
  • 33
  • i have `KillSyncThread()` being sent from a button click, so I call `Class.KillSyncThread();`, and Im calling it after the thread is running. I'm using system.out. to tell if it is running or not. – Ducksauce88 Aug 19 '15 at 01:14