There is a stream:

Thread t = new Thread(new Runnable() {...}); 

It has an infinite while (true) . If I some time after starting the stream

 t.start(); 

I will execute this code

 t = null; 

will my endless loop inside this thread be stopped?

  • I do not know, rather, NullPointerException will crash - Flippy
  • @ SergeyGrushin, where? In the loop or in the code where I wrote t = null ? - nick
  • In the loop. Well this is IMHO. In essence, the thread loses initialization. And that means all the cycles and other bad things will be destroyed. although stop. I think it will stop - Flippy
  • And why suddenly that you lost the link to the stream should stop it immediately? After all, the TV in your home does not disappear if you do not look at it. - VladD
  • @VladD, I do not understand what you mean. I have already solved the question. - nick

2 answers 2

Not. Only the stop() method can stop the loop and, accordingly, the thread itself.

 t.stop(); 

This

 t = null; 

or

 t.interrupt(); 

will not lead to anything. I just checked it myself.

  • one
    once checked)) - Flippy
  • The stop() method is deprecated and not recommended for use. For interrupt() to work, your thread must be in an interrupted wait state. Read javadoc. The spear method gives a wrong idea about things. - a_gura
  • @a_gura, unfortunately, I can not make a stream in standby mode. It uses Socket.connect(...) with a timeout in seconds, and also indicates the waiting time for the incoming stream. I read Javadoc, from the useful I found only the stop() method. What kind of spear method do you mean? I answered not at random and not thoughtlessly. - nick

I compiled a test application, where at startup a thread is created and started, and the back button becomes null . The result: nothing flies, but the cycle continues its work.

  • quite right. Already tested myself. Thank. - nick