为什么不能重新启动Java Thread对象? [英] Why can't a Java Thread object be restarted?

查看:161
本文介绍了为什么不能重新启动Java Thread对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道无法重新启动已使用的Java Thread对象,但我找不到解释为什么这是不允许的;即使保证线程已经完成(参见下面的示例代码)。

I know that it is not possible to restart a used Java Thread object, but I don't find an explanation why this is not allowed; even if it is guaranteed that the thread has finished (see example code below).

我不明白为什么 start()(或至少一个 restart())方法不应该以某种方式将Thread对象的内部状态(无论它们是什么)重置为相同的值他们在新创建Thread对象时有。

I don't see why start() (or at least a restart()) method should not be able to somehow reset the internal states - whatever they are - of a Thread object to the same values they have when the Thread object is freshly created.

示例代码:

class ThreadExample {

  public static void main(String[] args){

    Thread myThread = new Thread(){
      public void run() {
        for(int i=0; i<3; i++) {
          try{ sleep(100); }catch(InterruptedException ie){}
          System.out.print(i+", ");
        }
        System.out.println("done.");
      }
    };

    myThread.start();

    try{ Thread.sleep(500); }catch(InterruptedException ie){}
    System.out.println("Now myThread.run() should be done.");

    myThread.start(); // <-- causes java.lang.IllegalThreadStateException

  } // main

} // class


推荐答案


我知道
重启旧的Java Thread对象是不可能的,但
我没有找到解释为什么不允许这个
;即使它是
保证线程已经完成
(参见下面的示例代码)。

I know that it is not possible to restart a used Java Thread object, but I don't find an explanation why this is not allowed; even if it is guaranteed that the thread has finished (see example code below).

我的猜测是什么线程可能直接绑定(为了效率或其他约束)实际本机资源,这些资源可能在某些操作系统中可重新启动,但在其他操作系统中不可重新启动。如果Java语言的设计者允许重新启动Threads,他们可能会限制JVM可以运行的操作系统的数量。

My guestimation is that Threads might be directly tied (for efficiency or other constrains) to actual native resources that might be re-startable in some operating systems, but not in others. If the designers of the Java language had allowed Threads to be re-started, they might limit the number of operating systems on which the JVM can run.

想想看它,我想不出一个允许线程或进程一旦完成或终止就重新启动的操作系统。当一个过程完成时,它就会死掉。你想要另一个,你重新启动它。你永远不会复活它。

Come to think of it, I cannot think of a OS that allows a thread or process to be restarted once it is finished or terminated. When a process completes, it dies. You want another one, you restart it. You never resurrect it.

除了底层操作系统强加的效率和限制问题外,还有分析和推理的问题。当事物是不可变的或具有离散的,有限的生命周期时,你可以推理出并发性。就像状态机一样,它们必须具有终端状态。是开始,等待,完成?如果允许Threads复活,那么这样的事情就不容易被推断。

Beyond the issues of efficiency and limitations imposed by the underlying OS, there is the issue of analysis and reasoning. You can reason about concurrency when things are either immutable or have a discrete, finite life-time. Just like state machines, they have to have a terminal state. Is it started, waiting, finished? Things like that cannot be easily reasoned about if you allow Threads to resurrect.

你还必须考虑复活线程的含义。重建它的堆栈,它的状态,是否可以安全地复活?你能复活一个异常结束的线程吗?等等。

You also have to consider the implications of resurrecting a thread. Recreate its stack, its state, is is safe to resurrect? Can you resurrect a thread that ended abnormally? Etc.

太毛茸茸,太复杂了。所有这些都是微不足道的收获。最好将Threads保留为不可复原的资源。

Too hairy, too complex. All that for insignificant gains. Better to keep Threads as non-resurrectable resources.

这篇关于为什么不能重新启动Java Thread对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆