多线程的使用例子

在一个请求中需要 调用几个service,每个service耗时长。

可以通过多线程的方式来处理

package cn.rc.controller;

import cn.rc.common.entity.JsonResponse;
import cn.rc.filter.AccessLimit;
import cn.rc.rpc.TestLimitApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.concurrent.*;

@RestController
@RequestMapping("test")
public class TestAccessLimitController {
    @Autowired
    private TestLimitApi testLimitApi;
    //CountDownLatch count = new CountDownLatch(3);
    @GetMapping("accessLimit")
    public JsonResponse testAccessLimit() throws InterruptedException, ExecutionException {

        System.out.println(new Date());
//        List list1 = this.getList1();
//        List list2 = this.getList2();
//        List list3 = this.getList3();
//        Map map = new HashMap<>();
//        map.put("aList",list1);
//        map.put("bList",list2);
//        map.put("cList",list3);

        ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(5, 10, 2000, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(5));

        List1 task1 = new List1();
        List2 task2 = new List2();
        List3 task3 = new List3();
        Future submit1 = poolExecutor.submit(task1);
        Future submit2 = poolExecutor.submit(task2);
        Future submit3 = poolExecutor.submit(task3);
//        try {
//            count.await();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        poolExecutor.shutdown();
        Map map = new HashMap();
        map.put("aList",submit1.get());
        map.put("bList",submit2.get());
        map.put("cList",submit3.get());

        System.out.println(new Date());

        return new JsonResponse(1,"success",map);
    }

    class List1 implements Callable{
        @Override
        public List call() throws Exception {
            List list1 = getList1();
            //count.countDown();
            return list1;
        }
    }

    class List2 implements Callable{
        @Override
        public List call() throws Exception {
            List list2 = getList2();
            //count.countDown();
            return list2;
        }
    }

    class List3 implements Callable{
        @Override
        public List call() throws Exception {
            List list3 = getList3();
            //count.countDown();
            return list3;
        }
    }


    @GetMapping("slep/{id}")
    public JsonResponse slep(@PathVariable("id") Integer id) {
        return  testLimitApi.testLimit(id);
    }

    public List getList1()  {
        try {
            Thread.sleep(5000);
        }catch (Exception e){

        }

        List arrayList = new ArrayList();
        arrayList.add(1);
        return arrayList;
    }

    public List getList2(){
        try {
            Thread.sleep(10000);
        }catch (Exception e){

        }
        List arrayList = new ArrayList();
        arrayList.add(2);
        return arrayList;
    }

    public List getList3(){
        try {
            Thread.sleep(20000);
        }catch (Exception e){

        }
        List arrayList = new ArrayList();
        arrayList.add(3);
        return arrayList;
    }
}

原本需要处理30秒才响应的接口,利用多线程后  只需要20秒 就响应来,大大优化处理速度

原文地址:https://www.cnblogs.com/wanjun-top/p/13288799.html