Java - Can java thread invoke start more than once?
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 = 开发者_StackOverflow社区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 !
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.
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 :)
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.
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();
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.
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.
精彩评论