Creating a java.lang.Thread instance

suggest change

There are two main approaches to creating a thread in Java. In essence, creating a thread is as easy as writing the code that will be executed in it. The two approaches differ in where you define that code.

In Java, a thread is represented by an object - an instance of java.lang.Thread or its subclass. So the first approach is to create that subclass and override the run() method.

Note: I’ll use Thread to refer to the java.lang.Thread class and thread to refer to the logical concept of threads.

class MyThread extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println("Thread running!");
        }
    }
}

With the code to run already defined, create the thread object like this:

MyThread t = new MyThread();

Constructing a Thread does not start it. Call start() to run run() on a new thread.

The Thread class also has a constructor that accepts a string, used as the thread’s name. This is particularly useful when debugging a multithreaded program.

class MyThread extends Thread {
    public MyThread(String name) {
        super(name);
    }
    
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println("Thread running! ");
        }
    }
}

MyThread t = new MyThread("Greeting Producer");

The second approach is to define the code using java.lang.Runnable and its only method run(). The Thread class then runs that method on a separate thread. Create the thread with a constructor that accepts a Runnable.

Thread t = new Thread(aRunnable);

This can be combined with lambdas or method references (Java 8):

Thread t = new Thread(operator::hardWork);

You can specify the thread’s name, too.

Thread t = new Thread(operator::hardWork, "Pi operator");

Practically speaking, you can use either approach. The usual advice is to prefer a Runnable, because the class can still extend another class.


For each of the four constructors mentioned above, there is also an overload that accepts a java.lang.ThreadGroup as the first parameter.

ThreadGroup tg = new ThreadGroup("Operators");
Thread t = new Thread(tg, operator::hardWork, "PI operator");

A ThreadGroup represents a set of threads. A Thread can be placed in a ThreadGroup only through a Thread constructor. The group can then manage its threads, and a thread can read information from its group.

The Thread class has these public constructors:

Thread()
Thread(String name)
Thread(Runnable target)
Thread(Runnable target, String name)
Thread(ThreadGroup group, String name)
Thread(ThreadGroup group, Runnable target)
Thread(ThreadGroup group, Runnable target, String name)
Thread(ThreadGroup group, Runnable target, String name, long stackSize)

The last one lets the caller suggest a stack size for the new thread. The JVM may ignore that value.


Readability suffers when many threads are created with the same properties. java.util.concurrent.ThreadFactory encapsulates that procedure in its newThread(Runnable) method.

class WorkerFactory implements ThreadFactory {
    private int id = 0;

    @Override
    public Thread newThread(Runnable r) {
        return new Thread(r, "Worker " + id++);
    }
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents