Main Thread in Java

In Java programming, when a program begins the first thread that starts running is called main thread.

 Other threads(child thread) can be spawned from this main thread.

When program terminates, 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.

By default name of main thread is “main”

By default Group name of main thread is “main”

By default priority of main thread is “5”.

Q.1 WAP to print the main thread and change the name of the main thread.

Output:

Q.2 WAP to print the 1 to 5 using thread and illustrate the use of sleep().

Output: