Java programming language logo

Starting a Thread

These days computers have multiple processors or processors with multiple cores. By concurrent programming, they can be better utilized, and the execution time can be reduced.

Every Java program has at least one thread, the main thread. This can start additional threads.

The Runnable Interface

The SimplestThread class can be run as a thread because it implements the Runnable interface, which defines a run method. This method contains the code that is executed in a thread.


package com.programcodex.concurrency.thread;

public class SimpleThread implements Runnable {

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()
                + " thread: Hello, from the simplest thread!");
    }
}

The TestSimpleThread class runs the above defined thread. It instantiates a java.lang.Thread class. This class takes a Runnable object in its constructor, so an instance of the SimplestThread class is passed to it. Finally, the start method of Thread class starts the thread, and the run method of it gets called by the Java Virtual Machine.

Every thread has a name. The Thread.currentThread().getName() returns with the name of the current thread. The name of the main thread is main. The started thread’s name is Thread-0.


package com.programcodex.concurrency.thread;

public class TestSimpleThread {

    public static void main(String args[]) {
        new Thread(new SimpleThread()).start();

        System.out.println(Thread.currentThread().getName()
                + " thread: I've just started a thread and I'm done.");
    }
}

The output of the above test class:

Starting a Thread

Thread as Superclass

There is another way to define a thread, but that uses inheritance instead of interface, which makes it less flexible because a class can implement several interface, but it can only extend one class.

To use this option, our thread class has to extend the java.lang.Thread class, which has a run method that does nothing. This method gets overridden by our subclass.


package com.programcodex.concurrency.thread;

public class SimpleThreadExt extends Thread {

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()
                + " thread: Hello, from the simplest thread!");
    }
}

The SimpleThreadExt class has the same output like the SimpleThread class above. It can be started like this: new SimpleThreadExt().start();

Name the Thread

The default names of the threads can be changed in multiple ways. The Thread class has another constructor that takes the desired name of the thread. It can also be changed later with the setName method.

This name not required to be unique, however practical to make them unique or not set at all.


package com.programcodex.concurrency.thread;

public class NameTheThreads {

    public static void main(String[] args) {
        Thread t = new Thread(new SimpleThread(), "Bla-bla");
        t.start();

        Thread.currentThread().setName("Mainly");

        System.out.println(Thread.currentThread().getName()
                + " thread: I have a strange name. Don't do this.");
    }
}

The output of the above class:

Starting a Thread with Name