Creating and Starting Java Threads: Subclassing Thread and Implementing Runnable
This article explains how to create and start Java threads by directly instantiating Thread, extending the Thread class, implementing the Runnable interface, handling common pitfalls such as calling run() instead of start(), and demonstrates naming threads and using thread pools with clear code examples.
Java thread objects are instances of java.lang.Thread or its subclasses. A thread can be created with Thread thread = new Thread(); and started by invoking thread.start(); , which triggers the thread's run() method.
Two main ways exist to provide code for a thread: extending the Thread class and overriding run() , or creating a class that implements the Runnable interface and passing its instance to a Thread constructor.
Creating a Thread subclass
public class MyThread extends Thread {
public void run() {
System.out.println("MyThread running");
}
}
MyThread myThread = new MyThread();
myThread.start();An anonymous subclass can also be used:
Thread thread = new Thread() {
public void run() {
System.out.println("Thread Running");
}
};
thread.start();Implementing Runnable
public class MyRunnable implements Runnable {
public void run() {
System.out.println("MyRunnable running");
}
}
Thread thread = new Thread(new MyRunnable());
thread.start();An anonymous Runnable can be created similarly:
Runnable myRunnable = new Runnable() {
public void run() {
System.out.println("Runnable running");
}
};
Thread thread = new Thread(myRunnable);
thread.start();Common mistake: calling run() directly instead of start() . Doing so executes the method in the current thread rather than creating a new concurrent thread.
Thread newThread = new Thread(new MyRunnable());
newThread.run(); // should be newThread.start();Threads can be named to aid debugging:
Thread thread = new Thread(runnable, "New Thread");
thread.start();
System.out.println(thread.getName());The current thread's name can be obtained with:
String threadName = Thread.currentThread().getName();Example showing multiple threads with custom names:
public class ThreadExample {
public static void main(String[] args) {
System.out.println(Thread.currentThread().getName());
for (int i = 0; i < 10; i++) {
new Thread("" + i) {
public void run() {
System.out.println("Thread: " + getName() + " running");
}
}.start();
}
}
}Note that thread start order does not guarantee execution order; the operating system scheduler determines which thread runs first.
Original source: http://ifeve.com/creating-and-starting-java-threads/
Java团长 微信号:javatuanzhang 每日分享Java技术干货
长按识别二维码
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.