Three Ways to Create Threads in Java: Extending Thread, Implementing Runnable, and Using Callable
Through a whimsical first‑person narrative, the article explains three Java thread‑creation techniques—extending Thread, implementing Runnable (including anonymous and lambda forms), and using Callable with FutureTask—to illustrate their syntax, usage, and the ability to retrieve thread results.
The article tells a fictional story of a thread named "Thread" to introduce Java thread creation methods in an engaging way.
First mother: extending Thread – The simplest way is to subclass Thread and override run() . Example:
// 创建方式 1:继承 Thread
class MyThread extends Thread {
@Override
public void run() {
System.out.println("你好,线程~");
}
}
public class ThreadExample {
public static void main(String[] args) {
Thread thread = new MyThread();
thread.start();
}
}The drawback is Java’s single‑inheritance rule: a class that extends Thread cannot extend any other class.
Second mother: implementing Runnable – By implementing Runnable , a class can still extend another class. Example:
public class ThreadExample2 {
static class MyThread implements Runnable {
@Override
public void run() {
System.out.println("你好,线程~");
}
}
public static void main(String[] args) {
MyThread myThread = new MyThread();
Thread thread = new Thread(myThread);
thread.start();
}
}Anonymous and lambda variants simplify the code:
// Anonymous Runnable
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("我是线程变种方法~");
}
});
t2.start();
// Lambda Runnable (JDK 8+)
Thread t3 = new Thread(() -> {
System.out.println("我是变种 2~");
});
t3.start();Third mother: using Callable – To obtain a result from a thread, implement Callable and wrap it with FutureTask . Example:
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class CreateThreadExample3 {
static class MyCallable implements Callable
{
@Override
public Integer call() throws Exception {
int num = new Random().nextInt(10);
System.out.println("生成随机数:" + num);
return num;
}
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
MyCallable callable = new MyCallable();
FutureTask
futureTask = new FutureTask<>(callable);
Thread thread = new Thread(futureTask);
thread.start();
int result = futureTask.get();
System.out.println("主线程中拿到子线程执行结果:" + result);
}
}This approach allows the main thread to retrieve the value produced by the worker thread.
Summary – The article presents three Java thread‑creation techniques, discusses their advantages and limitations, and demonstrates how to obtain return values using Callable . It blends storytelling with practical code examples for clearer learning.
Full-Stack Internet Architecture
Introducing full-stack Internet architecture technologies centered on Java
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.