开发者

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 o开发者_如何学Gon the same thread instance. 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. If you allow creating many concurrent executions associated with the same java.lang.Thread, what would you expect getStackTrace() and getState() methods to return?

I suppose that Thread class could have been designed to allow spawning multiple running contexts, but its API would be less simple and clean.


You want 1 instance for 1 thread, as that thread has internal state it will manage.

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).

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. Atleast the API doesn't make it a no-brainer to do that screw-up.

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

Basically, the only time you can start a thread is when it is in the NEW state. And none of the other states can make it transition back to NEW


A Thread is not the same thing as a thread.

A (little-t) thread is an independent execution of your code. A (Big-T) Thread is a Java object that can be used to start, and manage the life cycle of a little-t thread.

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. Somebody asks you, "Why can't I re-use an Accident instance?"

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.

Thread can be bought to Running state from Runnable state not from Dead state.


This is my opinion, It is due to Thread id. Thread scheduler is identifying the thread through the thread 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. A new thread id is created.

PS: Even a thread is done, we can't use the same object (thread id) second time. Until or unless the JVM is reuse that id.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜