简体   繁体   中英

some issue about thread in java

A thread class:

class NewThread extends thread{
    public void abc(){
    }
}

then in another thread's a method perform these code

public void buttonClicked(){
   NewThread  thread = new NewThread();
   thread.start();
   thread.abc();
}
  1. OK,so the object thread 's life cycle is up to the method buttonClicked , so if the method return, object thread will be destroy even if thread.start() is still running?

  2. Assume buttonClicked() is invoked when we click a button. So I click the button several times, every time I click it, a new Object thread will be created,in this case, there are many different "NewThread" Object exist in JVM? I do like this, then JVM crash,In my PC,it appears that my java program lose answering,I must use task manager to kill java.exe,but sometimes it's ok,weird!!

  3. In buttonClicked() method ,before invoke thread.abc(); if thread.start() is already finished,can i still invoke the thread object thread.abc(); ?

OK, so the object "thread"'s life cycle is up to the method buttonClicked , so if the method return, object thread will be destroy even if thread.start() is still running?

I see two missunderstandings here:

  • If the method buttonClicked returns, the reference to the object will be gone. The object instance of NewThread will be still alive until no one references it and additionally a garbage collector decides to reclaim that memory. This means, that the instance of NewThread will be there at least as long as the run() method of NewThread is running because the stack of the thread which is described by the NewThread instance references that instance. That stack will be cleared when run() terminates.

  • thread.start() will return immediately. It only signals the second thread to start working.

Assume buttonClicked() will be invoke when we click a button, so I click the button several times, every time I click it, a new Object thread will be created, so in this case, there are many different "NewThread" Object exist in JVM?

In fact you are accumulating NewThread instances if the run() method of NewThread is slow compared to your click rate.

I do like this, then JVM crash.

You mean you like clicking buttons and the following JVM crash? ;-)

Edit: After the question has been changed/extended, a third point appeared:

In buttonClicked() method, before invoke thread.abc(); if thread.start() is already finished, can i still invoke the thread object thread.abc(); ?

Of course you can invoke any method on an object as long as you have a reference. Thread is no exception here.

I think the main difficulty you have is this: The reference thread points to an object. This object is not the real thread (note the small t ), it is only some small memory area with some descriptions about the real thread. The real thread consists of some CPU settings, the OS scheduler stuff, a stack and so on.

The lifetimes of these two things , the object and the real thread, are not coupled strictly: The object Thread exists before the real thread is created (which happens inside Thread.start() ). The real thread dies after leaving the run() method. But the object Thread exists even after that up to the point when normal garbage collection kicks in as already described.

Thread is not destroyed after exiting from method. Reference to thread is saved in "thread table" which is accessible by static link (so thread won't garbage collected).

A Thread is also known to be the garbage root and this it is not cleaned up by the garbage collector. GC decides if an object is reachable or not by using the set of garbage collector root as reference points. This is also the reason why the main thread doesn't get collected by the garbage collector.

An object enters an unreachable state when no more strong references to it exist. When an object is unreachable, it is a candidate for collection. Note the wording: Just because an object is a candidate for collection doesn't mean it will be immediately collected. The JVM is free to delay collection until there is an immediate need for the memory being consumed by the object. It's important to note that not just any strong reference will hold an object in memory. These must be references that chain from a garbage collection root. GC roots are a special class of variable that includes

Temporary variables on the stack (of any thread) Static variables (from any class) Special references from JNI native code

Taken from : http://java.sun.com/docs/books/performance/1st_edition/html/JPAppGC.fm.html#997428

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM