一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

Java线程创建的5种方式详解

时间:2026-05-31 16:30:01 编辑:袖梨 来源:一聚教程网

多线程编程是Java开发的核心技术之一,掌握线程创建方式对提升程序性能至关重要。本文将详细介绍五种常见实现方法。

一、多线程基础概念

1. 线程与进程的关系

  1. 线程作为轻量级进程,相比重量级进程具有更小开销
  2. 操作系统以进程为资源分配的基本单位,以线程为调度执行的基本单位
  3. 进程间相互独立,线程间可能互相干扰(共享进程资源)
  4. 同进程内的线程共享资源,创建销毁无需额外资源申请
  5. 多核CPU时代,多线程能有效提升计算资源利用率

2. 线程状态:Thread.sleep(millis: 1000)

  1. sleep方法使线程暂时进入阻塞状态,暂停执行指定时长

3. 方法重写要求

  1. 子类重写方法必须与父类保持完全一致的签名(方法名、参数、异常声明等)

二、线程创建的几种写法

写法1:继承Thread,重写run

class MyThread extends Thread {
    @Override
    public void run() {
        while (true) {
            System.out.println("hello thread");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

写法2:实现Runnable,搭配Thread执行

class MyRunnable implements Runnable {
    @Override
    public void run() {
        while (true) {
            System.out.println("hello thread");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

public class Demo3 {
    public static void main(String[] args) throws InterruptedException {
        MyRunnable r = new MyRunnable();
        Thread t = new Thread(r);
        t.start();
        
        while (true) {
            System.out.println("hello main");
            Thread.sleep(1000);
        }
    }
}
  1. Runnable需要配合Thread作为执行载体
  2. 无需重写run方法,直接实现接口即可
  3. 新线程会执行Runnable中定义的run逻辑

写法3:继承Thread,使用匿名内部类

public class Demo3 {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread() {
            @Override
            public void run() {
                while (true) {
                    System.out.println("hello thread");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        };
        t.start();
        
        while (true) {
            System.out.println("hello main");
            Thread.sleep(1000);
        }
    }
}

Java线程创建的5种写法详解

  1. 通过匿名内部类方式继承Thread
  2. 重写run方法定义线程逻辑
  3. 实例化后通过start启动线程

该方法适合一次性使用场景。

写法4:实现Runnable,使用匿名内部类

public class Demo3 {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    System.out.println("hello thread");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        });
        t.start();
        
        while (true) {
            System.out.println("hello main");
            Thread.sleep(1000);
        }
    }
}
  1. 通过匿名类实现Runnable接口
  2. 将实例作为参数传入Thread构造器

写法5:【简化】lambda表达式

public class Demo5 {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(() -> {
            while (true) {
                System.out.println("hello thread");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        t.start();
        
        while (true) {
            System.out.println("hello main");
            Thread.sleep(1000);
        }
    }
}
  1. lambda本质是匿名函数,简化代码结构
  2. 适用于函数式接口如Runnable
  3. Java8开始支持的语法糖

三、常见面试题:Thread对象与start()的关系

问题:多次调用start()会怎样?

public static void main(String[] args) {
    Thread thread = new Thread(() -> {
        System.out.println("hello");
    });
    thread.start();
    thread.start(); // 报错!
    thread.start();
}
  1. 抛出IllegalThreadStateException异常
  2. Java规定Thread对象与系统线程一一对应
  3. 每个Thread实例仅允许start一次

四、补充:高耦合 vs 低耦合(生活类比)

  1. 高耦合场景如密切人际关系,一方变化会显著影响另一方
  2. 低耦合设计如Runnable接口,任务与执行载体解耦

五、包(package)的概念

  1. 包用于组织管理大量Java类文件
  2. 通过import语句引入所需类
  3. java.lang包默认自动导入

本文全面解析了Java线程的五种创建方式,从基础实现到高级语法,帮助开发者掌握多线程编程的核心技术。

热门栏目