Course Content
Core Java
About Lesson

The Thread class in Java provides several constructors that allow you to create threads with different configurations.

Thread()

Default constructor. It allocates a new Thread object with default name and no target. It will execute Thread’s class run() method which does nothing.

public class Main {
    public static void main(String[] args) {
        Thread thread = new Thread();
        thread.start(); // Starts the thread, executes Thread's run() method
    }
}

Thread(Runnable Target)

Creates a new thread that executes the specified Runnable target. It will execute the run() method of the specified Runnable target.

public class MyRunnable implements Runnable {
    public void run() {
        System.out.println("Thread is running");
    }
}

public class Main {
    public static void main(String[] args) {
        Runnable myRunnable = new MyRunnable();
        Thread thread = new Thread(myRunnable);
        thread.start(); // Starts the thread, executes MyRunnable's run() method
    }
}

Thread(Runnable Target, String name)

Creates a new thread with a specified name that will execute the run() method of the specified Runnable target.

public class MyRunnable implements Runnable {
    public void run() {
        System.out.println("Thread is running");
    }
}

public class Main {
    public static void main(String[] args) {
        Runnable myRunnable = new MyRunnable();
        Thread thread = new Thread(myRunnable, "MyThread");
        System.out.println("Thread Name: " + thread.getName()); // MyThread
        thread.start(); // Starts the thread, executes MyRunnable's run() method
    }
}

Thread(String name)

Creates a new thread with a specified name.

public class Main {
    public static void main(String[] args) {
        Thread thread = new Thread("MyThread");
        System.out.println("Thread Name: " + thread.getName()); // MyThread
        thread.start(); // Starts the thread
    }
}

Thread(ThreadGroup group, String name)

Allocates a new Thread object in a specified thread group with the specified name.

Thread(ThreadGroup group, Runnable target)

Creates a new thread in a specified thread group that will execute the run() method of the specified Runnable target.

public class MyRunnable implements Runnable {
    public void run() {
        System.out.println("Thread is running");
    }
}

public class Main {
    public static void main(String[] args) {
        ThreadGroup group = new ThreadGroup("MyGroup");
        Runnable myRunnable = new MyRunnable();
        Thread thread = new Thread(group, myRunnable);
        thread.start(); // Starts the thread in the specified thread group
    }
}

Thread(ThreadGroup group, Runnable target, String name)

Allocates a new Thread object so that it has target as its run object, has the specified name as its name, and belongs to the thread group referred to by group.

public class MyRunnable implements Runnable {
    public void run() {
        System.out.println("Thread is running");
    }
}

public class Main {
    public static void main(String[] args) {
        ThreadGroup group = new ThreadGroup("MyGroup");
        Runnable myRunnable = new MyRunnable();
        Thread thread = new Thread(group, myRunnable, "MyThread");
        System.out.println("Thread Name: " + thread.getName()); // MyThread
        thread.start(); // Starts the thread in the specified thread group, executes MyRunnable's run() method
    }
}

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

Allocates a new Thread object so that it has target as its run object, has the specified name as its name, and belongs to the thread group referred to by group, and has the specified stack size.

public class MyRunnable implements Runnable {
    public void run() {
        System.out.println("Thread is running");
    }
}

public class Main {
    public static void main(String[] args) {
        ThreadGroup group = new ThreadGroup("MyGroup");
        Runnable myRunnable = new MyRunnable();
        Thread thread = new Thread(group, myRunnable, "MyThread", 1024);
        System.out.println("Thread Name: " + thread.getName()); // MyThread
        thread.start(); // Starts the thread in the specified thread group with custom stack size, executes MyRunnable's run() method
    }
}

Scroll to Top