一、在新创建一个SpringBoot工程中实现基本的网页请求
需要的包和类
- service包:接口AsyncService ,impl包用存放实现类AsyncServiceImpl
- controller包:控制器AsyncController
public interface AsyncService {
boolean tack1() throws InterruptedException;
boolean tack2() throws InterruptedException;
boolean tack3() throws InterruptedException;
}
//注入spring容器表示是一个Service服务处
@Service
public class AsyncServiceImpl implements AsyncService {
@Override
public boolean tack1() throws InterruptedException {
//计算开始时间
long start = System.currentTimeMillis();
System.out.println(Thread.currentThread().getName()+"]task1开始于"+start+"秒");
//让线程暂停1000毫秒=1秒
Thread.sleep(1000);
//计算结束时间
long end = System.currentTimeMillis();
System.out.println(Thread.currentThread().getName()+"]task1结束于"+(end-start)+"秒");
return true;
}
@Override
public boolean tack2() throws InterruptedException {
//计算开始时间
long start = System.currentTimeMillis();
System.out.println(Thread.currentThread().getName()+"]task2开始于"+start+"秒");
//让线程暂停1000毫秒=1秒
Thread.sleep(1000);
//计算结束时间
long end = System.currentTimeMillis();
System.out.println(Thread.currentThread().getName