Java provides built-in support for multithreaded programming. A multi-threaded program contains two or more parts that can run concurrently. Each part of such a program is called a thread, and each thread defines a separate path of execution.
When a Java program starts up, one thread begins running immediately. This is usually called the main thread of our program, because it is the one that is executed when our program begins.
- It is the thread from which other “child” threads will be spawned.
- Often, it must be the last thread to finish execution because it performs various shutdown actions
Flow diagram :
How to control Main thread
The main thread is created automatically when our program is started. To control it we must obtain a reference to it. This can be done by calling the method currentThread( ) which is present in Thread class. This method returns a reference to the thread on which it is called. The default priority of Main thread is 5 and for all remaining user threads priority will be inherited from parent to child.
Current thread: main After name change: Geeks Main thread priority: 5 Main thread new priority: 10 Main thread Main thread Main thread Main thread Main thread Child thread priority: 10 Child thread new priority: 1 Child thread Child thread Child thread Child thread Child thread
Relation between the main() method and main thread in Java
For each program, a Main thread is created by JVM(Java Virtual Machine). The “Main” thread first verifies the existence of the main() method, and then it initializes the class. Note that from JDK 6, main() method is mandatory in a standalone java application.
Deadlocking with use of Main Thread(only single thread)
We can create a deadlock by just using Main thread, i.e. by just using a single thread. The following java program demonstrate this.
Entering into Deadlock
The statement “Thread.currentThread().join()”, will tell Main thread to wait for this thread(i.e. wait for itself) to die. Thus Main thread wait for itself to die, which is nothing but a deadlock.
Related Article: Daemon Threads in Java.
This article is contributed by Gaurav Miglani. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Naming a thread and fetching name of current thread in Java
- Difference between Thread.start() and Thread.run() in Java
- How a thread can interrupt an another thread in Java?
- Is main method compulsory in Java?
- Valid variants of main() in Java
- GFact 48 | Overloading main() in Java
- How to overload and override main method in Java
- Static Block and main() method in Java
- Replacing 'public' with 'private' in "main" in Java
- What are the main differences between the Java platform and other platforms?
- Java.lang.Thread class in Java
- Execute main() multiple times without using any other function or condition or recursion in Java
- Understanding public static void main(String args) in Java
- Thread Pools in Java
- Daemon thread in Java