简体   繁体   English

有关Java中线程的一些问题

[英]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? 好的,所以对象thread的生命周期取决于方法buttonClicked ,因此,如果方法返回,即使thread.start()仍在运行,对象thread也会被销毁?

  2. Assume buttonClicked() is invoked when we click a button. 假设单击按钮时调用buttonClicked() 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? 所以我多次单击按钮,每次单击都会创建一个新的Object thread ,在这种情况下,JVM中存在许多不同的“ NewThread”对象吗? 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!! 我确实喜欢这样,然后JVM崩溃了,在我的PC上,我的Java程序似乎失去了应答,我必须使用任务管理器杀死java.exe,但有时还可以,很奇怪!

  3. In buttonClicked() method ,before invoke thread.abc(); buttonClicked()方法中,在调用thread.abc();之前thread.abc(); if thread.start() is already finished,can i still invoke the thread object thread.abc(); 如果thread.start()已经完成,我仍然可以调用线程对象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? 好的,因此对象“线程”的生命周期取决于方法buttonClicked ,因此,如果该方法返回,那么即使thread.start()仍在运行,对象线程也将被销毁?

I see two missunderstandings here: 我在这里看到两个误解:

  • If the method buttonClicked returns, the reference to the object will be gone. 如果方法buttonClicked返回,则对该对象引用将消失。 The object instance of NewThread will be still alive until no one references it and additionally a garbage collector decides to reclaim that memory. NewThread的对象实例将一直存在,直到没有人引用它为止,此外,垃圾回收器决定回收该内存。 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. 这意味着,只要NewThreadrun()方法正在运行, NewThread实例就将在那里,因为NewThread实例描述的线程堆栈引用了该实例。 That stack will be cleared when run() terminates. run()终止时,将清除该堆栈。

  • thread.start() will return immediately. thread.start()将立即返回。 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? 假设单击按钮时将调用buttonClicked(),所以我多次单击按钮,每次单击都会创建一个新的Object线程,因此在这种情况下,存在许多不同的“ NewThread”对象JVM?

In fact you are accumulating NewThread instances if the run() method of NewThread is slow compared to your click rate. 事实上,你正在积累NewThread如果实例run()方法NewThread慢相比,你的点击率。

I do like this, then JVM crash. 我喜欢这样,然后JVM崩溃。

You mean you like clicking buttons and the following JVM crash? 您的意思是您喜欢单击按钮,并且以下JVM崩溃? ;-) ;-)

Edit: After the question has been changed/extended, a third point appeared: 编辑:问题已更改/扩展后,出现第三点:

In buttonClicked() method, before invoke thread.abc(); buttonClicked()方法中,调用thread.abc();之前thread.abc(); if thread.start() is already finished, can i still invoke the thread object thread.abc(); 如果thread.start()已经完成,我是否仍可以调用线程对象thread.abc(); ?

Of course you can invoke any method on an object as long as you have a reference. 当然,只要有引用,就可以在对象上调用任何方法。 Thread is no exception here. Thread在这里也不例外。

I think the main difficulty you have is this: The reference thread points to an object. 我认为您遇到的主要困难是:引用thread指向一个对象。 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. 该对象不是实际线程(请注意小t ),它只是一些小内存区域,其中包含有关实际线程的一些描述。 The real thread consists of some CPU settings, the OS scheduler stuff, a stack and so on. 实际线程由一些CPU设置,OS调度程序素材,堆栈等组成。

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() ). 这两个东西的寿命,在对象和真实线程,不严格耦合:对象Thread创建真正的线程之前存在的(这里面发生Thread.start() The real thread dies after leaving the run() method. 实际线程在离开run()方法后死亡。 But the object Thread exists even after that up to the point when normal garbage collection kicks in as already described. 但是对象 Thread甚至在那之后一直存在,直到正常的垃圾回收开始时为止(如上所述)。

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. GC通过使用垃圾收集器根目录集作为参考点来确定对象是否可访问。 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. JVM可以自由地延迟收集,直到立即需要该对象消耗的内存为止。 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 GC根是一类特殊的变量,包括

Temporary variables on the stack (of any thread) Static variables (from any class) Special references from JNI native code (任何线程的)堆栈上的临时变量(任何类的静态变量)JNI本机代码的特殊引用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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