最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Java线程创建的5种方式详解
时间:2026-05-31 16:30:01 编辑:袖梨 来源:一聚教程网
多线程编程是Java开发的核心技术之一,掌握线程创建方式对提升程序性能至关重要。本文将详细介绍五种常见实现方法。
一、多线程基础概念
1. 线程与进程的关系
- 线程作为轻量级进程,相比重量级进程具有更小开销
- 操作系统以进程为资源分配的基本单位,以线程为调度执行的基本单位
- 进程间相互独立,线程间可能互相干扰(共享进程资源)
- 同进程内的线程共享资源,创建销毁无需额外资源申请
- 多核CPU时代,多线程能有效提升计算资源利用率
2. 线程状态:Thread.sleep(millis: 1000)
sleep方法使线程暂时进入阻塞状态,暂停执行指定时长
3. 方法重写要求
- 子类重写方法必须与父类保持完全一致的签名(方法名、参数、异常声明等)
二、线程创建的几种写法
写法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);
}
}
}
Runnable需要配合Thread作为执行载体- 无需重写
run方法,直接实现接口即可 - 新线程会执行
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);
}
}
}

- 通过匿名内部类方式继承
Thread - 重写
run方法定义线程逻辑 - 实例化后通过
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);
}
}
}
- 通过匿名类实现
Runnable接口 - 将实例作为参数传入
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);
}
}
}
- lambda本质是匿名函数,简化代码结构
- 适用于函数式接口如
Runnable - Java8开始支持的语法糖
三、常见面试题:Thread对象与start()的关系
问题:多次调用start()会怎样?
public static void main(String[] args) {
Thread thread = new Thread(() -> {
System.out.println("hello");
});
thread.start();
thread.start(); // 报错!
thread.start();
}
- 抛出
IllegalThreadStateException异常 - Java规定
Thread对象与系统线程一一对应 - 每个
Thread实例仅允许start一次
四、补充:高耦合 vs 低耦合(生活类比)
- 高耦合场景如密切人际关系,一方变化会显著影响另一方
- 低耦合设计如
Runnable接口,任务与执行载体解耦
五、包(package)的概念
- 包用于组织管理大量Java类文件
- 通过
import语句引入所需类 java.lang包默认自动导入
本文全面解析了Java线程的五种创建方式,从基础实现到高级语法,帮助开发者掌握多线程编程的核心技术。
相关文章
- 《我嘎嘎乱杀》角色属性详解-全面解析各类属性作用 05-31
- 教师引导策略优化解决LLM蒸馏中分布失配问题 05-31
- 魔兽世界_乔拉克的鳄鱼皮带获取方法详解 05-31
- i厦门服务平台怎么申请无犯罪记录证明申请 i厦门APP办理无犯罪记录证明方法 05-31
- 教师引导策略优化解决LLM蒸馏中分布差异失效问题 05-31
- 魔兽世界:梅森纳任务全流程攻略指南 05-31