-2

first: sorry for the nooby question!

consider a normal basic Java programm:

public class TestClass {
    public static void main(String[] args) {

        Thread t=new Thread()
        {
            public boolean isRunning;
            public void run()
            {
                while(isRunning);
            }
        }
        t.isRunning=true;
        t.start();
        t=null;
    }
}

The Thread would run for ever doesnt he?

How would I stop the Thread when I accidently set it to null like in the code above? Or what happens if for some reason the Thread object gets nullified?

Basically how does the programm behave? Can I query somehow a running thread without any reference to it and the i just yould stop() it somehow?

Now what about when the Thread object also holds some Data that is needed and will be used in the run method? Are they null or can they still be accessed?

If so it is really essential to get the reference of the object back again even more

edit: I added an android tag to the question. I am more thinking of Android enviroments. The process of an app can be killed very easy and things can go so wrong that a thread gets nullified but its execution is still going on, so getting the reference of a thread back again is important. Like when an activity is killed by the system its reference to a thread is also gone. So before a memory leak occurs there must be sth i can do to prevent from having an ongoing thread before starting another one

Ilja KO
  • 1,272
  • 12
  • 26
  • 3
    Have you read the documentation about threads? Most of that is covered there: http://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html – Vincent Mimoun-Prat Sep 23 '15 at 17:24
  • Yes Im not new to Java but this is what I dont know : how to get a reference to a thread that I accidetly nullified – Ilja KO Sep 23 '15 at 17:28
  • you mean how to list all threads i guess – Antoniossss Sep 23 '15 at 17:29
  • @IljaKO You may be thinking the wrong way: accidentally nullifying something that should not be is a **bug** (and a big one with threads which end up running forever). Use a debugger and check where/why the reference gets lots. Use weak references if needed. Just don't try to hide your bugs with some more buggy code. – Vincent Mimoun-Prat Sep 23 '15 at 17:32
  • Yes of course Im aware that this is a bug and should be avoided. But if something happens on the RAM for example and I want my Programm to be aware of it. Thus when my Thread has some Data that I need – Ilja KO Sep 23 '15 at 17:34
  • Some related posts that might help: http://stackoverflow.com/questions/1323408/get-a-list-of-all-threads-currently-running-in-java http://stackoverflow.com/questions/15624296/how-to-find-and-stop-all-currently-running-threads – projectIncomplete Sep 23 '15 at 17:36
  • _...when I accidently set it to null..._ Don't do that. "Accidents" in your code are better known as _defects_. The only way to fix defective code is to change the code so that it isn't defective anymore. – Solomon Slow Sep 23 '15 at 18:15
  • _...what about when the Thread object also holds some Data..._ Setting the variable `t = null;` does not do anything at all to the thread. The variable is not the thread: The variable only holds a _reference_ to the thread object. When you set `t = null;` the thread object still is there, and it won't be garbage collected because the JVM will keep its own references to it as long as the thread is running. – Solomon Slow Sep 23 '15 at 18:20
  • thank you very much. this is what I also experienced when nullyfying a thread but I thought this is jsut how Android works. How can I get hold of this reference again when itrs lsot for some reasons? Just for the sake of preventing memory leaks like when app process is killed and I need to start a new thread becuase for some reason the old thread had no chance to get stopped because a call to set the isRunning variable failed inside the activity with nullpointerexception – Ilja KO Sep 23 '15 at 22:19
  • Im sorry here a better text: It can now occur that my process is killed and for some reason the Activity looses the reference of the Thread while killing process before it has the chance to set the isRunning variable to false so that the old Thread can stop execution before the Activity is restarted again – Ilja KO Sep 23 '15 at 22:26
  • Can I somehow query the Reference in Android again so I wont have 100 Threads after 100 App fails? – Ilja KO Sep 23 '15 at 22:26

1 Answers1

1

That is the case of thread-leak. If you keep on doing that (i.e. starting a thread and forgetting about it) very soon you will get java.lang.OutOfMemoryError: Unable to create new native thread exception.

You should always use a controlled threadpool (like a ExecutorService) and shut it down in the finally clause or register a shutdown hook using Runtime.getRuntime().addShutdownHook to close the threadpool/thread

Amm Sokun
  • 1,298
  • 4
  • 20
  • 35