1.实现Runable接口
实现Runable接口,实现run方法;
这种方式创建的线程实现类执行时需要创建Thread实例去运行该任务
示例如下:
package com.example.springbootdamo.Thread;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class RunnableTest implements Runnable{
static Logger log = LogManager.getLogger();
@Override
public void run() {
log.info("实现Runable接口的线程任务执行中");
}
}
public static void runable(ThreadPoolExecutor poolExecutor) { //创建线程任务对象 RunnableTest runnableTest = new RunnableTest(); //调用方法的线程执行 runnableTest.run(); //线程调用 Thread thread = new Thread(runnableTest); //重开线程执行 thread.start(); //调用方法的线程执行 thread.run(); //无返回值的线程池运行任务 poolExecutor.execute(runnableTest); //有返回值的线程池运行任务 Future<?> submit = poolExecutor.submit(runnableTest); }运行图如下:
1.继承Thread类
继承Thread类,重写run方法;直接运行run方法 ,调用start(),或者交由线程池执行
package com.example.springbootdamo.Thread;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.concurrent.TimeUnit;
/**
* @ClassName ThreadTest
* @Description TODO
* @Author zoro
* @Date 2024/4/4 22:24
*/
public class ThreadTest extends Thread{
static Logger log = LogManager.getLogger();
@Override
public void run(){
log.info(this.getName()+"继承Thread类的线程任务执行开始");
try {
//睡五秒
this.sleep(5);
log.info(this.getName()+"继承Thread类的线程任务执行了五秒钟");
} catch (InterruptedException e) {
e.printStackTrace();
}
log.info(this.getName()+"继承Thread类的线程任务执行结束");
}
}
运行示例图如下:由日志是被那个线程所以打印的可知当前执行线程为何
3.实现Callable接口
实现Callable接口,实现call方法,利用FutureTask的构造器(入参为callable对象的实现类)创建实例,调用该对象的run方法或者直接用线程池执行该实例,FutureTask实现了RunnableFuture接口,RunnableFuture实现了Runnable与Future接口。
方法实现:
public static void callable(ThreadPoolExecutor poolExecutor) { CallableTest callable = new CallableTest(); FutureTask futureTask = new FutureTask<>(callable); //运调用方法的线程行 futureTask.run(); //线程池调用 poolExecutor.execute(futureTask); //线程池运行时,调用方法的线程等待返回值 //方式一 Future<ResponseBO> submit = poolExecutor.submit(callable); //方式二 poolExecutor.submit(futureTask); try { //方式一 ResponseBO responseBO = submit.get(); log.info(responseBO.getReturnMessage()); //方式二 ResponseBO responseBO2 = (ResponseBO) futureTask.get(); log.info(responseBO2.getReturnMessage()+"+++"); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); }运行结果:因为返回值问题,所以调用线程会等待线程执行完任务后继续执行。从下方线程池的工作任务队列workers可知三次都是重新创建的线程
4.线程池创建线程
主要是通过线程池工厂创建
public void threadPool(){ //默认线程池工厂 ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(5, 5, 5000, TimeUnit.SECONDS, new LinkedBlockingDeque<>()); //自定义线程池工厂 ThreadPoolExecutor poolExecutor1 = new ThreadPoolExecutor(5, 5, 5000, TimeUnit.SECONDS, new LinkedBlockingDeque<>(),new ThreadFactoryTest()); }线程池创建的线程需要有执行任务对象该对象一般要实现Runable接口或者Callable接口
执行方法有 submit()和execute()两种方式,前者有返回值,后者没有返回值
示例如下:
package com.example.springbootdamo.Thread;
import com.example.springbootdamo.ResponseBO;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.concurrent.*;
public class ThreadPoolTest {
static Logger log = LogManager.getLogger();
public static void main(String[] args) {
//创建线程池
ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(5, 5, 5000, TimeUnit.SECONDS, new LinkedBlockingDeque<>());
callable(poolExecutor);
runable(poolExecutor);
thread(poolExecutor);
log.info("日志打印测试");
}
public static void callable(ThreadPoolExecutor poolExecutor) {
CallableTest callable = new CallableTest();
FutureTask futureTask = new FutureTask<>(callable);
//线程池调用
poolExecutor.execute(futureTask);
//线程池运行时,调用方法的线程等待返回值
//方式一
Future<ResponseBO> submit = poolExecutor.submit(callable);
//方式二
poolExecutor.submit(futureTask);
try {
//方式一
ResponseBO responseBO = submit.get();
log.info(responseBO.getReturnMessage());
//方式二
ResponseBO responseBO2 = (ResponseBO) futureTask.get();
log.info(responseBO2.getReturnMessage()+"+++");
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
public static void runable(ThreadPoolExecutor poolExecutor) {
//创建线程任务对象
RunnableTest runnableTest = new RunnableTest();
//无返回值的线程池运行任务
poolExecutor.execute(runnableTest);
//有返回值的线程池运行任务
Future<?> submit = poolExecutor.submit(runnableTest);
}
public static void thread(ThreadPoolExecutor poolExecutor) {
//创建线程任务对象
ThreadTest threadTest = new ThreadTest();
//线程池运行任务
poolExecutor.execute(threadTest);
// 执行的是线程的任务
poolExecutor.submit(threadTest, new ResponseBO());
}
}
运行结果图如下: