简体   繁体   English

为什么我们不能在 Thread 对象的同一个实例上调用 start 方法两次?

[英]Why can't we call the start method twice on a same instance of the Thread object?

I was reading about threads and found that we can't call the start method twice on the same thread instance.我正在阅读有关线程的内容,发现我们不能在同一个线程实例上两次调用 start 方法。 But I didn't understand the exact reason for the same.但我不明白相同的确切原因。 So why can't we call it twice or even more times?那么为什么我们不能调用它两次甚至更多次呢?

In my opinion the Thread object is your "handle" for the actual running context.在我看来,Thread 对象是您实际运行上下文的“句柄”。 If you allow creating many concurrent executions associated with the same java.lang.Thread, what would you expect getStackTrace() and getState() methods to return?如果您允许创建与同一个 java.lang.Thread 关联的多个并发执行,您希望 getStackTrace() 和 getState() 方法返回什么?

I suppose that Thread class could have been designed to allow spawning multiple running contexts, but its API would be less simple and clean.我想 Thread 类可以被设计为允许产生多个运行上下文,但它的 API 会不那么简单和干净。

You want 1 instance for 1 thread, as that thread has internal state it will manage.您需要 1 个线程的 1 个实例,因为该线程具有它将管理的内部状态。

Consider threads as a kind of resource.将线程视为一种资源。 It usually does not make sense to have 1 instance refer to several resources - (just as you can't have a java File object refer to more than 1 file).让 1 个实例引用多个资源通常没有意义 - (就像您不能让 java File 对象引用多个文件一样)。

It would also get you in all sorts of trouble if you started a thread twice, and you either inherited from Thread and made some instance variables that now more than 1 thread accesses, - same thing if you create the thread from a Runnable .如果您启动一个线程两次,它也会给您带来各种麻烦,并且您要么从Thread继承并创建了一些现在有 1 个以上线程访问的实例变量 - 如果您从Runnable创建线程也是如此。 Atleast the API doesn't make it a no-brainer to do that screw-up.至少 API 并没有让搞砸的事情变得轻而易举。

Take a look at the states a thread can be in , here http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Thread.State.html看看一个线程可以处于的状态,这里是http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Thread.State.html

Basically, the only time you can start a thread is when it is in the NEW state.基本上,您可以启动线程的唯一时间是它处于NEW状态时。 And none of the other states can make it transition back to NEW其他任何状态都无法使其转换回NEW

A Thread is not the same thing as a thread. Thread与线程不同。

A (little-t) thread is an independent execution of your code. (little-t) 线程是代码的独立执行。 A (Big-T) Thread is a Java object that can be used to start, and manage the life cycle of a little-t thread. A (Big-T) Thread是一个 Java 对象,可用于启动和管理 little-t 线程的生命周期。

Suppose you were hired to write code for an insurance company, and you defined a (Big-A) Accident class to represent a (little-a) accident.假设您受雇为一家保险公司编写代码,并且您定义了一个 (Big-A) Accident类来表示一个 (little-a) 事故。 Somebody asks you, "Why can't I re-use an Accident instance?"有人问你,“为什么我不能重用一个Accident实例?”

Well, an accident can only happen once, right?好吧,事故只能发生一次,对吧? Even if the exact same thing happens to the exact same drivers and cars in the exact same way on a different day, it's still a different accident, right?即使完全相同的司机和汽车在不同的日子以完全相同的方式发生完全相同的事情,这仍然是不同的事故,对吧?

according to thread life cycle, once thread is 'dead' you can not restart it.You only can start new thread invoking start() method.根据线程生命周期,一旦线程“死”,您将无法重新启动它。您只能通过调用 start() 方法启动新线程。

Thread can be bought to Running state from Runnable state not from Dead state.线程可以从Runnable状态而不是Dead状态购买到Running状态。

This is my opinion, It is due to Thread id.这是我的意见,这是由于线程 id。 Thread scheduler is identifying the thread through the thread id.线程调度程序通过线程 id 识别线程。 It is unique real number.它是唯一的实数。 Please find below code,请找到下面的代码,

    public class StartTwice extends Thread {

    public void run() {
        System.out.println("running...");
    }

    public static void main(String[] args) {

        StartTwice start1 = new StartTwice();
        System.out.println("Thread id: " + start1.getId());
        start1.start();

        start1 = new StartTwice();
        System.out.println("Thread id: " + start1.getId());
        start1.start();
    }

}

Output is:

Thread id: 10
Thread id: 11
running...
running...

When I re-instantiate the start1 object.当我重新实例化 start1 对象时。 A new thread id is created.创建一个新的线程 id。

PS: Even a thread is done, we can't use the same object (thread id) second time. PS:即使一个线程完成了,我们也不能第二次使用同一个对象(线程id)。 Until or unless the JVM is reuse that id.直到或除非 JVM 重用该 ID。

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

相关问题 在同一个线程上两次调用 start 方法是否合法? - Is it legal to call the start method twice on the same Thread? 为什么我们不能在方法之外使用类的对象来调用类的方法? - Why can't we call the method of the class using the class's object outside a method? 为什么我们可以调用来自 Java 中另一个线程的对象的方法? - WHY can we invoke a method of an object that is FROM another thread in Java? 为什么我们不能在启动线程后调用 setDaemon(true) ? - why we can't call setDaemon(true) after starting thread? 为什么我们调用Thread.start()方法反过来调用run方法? - Why we call Thread.start() method which in turns calls run method? 为什么我们不能在扩展类的静态方法中使用此实例? - Why can't we use this instance in the static method of extended class? 为什么我们不能在PrintStream类的帮助下调用'println()'方法,其中out是这个类的对象? - Why we can't call 'println()' method with help of PrintStream class where out is object of this class? 为什么我们可以使用 'this' 作为实例方法参数? - Why can we use 'this' as an instance method parameter? Thread.start()不调用Thread.run()方法 - Thread.start() doesn't call Thread.run() method 为什么不能在Thread实例中使用notifyAll()? - Why can't notifyAll() be used in a Thread instance?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM