如何创建一个线程

本文最后更新于 2025年5月20日 14:36

Thread类实现了Runnable接口,Runnable接口中有一个未实现的方法run()

严格来说,只有一种方式能够创建 Java 线程:new Thread().start(),因为无论哪种方式,最终还是依赖于new Thread().start()

  • 继承Thread类,重写run()方法,调用start()方法开启线程;

    1
    2
    3
    4
    5
    6
    7
    class MyThread extends Thread {
         @Override
         public void run() {
             // 线程执行的任务
             System.out.println("线程正在运行:" + Thread.currentThread().getName());
        }
     }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    public class ThreadTest {
         public static void main(String[] args) {
             // 创建线程实例
             MyThread thread1 = new MyThread();
             MyThread thread2 = new MyThread();
     
             // 启动线程
             thread1.start();
             thread2.start();
        }
     }
  • 继承Thread类,重写run()方法,调用start()方法开启线程;(匿名子类写法)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    public class MyThread {
         public static void main(String[] args) {
             // 使用匿名子类继承 Thread 类
             Thread thread = new Thread() {
                 @Override
                 public void run() {
                     System.out.println("线程正在运行:" + Thread.currentThread().getName());
                }
            };
     
             // 启动线程
             thread.start();
        }
     }
  • 实现Runnable接口;

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    public class ThreadTest {
         public static void main(String[] args) {
             // 创建并启动线程
             Thread thread1 = new Thread(new Runnable() {
                 @Override
                 public void run() {
                     System.out.println("线程1正在运行:" + Thread.currentThread().getName());
                }
            });
             thread1.start();
     
             // 创建并启动另一个线程
             Thread thread2 = new Thread(new Runnable() {
                 @Override
                 public void run() {
                     System.out.println("线程2正在运行:" + Thread.currentThread().getName());
                }
            });
             thread2.start();
        }
     }
  • 实现Runnable接口;(匿名内部类写法)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    public class ThreadTest {
         public static void main(String[] args) {
             // 使用 lambda 表达式创建并启动线程
             Thread thread1 = new Thread(() -> {
                 System.out.println("线程1正在运行:" + Thread.currentThread().getName());
            });
             thread1.start();
     
             // 使用 lambda 表达式创建并启动另一个线程
             Thread thread2 = new Thread(() -> {
                 System.out.println("线程2正在运行:" + Thread.currentThread().getName());
            });
             thread2.start();
        }
     }
  • 实现Callable接口;

  • 使用ExecutorService线程池。


如何创建一个线程
http://example.com/2025/03/11/如何创建一个线程/
作者
Moonike
发布于
2025年3月11日
更新于
2025年5月20日
许可协议