Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a thread guaranteed to have started when Thread.start() returns?

I have tried some sample code along the lines of:

Thread thread = new TestThread();
thread.start();
thread.isAlive();

It appears to me that the only way for the isAlive() call to return false, is for the thread to have already finished. Is this true?

The Java 7 JavaDoc for 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.

The Java 7 JavaDoc for isAlive:

Tests if this thread is alive. A thread is alive if it has been started and has not yet died.

Neither of these seem very conclusive to me.

like image 553
Alex N. Avatar asked Aug 05 '13 17:08

Alex N.


People also ask

What happens when thread start () method is called?

If you call start() method on Thread, Java Virtual Machine will call run() method and two threads will run concurrently now - Current Thread and Other Thread or Runnable implementation.

Is it possible to call run () method instead of start () on a thread in Java?

No, you can not directly call run method to start a thread. You need to call start method to create a new thread. If you call run method directly , it won't create a new thread and it will be in same stack as main.

What is difference between starting thread with Run () and start ()?

start method of thread class is implemented as when it is called a new Thread is created and code inside run() method is executed in that new Thread. While if run method is executed directly than no new Thread is created and code inside run() will execute on current Thread and no multi-threading will take place.

What happens when thread start?

start() method 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).


3 Answers

  • The statement someThread.start() will cause that thread to go into runnable state
  • It is not guaranteed that it will start executing its run method immediately after start() is called.
  • Exactly when to execute the thread, is totally dependent on the thread scheduler.
  • When someThread.start() is called, the thread can move from runnable to runing state and even waiting state.

Tests if this thread is alive. A thread is alive if it has been started and has not yet died.

This means, the thread is alive when its start() is called and its run() not completed yet. Completion of run() method means thread is dead.

like image 115
Prasad Kharkar Avatar answered Oct 21 '22 09:10

Prasad Kharkar


Thread has started doesn't mean it is already executing run method but it's status is Alive.

So after start is returned Thread is alive but not guaranteed to be executing run method it's state can be anything except New after completion of start method. (Runnable, Waiting, Terminated, etc)

t.isAlive() returns true if t is not in the NEW or TERMINATED state

Also take a look at this great resource on java threading

like image 3
Tala Avatar answered Oct 21 '22 09:10

Tala


A thread t is alive as soon as t.start() returns and until t.run() completes.

like image 2
assylias Avatar answered Oct 21 '22 10:10

assylias