Java Thread Explained

In this blog post we’ll talk about what is a Java thread and when to use it. Also we’ll go through some examples of how to create and run a thread.

What is Java Thread?

Java thread is a lightweight process. When we start up a program in Java, we have at least one thread in our application- the main thread. It is run directly when we start the application and looks for the main method, the entry point of the application, to execute the code it contains. In addition, main thread has the ability to create other threads if needed.

When we talk about processes, we usually think of them as being programs or applications. Every processes contains one or more threads to execute tasks. There is one very important difference between threads and processes. Process has its own memory space. In contrast, threads share the process’s resources (e.g. memory). This means that threads have access to the same variables and objects. Therefore, we have to be careful when dealing with concurrent applications. Luckily there are different approaches to deal with it. Some of the examples include synchronization and making sure you have immutable objects in your application.

When to Use Threads in Java?

Concurrent applications use threads to execute multiple tasks in parallel. In other words, for multitasking. This helps us to create less blocking code in our applications and is especially useful with user-facing applications or other time-critical parts of the system. You could consider using threads whenever you see a possibility of doing some tasks concurrently or in the background.

How to Create And Run a Thread in Java?

We have two options when it comes to creating a thread in Java:

The Runnable interface contains one run method that should contain code to be executed by a thread. As a matter of fact, Runnable interface itself is actually implemented by the Thread class. Another good thing to know is that Runnable interface should be used when you only intend to override the run method and no other methods from the Thread class.

Here is an example of how to create and run a thread in Java by implementing the Runnable interface:

public class RunnableExample implements Runnable {

    @Override
    public void run() {
        System.out.println("Hello!");
    }

    public static void main(String[] args) {
        // Creating a new thread
        Thread thread = new Thread(new RunnableExample());
        // Starts the thread and calls the run method
        thread.start();
    }
}

Let’s also see how to create a thread by extending the Thread class:

public class ThreadExample extends Thread {

    @Override
    public void run(){
        System.out.println("Hello!");
    }

    public static void main(String[] args) {
        // Creating a new thread
        ThreadExample thread = new ThreadExample();
        // Starts the thread and calls the run method
        thread.start();
    }
}

Conclusion

Java Thread is a lightweight process. Every time we run an application we have at least one thread running – the main thread. This thread can create additional threads if needed to run tasks in parallel from the main thread. We have two options to choose from to create a thread in Java: to implement a Runnable interface or to extend a Thread class.

Be the first to reply

Leave a Reply

Your email address will not be published. Required fields are marked *