4
    Thread d = new Thread(new Runnable() {
        @Override
        public void run() {
            while(true);
        }
    });
    d.start();

How can I quit the infinite loop, without changing the code inside the method public void run(), and without using d.stop(); (deprecated method).

P.S: I'd prefer publishing the whole exercise details I need to do. That's kinda the thing I need to dill with. They gave me a function which sometimes goes inside infinite loop, and I can't change that method.

Kobi Burnley
  • 105
  • 1
  • 2
  • 10

5 Answers5

9

How can I quit the infinite loop, without changing the code inside the method public void run(), and without using d.stop(); (deprecated method).

I assume this is some sort of academic or interview question. If you can't change the thread code then you can't add an interrupt or volatile boolean check. And you can't call .stop() (which is btw deprecated and never a good idea).

The only thing I can think of is to set the thread be a daemon thread.

Thread d = new Thread(new Runnable() { ... });
...
d.setDaemon(true);
d.start();

It needs to be set daemon before it is started. This is a hack but maybe within the framework of the question. This won't kill the thread immediately but if the last non-daemon thread exits then the thread will be killed by the JVM.

Of course you can also remove the .start() line but that seems outside the realm of the question. System.exit(0); would also bring down the JVM as @MattBall pointed out but that also seems like cheating.

Gray
  • 115,027
  • 24
  • 293
  • 354
  • what if the code within the thread is a third party API call that can hang for an indefinite time? For example consumer.poll() for Kafka which used hang for older versions. Does this solution apply for the same scenario? – user2206366 Feb 26 '20 at 01:27
  • The daemon thread will come down even if it is blocked inside of a Kafka call. That said, Kafka might have some background threads that might not be affected @user2206366. – Gray Mar 04 '20 at 22:46
5

Outside of killing the JVM running the thread, I don't see how you can quit the loop.

A better method would at minimum check for thread interruption:

Thread d = new Thread(new Runnable() {
   @Override
   public void run() {
      while(!Thread.currentThread().isInterrupted());
};
d.start();
d.interrupt();
Adam Kaplan
  • 53
  • 1
  • 2
3

You can't. The only way to stop a thread asynchronously is the stop() method. But without that, you can't.

Sibbo
  • 3,796
  • 2
  • 23
  • 41
2

Without .stop() you need to change the code in the thread itself. see here here for some ideas.

Paul Harris
  • 5,769
  • 1
  • 25
  • 41
  • 1
    Thats true but he also says he doesn't want to use stop which is the only other way. I just thought id provide him a source of where this information comes from and reasons why he might be able to justify the changing of the thread code to someone. – Paul Harris Jan 03 '14 at 16:55
-1

Always avoid while(true). Try while(running). That condition should determine the life of the loop. Then when you set running = false, the life of the loop ends and subsequently the thread.

avijendr
  • 3,958
  • 2
  • 31
  • 46
  • 3
    _"How can I quit the infinite loop, **without changing** the code inside the method public void run()"_ – Alexis C. Jan 03 '14 at 17:01