Java 开启多线程常见的4种方法
作者:全栈小定 时间:2023-11-23 02:30:10
简介
常见的4种使用线程的方法:
1实现 Runnable 接口;
2实现 Callable 接口;
3继承 Thread 类。
4匿名内部类的写法。
1. 实现 Runnable 接口
编写测试类RunnableDemo
实现 Runnable 接口,实现接口中的 run() 方法。
public class RunnableDemo implements Runnable {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "执行了");
}
/**
* 使用 Runnable 实例再创建一个 Thread 实例,然后调用 Thread 实例的 start() 方法来启动线程。
*/
public static void main(String[] args) {
RunnableDemo runnable = new RunnableDemo();
Thread thread0 = new Thread(runnable);
Thread thread1 = new Thread(runnable);
thread0.start();
thread1.start();
}
}
运行main
方法
2. 实现 Callable 接口
与 Runnable
相比,Callable
可以有返回值,返回值通过 FutureTask
进行封装。
public class CallableDemo implements Callable<String> {
@Override
public String call() {
System.out.println(Thread.currentThread().getName() + "执行了");
return "执行成功!";
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
CallableDemo mc = new CallableDemo();
FutureTask<String> ft0 = new FutureTask<>(mc);
FutureTask<String> ft1 = new FutureTask<>(mc);
Thread thread0 = new Thread(ft0);
Thread thread1 = new Thread(ft1);
thread0.start();
thread1.start();
System.out.println(ft0.get());
System.out.println(ft1.get());
}
}
启动main方法
3. 继承 Thread 类
同样也是需要实现 run() 方法,因为 Thread 类也实现了 Runable 接口。
public class ThreadDemo extends Thread {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "执行了");
}
public static void main(String[] args) {
ThreadDemo thread0 = new ThreadDemo();
ThreadDemo thread1 = new ThreadDemo();
thread0.start();
thread1.start();
}
}
启动main方法
4. 匿名内部类的写法
new Runnable()
是一个实现接口Runnable的类的对象,后面的run()
方法是该类里实现的方法,这就是匿名内部类的写法
public class ThreadTest {
public static void start() {
new Thread(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "执行了");
}
}).start();
}
public static void main(String[] args) {
start();
start();
}
}
来源:https://blog.csdn.net/qq_42411805/article/details/125090981
标签:Java,开启,多线程
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C++ vector数组用法及解析
2022-09-20 01:04:25
详解使用IntelliJ IDEA新建Java Web后端resfulAPI模板
2023-12-14 09:26:04
Spring Boot右键maven build成功但是直接运行main方法出错的解决方案
2021-08-22 00:21:26
![](https://img.aspxhome.com/file/2023/1/129121_0s.png)
Java实现年兽大作战游戏详解
2023-11-08 04:28:05
![](https://img.aspxhome.com/file/2023/5/58875_0s.jpg)
SpringBoot使用过滤器、拦截器和监听器的案例代码(Springboot搭建java项目)
2022-11-21 06:08:54
Java的数据类型和参数传递(详解)
2022-12-30 18:52:25
![](https://img.aspxhome.com/file/2023/7/72297_0s.png)
Java Swing JPanel面板的使用方法
2023-02-13 12:04:49
![](https://img.aspxhome.com/file/2023/2/80892_0s.png)
微服务如何通过feign.RequestInterceptor传递参数
2022-02-26 00:28:20
![](https://img.aspxhome.com/file/2023/5/110815_0s.png)
C#表达式中的动态查询详解【译】
2021-07-29 05:19:42
![](https://img.aspxhome.com/file/2023/3/76423_0s.png)
Java常见数据结构面试题(带答案)
2023-11-24 19:44:05
Spring Cloud 配置中心内容加密的配置方法
2023-02-11 09:05:15
Java中常见的陷阱题及答案
2021-08-10 16:32:11
springBoot+dubbo+zookeeper实现分布式开发应用的项目实践
2021-11-22 02:03:25
![](https://img.aspxhome.com/file/2023/8/85518_0s.png)
Java集合教程之Collection实例详解
2022-12-01 23:45:13
![](https://img.aspxhome.com/file/2023/4/104704_0s.jpg)
springmvc参数为对象,数组的操作
2022-04-20 07:38:03
Java 实战练习之网上电商项目的实现
2021-07-17 04:23:59
![](https://img.aspxhome.com/file/2023/2/94192_0s.png)
Android数据库中事务操作方法之银行转账示例
2023-07-22 07:56:13
深入浅出探索Java分布式锁原理
2021-12-16 11:58:07
![](https://img.aspxhome.com/file/2023/3/110893_0s.png)
C#图像处理之头发检测的方法
2023-11-08 22:10:28
![](https://img.aspxhome.com/file/2023/7/96357_0s.jpg)
使用fastjson中的JSONPath处理json数据的方法
2021-12-14 09:09:58