3

Folks,

I know this question has been asked before here, though indirectly. But it didn't answer my doubt.
Question : Is it legal to call the start method twice on the same Thread?

From the spec,

It is never legal to start a thread more than once. In particular, a thread may not be restarted once it has completed execution.

I agree. But my code doesn't throw a IllegalThreadStateException which it is expected to throw on execution of following program.

   public class Tester extends Thread {
        public void run() {
            System.out.print("run");
        }
        public static void main(String[] args) {
            Tester thread = new Tester();
            new Thread(thread).start();
            new Thread(thread).start();
        }
    }    

Q.1) I ran the above code in Eclipse. Here, since I am trying to start a new thread on the same instance, a IllegalThreadStateException is expected to be thrown. But it doesn't.

Why ?

Q.2) If at all we did start a new thread on the same instance, what harm it would do ?

Any help would be greatly appreciated !

Community
  • 1
  • 1
Saurabh Gokhale
  • 53,625
  • 36
  • 139
  • 164
  • 3
    I don't think it's (only) a matter of what harm it would do. I think it's more of a matter of it making no sense. What does it mean to start a thread that is already running? Can you start a car that is already running? That idea just makes no sense. – R. Martinho Fernandes Mar 31 '11 at 14:19

6 Answers6

9

You are NOT calling start() on the same instance. Everytime you use new you are creating a distinct instance. Hence no problem calling start().

If you did this:

 Thread t = new Thread(thread);
 t.start();
 t.start();

Then you may have a problem.

Vincent Ramdhanie
  • 102,349
  • 23
  • 137
  • 192
6

Firstly, you are invoking on two different thread objects ie:

 new Thread(thread).start();
 new Thread(thread).start();

you are calling start method on two different instances. for which reason you are not getting the exception.

try with following to get the exception

thread.start();
thread.start();

For your second question. you can get the answer here : Why can't we call the start method twice on a same instance of the Thread object?

which is fortunately asked by me :)

Community
  • 1
  • 1
GuruKulki
  • 25,776
  • 50
  • 140
  • 201
1

As most of all answers covered Q1, Here i would like to concentrate on Q2 which is " If at all we did start a new thread on the same instance, what harm it would do ?"

first point to consider is when do you want want to call the thread start method second time,is it when the first thread is executing(case:1) or else after the execution of first thread is done(case 2)

case1 : To identify the thread which is started and executing the only way we have is thread object which is used for creation, so IF there was a chance to call the start() method second time there would be for suppose other thread getting created and it executes, but if we want to change/manipulate a particular thread of the multiple threads which are executed on a particular instance how would we identify them individually,so it would be totally impossible and so to identify a running thread uniquely and work on it java didn't allow to call start() method multiple times .

case 2:why didn't java allow us to call start() method multiple times if already running thread is finished? in java once the scope of the object is ended it need to be garbage collected,so in case of thread objects also this happen,but if there is a facility to call start method multiple times the java env should not allow GC to takes place on thread object thinking that there may be a second time to use this thread object and this thread object will forever be in heap(or some place) without getting GC.

so considering above two reasons they might have made restrictions on calling start() method on a thread object only once.

k.explorer
  • 291
  • 6
  • 19
1

Can java thread invoke start more than once ?

You can involve start() as often as you like. However you will get an IllegalThreadStateException if you call it more than once on the same Thread.

Q.1) I ran the above code in Eclipse. Here, since I am trying to start a new thread on the same instance, a IllegalThreadStateException is expected to be thrown. But it doesn't.

Thats because you created three different threads. One is the Tester and two wrap the Tester.

Q.2) If at all we did start a new thread on the same instance, what harm it would do ?

Other than create confusion, none. You shouldn't do this. Instead the Tester should implement Runnable.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

Q.1) I ran the above code in Eclipse. Here, since I am trying to start a new thread on the same instance, a IllegalThreadStateException is expected to be thrown. But it doesn't.

You are not caling start() on same instance.

new Thread(thread).start();

the above statement is same as

new Thread((Runnable)thread).start();
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
Prince John Wesley
  • 62,492
  • 12
  • 87
  • 94
0

And here we see why the Executors make so much sense.

Typical idiomatic Java would say that you shouldn't run your own threads much at all; create an executor service, and let it manage the threads. You just create Runnable instances and pass those to the executor service; you can call Runnable's run() method as often as you like, whenever and wherever it makes sense to do so, and you don't have to concern yourself with Thread management at that point.

Extending Thread is also a one-shot deal; extending superclasses in Java is expensive (since you get ONE superclass, that's it). However, you can extend as many interfaces as you like, so Runnable gives you a more powerful object hierarchy as well.

Joseph Ottinger
  • 4,911
  • 1
  • 22
  • 23
  • What this answer has relation to my question ? – Saurabh Gokhale Mar 31 '11 at 14:13
  • 1
    Well, IMO, the answer to your question was pretty obvious, on the surface of it: no, it's not legal to call start() twice on the same instance of Thread. The first question used an invalid supposition (you weren't calling start() twice on the same instance of Thread, you were creating two separate Thread instances). The second question has no real meaning, since the definition of Thread means that you can't start it twice; you'll get an illegal state. So what **I** did was to try to think of possible tasks you were trying to achieve. Using runnables means you can **seem** to run it twice. – Joseph Ottinger Mar 31 '11 at 15:07