Start a thread in Java

problem

You wish to create a multithreated Java application and you want to know how to start with concurrent programming. In this post we will see the two classic ways of starting an additional thread from your program.

solution

Java has a Thread class that we can simply extend and add our piece of code that will run in its own Thread. It can start executing it on demand. For example:

public class MyThread extends Thread {

    @Override
    public void run() {
        System.out.println("MyThread is running...");
    }
    
}

Simply extend the Thread class and override its run method.

In order for this thread to run we first need to initialize it and afterwards to start it. For example:

 public static void main(String[] args) {
        MyThread myThread = new MyThread();
        myThread.start();
 }

In the main method we instantiate a MyThread object and then we invoke start() and it starts running. In fact, first it’s being scheduled and then it is run. The start method will invoke the run method at some point; we never invoke run method direcctly as it will not be treated as thread code but instead as normal method invocation.

Putting it all together:

public class MyThread extends Thread {

    @Override
    public void run() {
        System.out.println("MyThread is running...");
    }

    public static void main(String[] args) {
        MyThread myThread = new MyThread();
        myThread.start();
    }
}
Running output
MyThread is running...

Option 2

The other option to start a Thread is by creating a class that is implementing the Runnable interface. For example:

public class MyThread implements Runnable {
    @Override
    public void run() {
        System.out.println("MyThread is running...");
    }
}

Similar to the previous approach, we still override the run method and place the code that we want to be executed. Now if we want to start it, we will need to instantiate a Thread object and pass an instance of MyThread as a parameter in the constructor. For example:

MyThread myThread = new MyThread();
Thread t1 = new Thread(myThread);
t1.start();

The only difference here is that we need an instance of Thread so we can invoke its start method.

Putting it all together, will be like:

public class MyThread implements Runnable {

    @Override
    public void run() {
        System.out.println("MyThread is running...");
    }

    public static void main(String[] args) {
        MyThread myThread = new MyThread();
        Thread t1 = new Thread(myThread);
        t1.start();
    }
}

When we invoke the start method on t1, behind the scenes it’s also scheduled and then it’s using the implemented Runnable (myThread) and invoking its run() method.

Running output
MyThread is running...

Slightly different approach on the invocation part but with big differences if we compare them. One of the positives the second approach has it is that you can still extend a class but with the other option since you’re extending the Thread class, no more classes can be extended as it impossible to extend multiple classes in Java.

Conclusion

We simply saw the two ways of starting a new thread in Java. One can use this as a starter point to build multi-threaded applications.


0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x