Java多线程的实现方式有3种,分别是继承Thread类、实现Runnable接口、实现Callable接口、线程池
一、继承Thread类实现
1、继承Thread类,并重写run方法
/**
* 继承Thread类,并重写run方法
*/
public class MyThread extends Thread {
@Override
public void run() {
super.run();
System.out.println("MyThread...");
}
}
2、创建Thread对象,调用start方法
MyThread thread = new MyThread();
thread.start();
说明:
调用start方法后并不意味着会立刻执行run方法里面的代码,只是使该线程处于可运行状态了,具体什么时候执行,要由系统来决定。
该方式使用的是继承的方式,由于java不支持多继承,所以如果需要继承其他类的时候,就不能使用该方式了。
线程不能同时调用2次start方法,否则会报错:
分析源码,由于启动有个threadStatus,启动之前默认为0,如下:
二、实现Runnable接口方式
1、 实现Runnable接口,并重写run方法
/**
* 实现Runnable接口,并重写run方法
*/
public class MyRunnable implements Runnable{
@Override
public void run() {
System.out.println("MyRunnable...");
}
}
2、 用MyRunnable的对象作为参数实例化Thread,并调用 Thread的start方法。
MyRunnable runnable=new MyRunnable();
Thread thread=new Thread(runnable);
thread.start();
说明:
该方式是实现接口的方式,限制较小,没有第一种方式的继承的问题,所以推荐使用这种方式。
三、通过创建线程池,实现Callable接口方式
1、 实现Callable接口,并重写call方法
/**
* 实现Callable接口,并重写call方法
*/
public class MyCallable implements Callable<String>{
@Override
public String call() throws Exception {
return "MyCallable...";
}
}
2、创建Callable实现类的实例,创建一个线程池,调用线程池的submit方法,如果需要返回值就调用Future的get方法获取。
//创建和调用
MyCallable callable=new MyCallable();
ExecutorService eService=Executors.newSingleThreadExecutor();
Future<String> future=eService.submit(callable);
//获取返回结果
try {
String result=future.get();
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
说明:
Callable和Runnable功能差不多,但是相比Runnable来说还是有很多区别的,主要体现在以下3点:
(1)Callable的call方法有返回值并且可以抛异常,而Runnable的run方法就没有返回值也没有抛异常。
(2)Callable运行后可以拿到一个Future对象,这个对象表示异步计算结果,可以从通过Future的get方法获取到call方法返回的结果。但要注意调用Future的get方法时,当前线程会阻塞,直到call方法返回结果。
(3)Runnable是作为线程的构造参数运行的,Callable是作为线程池的submit方法的参数运行的。
四、通过SpringBoot结合@Async实现异步调用线程
1、定义线程池
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@EnableAsync
@Configuration
class TaskPoolConfig {
@Bean("taskExecutor")
public Executor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(16);
executor.setMaxPoolSize(100);
executor.setQueueCapacity(200);
executor.setKeepAliveSeconds(60);
executor.setThreadNamePrefix("taskExecutor-");
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
return executor;
}
}
}
2、使用线程池
@Slf4j
@Component
public class Task {
public static Random random = new Random();
@Async("taskExecutor")
public void doTaskOne() throws Exception {
log.info("开始做任务一");
long start = System.currentTimeMillis();
Thread.sleep(random.nextInt(10000));
long end = System.currentTimeMillis();
log.info("完成任务一,耗时:" + (end - start) + "毫秒");
}
@Async("taskExecutor")
public void doTaskTwo() throws Exception {
log.info("开始做任务二");
long start = System.currentTimeMillis();
Thread.sleep(random.nextInt(10000));
long end = System.currentTimeMillis();
log.info("完成任务二,耗时:" + (end - start) + "毫秒");
}
@Async("taskExecutor")
public void doTaskThree() throws Exception {
log.info("开始做任务三");
long start = System.currentTimeMillis();
Thread.sleep(random.nextInt(10000));
long end = System.currentTimeMillis();
log.info("完成任务三,耗时:" + (end - start) + "毫秒");
}
}
3、单元测试
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class ApplicationTests {
@Autowired
private Task task;
@Test
public void test() throws Exception {
task.doTaskOne();
task.doTaskTwo();
task.doTaskThree();
Thread.currentThread().join();
}
}
执行上面的单元测试,我们可以在控制台中看到所有输出的线程名前都是之前我们定义的线程池前缀名开始的,说明我们使用线程池来执行异步任务的试验成功了!
参考文章
https://www.jianshu.com/p/732a8858b0d4
https://segmentfault.com/a/1190000022394442
https://blog.csdn.net/weixin_43323416/article/details/106474286