一、实现多线程的方式
1、继承Thread类
2、实现Runnable接口
3、实现Callable接口(JDK>=1.5)
4、线程池方式创建
二、代码示例
package com.example.demo5;
import lombok.SneakyThrows;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import javax.annotation.Resource;
import java.util.concurrent.*;
@SpringBootTest
public class ThreadTest {
@Resource
private ThreadPoolConfig threadPool;
@Test
@SneakyThrows
void test() {
System.out.println("==1、继承Thread类start==");
Thread thread1 = new Thread(new ThreadDemo("继承thread类"));
thread1.start();
Thread.sleep(10);
System.out.println("==1、继承Thread类end==\n");
System.out.println("==2、实现Runnable接口start==");
Thread thread2 = new Thread(new RunnableDemo("实现Runnable接口"));
thread2.start();
Thread.sleep(10);
System.out.println("==2、实现Runnable接口end==\n");
System.out.println("==3、实现Callable接口start==");
FutureTask<String> callable = new FutureTask<>(new CallableDemo("callable线程1"));
callable.run();
System.out.println(callable.get());
Thread.sleep(10);
System.out.println("==3、实现Callable接口end==\n");
System.out.println("==4、线程池方式创建1 start==");
Executor executor = threadPool.taskExecutor();
executor.execute(new ThreadDemo("继承thread类"));
executor.execute(new RunnableDemo("实现Runnable接口"));
Thread.sleep(10);
System.out.println("==4、线程池方式创建1 end==\n");
System.out.println("==4、线程池方式创建2 start==");
ExecutorService executorService = Executors.newCachedThreadPool((ThreadFactory) executor);
executorService.execute(new ThreadDemo("继承thread类"));
executorService.execute(new RunnableDemo("实现Runnable接口"));
Future<String> callable2 = executorService.submit(new CallableDemo("callable线程2"));
System.out.println(callable2.get());
Thread.sleep(10);
System.out.println("==4、线程池方式创建2 end==");
}
}
class ThreadDemo extends Thread {
private String name;
public ThreadDemo(String name) {
this.name = name;
}
@Override
public void run() {
System.out.println(String.format("%s ThreadDemo run...", name));
}
}
class RunnableDemo implements Runnable {
private String name;
public RunnableDemo(String name) {
this.name = name;
}
@Override
public void run() {
System.out.println(String.format("%s RunnableDemo run...", name));
}
}
class CallableDemo implements Callable<String> {
private String name;
public CallableDemo(String name) {
this.name = name;
}
@Override
public String call() {
System.out.println(String.format("%s CallableDemo run...", name));
return this.name;
}
}
@Configuration
@EnableAsync
class ThreadPoolConfig {
@Bean(name = "taskExecutor")
public Executor taskExecutor() {
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setCorePoolSize(20);
taskExecutor.setMaxPoolSize(50);
taskExecutor.setQueueCapacity(5000);
taskExecutor.setKeepAliveSeconds(600);
taskExecutor.setThreadNamePrefix("taskExecutor--");
taskExecutor.setWaitForTasksToCompleteOnShutdown(true);
taskExecutor.setAwaitTerminationSeconds(6000);
return taskExecutor;
}
}
三、执行结果
