1

I do have an application which starts a Calculation when User prompt START action. Whenever START action is received a new Thread A is executed. (1a and 1b in the diagram)

This Thread One is created and started at the application startup as below.

public static void main(String[] args) {

    ScheduledExecutorService ses = Executors.newScheduledThreadPool(100);
    ses.schedule(new ThreadOne(refObject), 3, TimeUnit.SECONDS);
    ses.schedule(new ThreadTwo(refObject), 6, TimeUnit.SECONDS);
    ses.schedule(new ThreadThree(refObject), 9, TimeUnit.SECONDS);
    ses.shutdown();

}

This Thread One recursively checks a db level flag like 'isStartReqReceived' and once this flag is true, Thread One will execute it's logic.

Now I want to stop this thread, when User prompt STOP action. Is this possible ? What I really want is to stop Thread One.

Please note that my application is a java application which does not use any framework.

enter image description here

codezoner
  • 1,054
  • 2
  • 13
  • 32

2 Answers2

3

Thread one in its loop should check if it's stopped or if it should continue working.

The check could be done similar to the start flag you mentioned e.g. you could make a isStopReqReceived. Note that you should make this a volatile variable due to thread visibility

Rick Sanchez
  • 4,528
  • 2
  • 27
  • 53
  • 2
    Making the flag `volatile` is not just "best": it's actually required to reliably work. It may work without volatile "by accident", but that could easily change due to code changes or even just JVM changes. – Joachim Sauer Apr 07 '23 at 09:16
  • Yeah you're right, it was bad phrasing on my part. I'll update. Thanks :) – Rick Sanchez Apr 07 '23 at 09:17
  • Interruption does the same thing except it uses its own flag and it wakes up the thread if it is sleeping or waiting at the time of the interruption. – Nathan Hughes Apr 07 '23 at 11:20
  • 1
    Re, "Interruption...wakes up the thread if it is sleeping..." Should emphasize the \*IF\* in that assertion. An interrupt does _not_ generally cause an exception to be thrown in the thread. It only causes certain calls (e.g., `Thread.sleep()`, `o.wait()`, and most I/O operations) to throw. If the thread does not make any of those calls, then the only way that it can respond to an interrupt is if it periodically checks `Thread.currentThread().isInterrupted()`. – Solomon Slow Apr 07 '23 at 15:24
  • @SolomonSlow in any case the code has to check a flag. My point was that the custom flag approach doesn't allow for waking a dormant thread. So if wake-up isn't a concern, you get the same functionality, but if wake-up does matter, the responsiveness isn't there. – Nathan Hughes Apr 08 '23 at 20:44
2

Now I want to stop this thread, when User prompt STOP action. Is this possible ? What I really want is to stop Thread One.

To stop a thread externally we could call interrupt() method of the thread object.

First, you need to keep the reference of ThreadOne in your code.

Thread t1 = new ThreadOne(refObject);

Then you could use this 't1' reference and call interrupt method to stop ThreadOne externally at the place where you want to stop it, based on your use case.

t1.interrupt();
  • 1
    Calling `t.interrupt()` does not "stop" thread `t`. All it does is get thread `t`'s attention. And, it works best if thread `t` was written to expect and handle interrupts. If thread `t` was _not_ written with interrupts in mind, then interrupting it could cause an exception that might effectively would "stop" `t`. Or, it might have no effect at all. https://docs.oracle.com/javase/tutorial/essential/concurrency/interrupt.html – Solomon Slow Apr 07 '23 at 15:29