赞
踩
说明:
自定义的线程是由方法start()启动一个线程,然后执行run()方法,过程中可以通过sleep()方法进入睡眠延迟,通过interrupt()方法进行中断当前线程,并且在异常方法中return跳出当前线程,如果不return跳出,线程还会继续执行run()方法中的程序。获取当前线程名称:Thread.currentThread().getName()。
package com.springboot.thread.bean; import java.text.SimpleDateFormat; import java.util.Date; //继承Thread类,重写run方法(其实Thread类本身也实现了Runnable接口) public class MyThread extends Thread { //存储传入参数字段,在初始化子进程时,传入参数 private String name; public MyThread(String name){ this.name = name; } @Override public void run() { for (int i = 0; i < 100; i++) { System.out.println(Thread.currentThread().getName()); System.out.println("多线程执行任务:是否可以传参数:"+this.name+"------------"+(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()))); //Thread.yield(); try{ Thread.sleep(2000); }catch (InterruptedException e) { e.printStackTrace(); return; //如果要异常时终止当前线程,一定要返回return; } } } }
@GetMapping("/startThread/{name}")
public String startThread(@PathVariable String name) throws InterruptedException{
Thread myThread = new MyThread(name);
myThread.start();
// Thread.sleep(4000);
// myThread.interrupt();
return Thread.currentThread().getName();
}
说明:
主要实现AsyncConfigurer接口的getAsyncExecutor()方法获取一个执行器Executor 。
package com.springboot.thread.config; import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.AsyncConfigurer; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import java.util.concurrent.Executor; @Configuration //开启异步支持 @EnableAsync public class AsyncTaskConfig implements AsyncConfigurer { @Override public Executor getAsyncExecutor() { ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor(); taskExecutor.setCorePoolSize(50); taskExecutor.setMaxPoolSize(100); taskExecutor.setQueueCapacity(100); taskExecutor.initialize(); return taskExecutor; } @Override public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() { return null; } }
通过注释@Async来声明异步方法:
package com.springboot.thread.service;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
@Service
public class ExecuteAsyncTask {
@Async
public void executAsyncTask(Integer i){
System.out.println("执行异步任务:----"+i);
}
}
5.congroller层调用异步方法:
@Autowired
private ExecuteAsyncTask executeAsyncTask;
@GetMapping("/startAsync")
public String startAsync() {
for (int i = 0; i < 1000; i++) {
executeAsyncTask.executAsyncTask(i);
}
System.out.println("----程序运行完毕,不知道执行异步任务是否完毕----");
return Thread.currentThread().getName();
}
效果如下:
package com.springboot.thread.bean; import java.util.concurrent.Callable; public class MyCallable implements Callable<Integer> { @Override public Integer call() throws Exception { int result =0; for (int i = 0; i < 10; i++) { result =i; } System.out.println(Thread.currentThread().getName()); return result; } }
5.2controller层中使用自定义的callable实现类,需要通过FutureTask对象来获取返回的值,再传入到Thread线程中,生成一个子线程。
@GetMapping("/startCallable")
public String startCallable() throws InterruptedException, ExecutionException {
for (int i = 0; i < 100; i++) {
Callable<Integer> mycallable = new MyCallable();
FutureTask<Integer> futureTask = new FutureTask<>(mycallable);
new Thread(futureTask).start();
System.out.println(Thread.currentThread().getName() + "------" + futureTask.get());
}
return Thread.currentThread().getName();
}
说明:
把runable实例生成一个线程实例,并且执行start()的方法。
@GetMapping("/submitExecutor") public String submitExecutor() { // 使用Executors工具类中的方法创建线程池 ExecutorService executorService = Executors.newFixedThreadPool(10); MyRunnables myRunnables = new MyRunnables(); // 为线程池中的线程分配任务,使用submit方法,传入的参数可以是Runnable的实现类,也可以是Callable的实现类 for (int i = 0; i < 1000; i++) { executorService.submit(myRunnables); System.out.println(Thread.currentThread().getName() + "---"+i+"---"); } // 关闭线程池 // shutdown : 以一种平和的方式关闭线程池,在关闭线程池之前,会等待线程池中的所有的任务都结束,不在接受新任务 // shutdownNow : 立即关闭线程池 executorService.shutdown(); return Thread.currentThread().getName(); }
package com.springboot.thread.bean;
public class MyRunnables implements Runnable {
private int i =0;
@Override
public void run() {
while (i<5000){
System.out.println(Thread.currentThread().getName()+"----"+i++);
}
}
}
6.6.效果如下,同一个线程池里,使用不同的线程执行代码:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。