How do I stop a thread

Question from Arbee#3030:

How do i stop a thread?

And how do I stop my bot then? lol

You can interrupt the thread, or cancel the task and then it is up to your code to check the Thread.isInterrupted flag.

Some built in methods which throw interrupted exceptions will do that check for you (like those on HttpClient) but without shutting down you cannot force a thread to stop.

Im kinda lost rn... how do I stop my bot without stopping the thread it is running in?

well - you can communicate to the bot's thread

And how do I do that?

Sharing a reference to an AtomicBoolean would do the trick.

So if your bot has an AtomicBoolean it can check it occasionally to see if the value is true. If it's true, it can clean itself up - and atomic boolean is safe to change from another thread.

But it might be difficult depending on how your bot framework is built

<- Index