How to start a java thread java interview question and answer

376 views · Published 17 October 2012 · 2:46 · Indexed 1 October 2026

Channel: Interview DOT · 2012 · Science & Technology

Watch on YouTube

Click here - https://www.youtube.com/channel/UCd0U_xlQxdZynq09knDszXA?sub_confirmation=1 to get notifications. http://www.interviewdot.com http://www.interviewdot.com http://www.interviewdot.com

Welcome to www.InterviewDot.com     How to prepare for java interview Questions :   

How to start a java thread ? Java interview questions and answers

Before starting a thread ,thread object has to be created.
Thread t = new Thread(MyRunnable);
//t  refers to thread of execution rather than the thread class.  its said to be in NEW state.

So what happens after you call start() method ?
A new thread of execution starts with a new call stack.
The thread moves from the new state to the runnable state.
When the thread gets a chance to execute ,its target "run()" method will run();

Note :
You always start a "Thread" not a runnable. You call start on thread's instance not on runnable instance.

Public class  InterviewDotThread  {
 Public static void main(String[] args){
 MyRunnable runnable = new MyRunnable ();
 Thread t = new Thread (runnable);
 t.start();
 }
}


For more Java Interview questions and answers  - logon to www.InterviewDot.com - India's Top Interview Guide.

http://www.interviewdot.com http://www.interviewdot.com http://www.interviewdot.com

More from this channel