实现多线程的方式-两种
来源官方文档:https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/lang/Thread.html
1、继承Thread 方法
public class ExtendsThread extends Thread {
@Override
public void run(){
System.out.println("继承Thread线程实现");
}
}
2、实现Runnable接口
public class ImplRunable implements Runnable {
@Override
public void run() {
System.out.println("实现Runnable创建线程");
}
}
3、执行
public class MyTest {
public static void main(String[] args){
ExtendsThread thread = new ExtendsThread();
thread.start();
ImplRunable implRunable = new ImplRunable();
new Thread(implRunable).start();
}
}
4、执行结果
继承Thread线程实现
实现Runnable创建线程
5、思考 :一个类同时继承Thread和实现Runnable接口时的执行结果
public class ThreadAndRunnable extends Thread implements Runnable {
@Override
public void run() {
System.out.println("实现Thread + Runable创建线程");
}
}
执行代码 这时启动线程两种方式都可以
public class MyTest {
public static void main(String[] args) throws Exception{
ThreadAndRunnable threadAndRunable = new ThreadAndRunnable();
threadAndRunable.start();
new Thread(threadAndRunable).start();
}
}
执行结果
实现Thread + Runable创建线程
实现Thread + Runable创建线程
其他创建线程方法分析-Thread 与 Runnable接口之上的扩展
1、实现Callnable接口
public class ImplCallable implements Callable {
@Override
public Object call() throws Exception {
System.out.println("callable 接口实现");
return "callable end";
}
}
运行
public static void main(String[] args) throws Exception{
Callable callable = new ImplCallable();
FutureTask futureTask = new FutureTask(callable);
Thread thread = new Thread(futureTask);
thread.start();
System.out.println(futureTask.get());
}
结果
callable 接口实现
callable end
代码:FutureTask futureTask = new FutureTask(callable);
查看源码 FutureTask 是实现Runnable接口的
class FutureTask<V> implements RunnableFuture<V>
interface RunnableFuture<V> extends Runnable, Future<V>
启动线程的方式
- start() 与 run()
- run() -- 错误的启动方式
public class ExtendsThread extends Thread{
@Override
public void run(){
try {
Thread.sleep(10000); //休眠10秒
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("继承Thread线程实现 end"+ this.getName());
}
}
执行
public static void main(String[] args) throws Exception{
ExtendsThread extendsThread = new ExtendsThread();
extendsThread.run();
// extendsThread.start();
System.out.println("main end");
}
输出----会等待10秒后才打印
- start()
public static void main(String[] args) throws Exception{
ExtendsThread extendsThread = new ExtendsThread();
// extendsThread.run();
extendsThread.start();
System.out.println("main end");
}
输出--- main出后 Thread10秒后打印文字
eng run() 就像是调用对象的一个普通方法
start() 才会真正的新建一个线程去执行
正确停止线程 interrupt
- 使用interrupt通知停止线程
- 线程停止的情况 - 1、代码执行完成 2、出现异常
Interrupt 停止信号 非强制停止
public class ExtendsThread extends Thread{
@Override
public void run(){
int num = 0;
while (true && !Thread.currentThread().isInterrupted()){
System.out.println("实现Runable创建线程" + num ++);
}
}
}
发送信号
public static void main(String[] args) throws Exception{
ExtendsThread extendsThread = new ExtendsThread();
extendsThread.start();
Thread.sleep(100);//主线程睡眠- 新线程一点执行时间
extendsThread.interrupt();//发送中断信号
System.out.println("main end");
}
- 恢复中断
public void run(){
int num = 0;
while (true && !Thread.currentThread().isInterrupted()){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
Thread.currentThread().interrupt(); //恢复中断
e.printStackTrace();
}
System.out.println("实现Runable创建线程" + num ++);
}
}
错误的停止线程 stop()
线程的六种状态 -- Thread 类中 源码
public enum State {
NEW,//新增
RUNNABLE,//可运行 - 正在运行也是该状态
BLOCKED,//阻塞
WAITING,//等待
TIMED_WAITING,//计时等待
TERMINATED;//运行种子
}