3

which is the thread that will begin as soon as the execution of a java program begins? This was asked in an interview for me. so can anyone suggest the answer here

3 Answers 3

4

From Thread API document

When a Java Virtual Machine starts up, there is usually a single non-daemon thread (which typically calls the method named main of some designated class).

And this thread will be called as main thread.

Sign up to request clarification or add additional context in comments.

Comments

1

The thread which is created when you start is called the main thread. It is the one which invokes the main method.

Edit: apparently someone beat me to the answer.

Comments

0

The 'main()' method in Java is referred to the thread that is running, whenever a Java program runs. It calls the main thread because it is the first thread that starts running when a program begins. Other threads can be spawned from this main thread. The main thread must be the last thread in the program to end. When the main thread stops, the program stops running.

Main thread is created automatically, but it can be controlled by the program by using a Thread object. The Thread object will hold the reference of the main thread with the help of currentThread() method of the Thread class.

for more details check this link

2 Comments

Please note that the main thread is not required to quit last. The program stops running when the last non-deamon thread stops, which may, but need not to be the main thread.
"Main thread...can be controlled by...using a Thread object [...that] will hold the reference of the main thread with the help of currentThread() method of the Thread class." I think what you are trying to say is that code running in any thread (including the main thread) can get a reference to its own Thread object by calling Thread.currentThread().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.