简体   繁体   English

关于java线程生命周期

[英]about java thread lifetime

So I have a "tricky" question, I want to see people opinions.所以我有一个“棘手”的问题,我想看看人们的意见。

I'm programming a component, which extends a JPanel and do some custom stuff.我正在编写一个组件,它扩展了一个 JPanel 并做一些自定义的事情。 Inside that component I have a Thread, which loops forever like this:在该组件内,我有一个线程,它永远循环如下:

//chat thread
    Thread chat_thread = new Thread(new Runnable(){
        public void run(){
            while(true){
                //get chat updates
            }
        }
    });
    chat_thread.start();

So the question is, when the component is removed from its parent by the remove() method, is this thread still alive, or does it die when you remove the component?那么问题来了,当组件通过remove()方法从其父组件中移除时,这个线程是否还活着,或者当你移除组件时它会死吗?

EDIT:编辑:
Thanks all for your replies, indeed the thread does not terminate removing its starter, so in order to terminate this thread from another component, I did the following:感谢大家的回复,确实该线程并没有终止删除它的启动器,所以为了从另一个组件终止这个线程,我做了以下事情:

Set<Thread> t = Thread.getAllStackTraces().keySet();
    Iterator it = t.iterator();
    while(it.hasNext()){
        Thread t2 = (Thread)it.next();
        if(t2.getName().equals("chat_thread")){
            t2.interrupt();
        }
    }

by first creating a name for my thread with the Thread.setName() method.首先使用 Thread.setName() 方法为我的线程创建一个名称。 Thanks!谢谢!

This thread will never die (unless it crashes or terminates itself).这个线程永远不会死(除非它崩溃或终止自己)。

Does not matter what happens to the thread that started it, or the component that contains it.启动它的线程或包含它的组件发生了什么无关紧要。

If you don't want to hang the JVM, you can declare it a daemon thread.如果不想挂起JVM,可以将其声明为守护线程。 Then it will shut down when everything else does.然后它会在其他一切正常时关闭。 For more control, you need to make sure you terminate the thread in your own code at the appropriate time.为了获得更多控制,您需要确保在适当的时间终止您自己代码中的线程。

It will still be alive.它仍然会活着。 A running thread constitutes a root for the GC.一个正在运行的线程构成了 GC 的根。 Averything reachable from a chain of references starting from a root is not eligible to GC.从根开始的引用链可访问的所有内容都不符合 GC 的条件。 This means, BTW, that your panel won't be GCed either, since the thread holds an implicit reference to the panel ( EnclosingPanel.this ).这意味着,顺便说一句,您的面板也不会被 GC,因为该线程持有对面板的隐式引用( EnclosingPanel.this )。

You need to make sure to stop the thread when you don't want it running anymore.当您不想再运行该线程时,您需要确保停止该线程。

线程在完成其工作或您interrupt它之前不会死亡。

线程在退出 run() 方法时将终止,因此当组件通过 remove() 方法从其父组件中删除时,线程将永远不会终止。

Take a look at Component.removeNotify.看看 Component.removeNotify。 In your component, keep a pointer to the thread:在您的组件中,保留一个指向线程的指针:

public class MyPanel extends JPanel {
   private Thread thread = ...;

   public void removeNotify() {
      super.removeNotify();
      thread.interrupt();
   }
}

This method is called by awt itself when it decides to remove the native peer of the component (ie, the windows or linux component backing the awt component).当 awt 决定删除组件的本机对等方(即支持 awt 组件的 windows 或 linux 组件)时,该方法会被 awt 自身调用。 As per the javadoc:根据javadoc:

Makes this Component undisplayable by destroying it native screen resource.通过破坏本机屏幕资源使该组件无法显示。 This method is called by the toolkit internally and should not be called directly by programs.该方法由工具包内部调用,不应由程序直接调用。 Code overriding this method should call super.removeNotify as the first line of the overriding method.覆盖此方法的代码应调用 super.removeNotify 作为覆盖方法的第一行。

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

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