java线程的使用案例_多线程的使用例子

packagecn.rc.controller;importcn.rc.common.entity.JsonResponse;importcn.rc.filter.AccessLimit;importcn.rc.rpc.TestLimitApi;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.PathVariable;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RestController;import java.util.*;import java.util.concurrent.*;

@RestController

@RequestMapping("test")public classTestAccessLimitController {

@AutowiredprivateTestLimitApi testLimitApi;//CountDownLatch count = new CountDownLatch(3);

@GetMapping("accessLimit")public JsonResponse testAccessLimit() throwsInterruptedException, ExecutionException {

System.out.println(newDate());//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(5));

List1 task1= newList1();

List2 task2= newList2();

List3 task3= newList3();

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= newHashMap();

map.put("aList",submit1.get());

map.put("bList",submit2.get());

map.put("cList",submit3.get());

System.out.println(newDate());return new JsonResponse(1,"success",map);

}class List1 implementsCallable{

@Overridepublic List call() throwsException {

List list1=getList1();//count.countDown();

returnlist1;

}

}class List2 implementsCallable{

@Overridepublic List call() throwsException {

List list2=getList2();//count.countDown();

returnlist2;

}

}class List3 implementsCallable{

@Overridepublic List call() throwsException {

List list3=getList3();//count.countDown();

returnlist3;

}

}

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

}publicList getList1() {try{

Thread.sleep(5000);

}catch(Exception e){

}

List arrayList= newArrayList();

arrayList.add(1);returnarrayList;

}publicList getList2(){try{

Thread.sleep(10000);

}catch(Exception e){

}

List arrayList= newArrayList();

arrayList.add(2);returnarrayList;

}publicList getList3(){try{

Thread.sleep(20000);

}catch(Exception e){

}

List arrayList= newArrayList();

arrayList.add(3);returnarrayList;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值