Skip to main content

Threads in Java - Main Thread (Part 3)

We have discussed that to call a thread we need to create an instance of this thread and call the start() method. But this raises the question, who does this for the very first thread in the Java application? Which thread calls the start() method of the very first thread?

This is apparently the chicken-egg problem 😏 but if we go a little deeper, we will find that a thread object is a mere representation of a thread in the program, it does not have information about call stacks and program counter. It only serves as a storage for a pointer to lower level mechanisms that implement threads. Thus, JVM is capable of creating threads without using the Thread class and the thread object can be created later if needed.

How does JVM create "main" thread?

An instance of java.lang.Thread is not a thread; it can be used to represent a thread of execution in the JVM but the JVM is perfectly capable of creating threads without using the Thread class at all.

This is what happens with the main thread: the JVM creates it, and an instance of java.lang.Thread is created to represent it later.

Following is based on the code present in the Hotspot JVM. 

The startup of the JVM calls the static Threads::create_vm function, which is already running in a thread set up by the operating system. Within that function we find:
Let's discuss this code -

The startup of the JVM calls the static Threads::create_vm function, which is already running in a thread set up by the operating system. Within this function, we find main_thread of C++ class type JavaThread is created and is set with the initial state.

The JavaThread class is apparently used for bookkeeping; it associates an OS or VM thread with a Java Thread object. The Java object apparently doesn't exist yet. The code then goes on to initialize various other things. In other words, it initializes the System, ThreadGroup, and Thread classes then create an instance of Thread referenced by thread_object and set the Thread instance for the main JavaThread.

If you wonder what the create_initial_thread does, apparently it allocates the Thread instance, stores a pointer to the JavaThread (C++) object in the private eetop field of the Thread instance, sets the thread priority field to normal, calls the Thread(ThreadGroup group, String name) constructor, and returns the instance:

Now, this is what Hotspot VM does. Other implementations probably do something like this (source).

Main Thread

When a Java program starts up, the main thread begins running immediately and it is the thread from which other children threads are spawned.

How to control the main thread?

To do this, we must take the reference of the main thread using the currentThread() method. This method returns the reference of the thread on which it is called. The default priority of the main thread is 5 and for all remaining threads, the priority is inherited from the parent to the child.
Let's see the below code to understand this

Deadlock using main thread (single thread)

If we call join() method from the main method on the current thread then it will wait for itself and will cause deadlock. Below code represents how - 


In this post, we discussed the thread life cycle, it's various states and we also saw these concepts in action by writing code. The code can be found in the commit on my GitHub.

Feel free to befriend me on FacebookTwitter or Linked In or say Hi by email.

Happy Coding 😊 and Happy Learning 😊


  1. Woori Casino No Deposit Bonus 2021 | Free Play in Demo
    Woori Casino offers a variety of free spins and no deposit bonuses, as well as regular promotions. As you can't claim this offer without 좜μž₯λ§ˆμ‚¬μ§€ being registered


Post a Comment

Popular posts from this blog

Parsing XML using Retrofit

Developing our own type-safe HTTP library to interface with a REST API can be a real pain as we have to handle many aspects - making connections caching retrying failed requests threading response parsing error handling, and more.  Retrofit, on the other hand, is a well-planned, documented and tested library that will save you a lot of precious time and headaches. In this tutorial, we are going to discuss how we can parse the XML response returned from  using the Retrofit library. To work with Retrofit, we need three classes -  Model class to map the JSON data Interfaces which defines the possible HTTP operations Retrofit.Builder class - Instance which uses the interface and the Builder API which allows defining the URL endpoint for the HTTP operation. Every method of the above interface represents on possible API call. The request type is specified by using appropriate annotations (GET, POST). The respon

Threads in Java - CountDownLatch (Part 12)

A CountDownLatch is a synchronizer which allows one thread to wait for one or more threads before starts processing. A good application of  CountDownLatch is in Java server-side applications where a thread cannot start execution before all the required services are started. Working A  CountDownLatch is initialized with a given count which is the number of threads it should wait for. This count is decremented by calling countDown() method by the threads once they are finished execution. As soon as the count reaches to zero, the waiting task starts running. Code Example Let us say we require three services, LoginService, DatabaseService and CloudService to be started and ready before the application can start handling requests. Output Cloud Service is up! Login Service is up! Database Service is up! All services are up. Now the waiting thread can start execution. Here, we can see that the main thread is waiting for all the three services to start before starting its own

Threads in Java - Masterclass (Part 0)

Threads in Java Multithreading is a way to introduce concurrency in a program. In any case, if there are parallel paths in our program (parts which do not depend on the result from another part), we can make use of multithreading. One should exploit this feature, especially with all these multiple core machines nowadays. Below are a few reasons why we should use multithreading - 1. Keep a process responsive There was once a time when you would print a document in MS Word and the application would freeze for an annoyingly long amount of time until the job finished. Eventually, Microsoft solved this problem by running a printing job parallel to the main thread/ GUI thread.  To be clear though, not only GUI apps but Network services have to keep an ear to the ground for new clients, dropped connections and cancellation requests. In either case, it is critical to do the heavy lifting on a secondary thread to keep the user satisfied. 2. Keep a processor busy Keeping a proc