Is It Legal to Call the Start Method Twice on the Same Thread

Is it legal to call the start method twice on the same Thread?

From the Java API Specification for the Thread.start method:

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

Furthermore:

Throws:

IllegalThreadStateException - if the thread was already started.

So yes, a Thread can only be started once.

If so than what do I do if I want to
run the thread again?

If a Thread needs to be run more than once, then one should make an new instance of the Thread and call start on it.

Called start() method twice on same thread in java, sometimes it executes run() method twice; What exactly happens internally?

You are calling run() method from Constructor, that's first execution when you create object. Second one is when you actually start a thread execution and exception is for second start call.

Thread start document clearly says "It is never legal to start a thread more than once".

public void start()
Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.
The result is that two threads are running concurrently: the current thread (which returns from the call to the start method) and
the other thread (which executes its run method).

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

Throws:
IllegalThreadStateException - if the thread was already started.

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

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.

Java - Can java thread invoke start more than once?

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 object be invoked twice

No you cant do that. From Thread.start() API: It is never legal to start a thread more than once.

Can I start a thread again after it has died?

No, you can't. And the Javadoc for the Thread.start() method tells you that!

Can you call the same method on different threads?

There are primarily 2 things you should know.

  1. If 2 threads call the same method, each thread will have a different Stack Frame for the method. So, method local variables are thread safe. The changes made in local variables of one method will not interfere with other thread's changes.

  2. You should (usually) worry about thread-safety / interference when you have a shared resource being modified by both threads.

PS : Your doSum() does very little processing. A smart JVM might actually inline the method.

What is the difference between Thread.start() and Thread.run()?

No, you can't. Calling run will execute run() method in the same thread, without starting new thread.



Related Topics



Leave a reply



Submit