发布于2026-05-31 阅读(0)
扫一扫,手机访问
sleep让当前线程进入阻塞状态(暂停执行指定时间)。注意是“阻塞”不是“睡眠”——其实用休息更形象。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);
}
}
}
}
// 1. 定义 Runnable 实现类
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);
}
}
}
}
// 2. 创建线程
public class Demo3 {
public static void main(String[] args) throws InterruptedException {
MyRunnable r = new MyRunnable();
Thread t = new Thread(r);
t.start(); // 启动新线程,执行 Runnable 内部的 run
while (true) {
System.out.println("hello main");
Thread.sleep(1000);
}
}
}
Runnable自身一般不会单独执行,它需要搭配一个“载体”——Thread对象来跑。run,因为它已经实现在 Runnable 里了。Runnable 内部的 run 方法。说白了就是把任务和线程的执行方式分开了。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(); // 记住:调用 start 才是真正创建线程
while (true) {
System.out.println("hello main");
Thread.sleep(1000);
}
}
}
步骤:

Thread 的子类(匿名内部类,没有类名)。run。Thread 子类的实例,并用引用 t 指向。特点是方便、一次性使用,适合在代码块内部快速定义逻辑。
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 定义匿名内部类,返回的结果是一个 Runnable 的引用,把它直接传给 Thread 构造器。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 本质上是一个“匿名函数”,在很多语言里早已支持。Java 语法一直很死板,早期必须依托类才能执行逻辑,所以先搞了个匿名内部类作为过渡,后来才引入 lambda。Runnable)才能用 lambda 简化,因为接口里只有一个抽象方法,编译器能自动推断。public static void main(String[] args) {
Thread thread = new Thread(() -> {
System.out.println("hello");
});
thread.start();
thread.start(); // 报错!
thread.start();
}
java.lang.IllegalThreadStateException: Thread already startedThread 对象和一个操作系统中的线程是“一一对应”的关系。同一个 Thread 对象只能被 start 一次,第二次就会抛出这个异常。Thread 上的其他方法或属性,本质上都是在控制这个与系统线程绑定的对象。Thread,那任务和线程就死死绑在一起;哪天想把任务换成其他执行方式(比如线程池),就得大改代码。而用 Runnable 拆分后,任务本身不依赖具体的执行载体,想迁移到哪里都很方便。import java.util.*; 可以导入包中所有类(但一般不推荐,容易拖慢编译并造成模糊引用)。java.lang.*,编译器会自动导入,所以 String、System 这些类可以直接用,不需要显式 import。上一篇:C#AvaloniaUI实现图片读取与显示的两种方式
下一篇:2345好压怎么解压
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
正版软件
正版软件
正版软件
正版软件
正版软件
1
2
3
7
8