Why a thread in java cannot be started once it completes its execution? Is it related to any design pattern?
Asked
Active
Viewed 54 times
0
-
I don't understand the question. Why do you want it to start twice? – Buhake Sindi Dec 24 '14 at 10:45
-
I am asking why java restricted an instance of thread can invoke start() method once? – amitguptageek Dec 24 '14 at 10:46
-
Duplicate: http://stackoverflow.com/questions/2663426/why-cant-we-call-start-method-twice-on-a-same-instance-of-the-thread-object – BackSlash Dec 24 '14 at 10:47
-
@amitguptageek read `Java` specification please. You can't do by design. – Ruchira Gayan Ranaweera Dec 24 '14 at 10:47
-
@ Ruchira I am asking why? – amitguptageek Dec 24 '14 at 10:48
-
@amitguptageek Not a technical explaination, but: Think it as a race. Every thread is a car, that can start the race and complete it. Can a car start two times the same race? – BackSlash Dec 24 '14 at 10:49
-
@amitguptageek It has design such a way. In real life, can you have birth twice in your life?. Thread are same as that. http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#start%28%29 – Ruchira Gayan Ranaweera Dec 24 '14 at 10:50
-
@Ruchira,@Backslash :- After looking into it I found that to maintain the state of any thread this step was taken by API developers. – amitguptageek Dec 24 '14 at 10:57
-
@Back no not a race, but the fact remains that all popular underlying native APIs (posix, win32) do it, so it makes no sense for a low level abstraction to abstract that much away. That said one shouldn't use raw threads anyhow - it's rarely what's best. – Voo Dec 24 '14 at 11:35
-
Since this detail is trivially worked-around, eg. with a while(true) loop with a wait at the top, it's a non-issue. – Martin James Dec 24 '14 at 11:49
-
Placing restrictions on the public API allows internal design flexibility. For instance, Thread will [free the given Runnable when it exits](https://bugs.openjdk.java.net/browse/JDK-4006245). If you could restart it there would be nothing to run the next time. – jmehrens Dec 24 '14 at 20:30
-
Each instance `t` of the (big T) `Thread` class manages the life cycle of one (little t) thread. A (little t) thread comes into existence when the program calls `t.start()`, and it ceases to exist when the `run()` method completes. A big T `Thread` object can only be used one time because a little t thread can only exist one time. – Solomon Slow Dec 26 '14 at 22:46