多線程的使用例子


在一個請求中需要 調用幾個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秒 就響應來,大大優化處理速度

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM