java http 并发_Java8并行http请求加快访问速度

背景

1.通常我们在获取到一个list列表后需要一个挨着一个的进行遍历处理数据,如果每次处理都需要长时间操作,那整个流程下来时间就是每一次处理时间的总和。

2.Java8的stream接口极大地减少了for循环写法的复杂性,stream提供了map/reduce/collect等一系列聚合接口,还支持并发操作:parallelStream。

例子

定义一个位置类和服务,其中创建10个地址位置

package cn.chinotan.dto;

/**

* @program: test

* @description: 位置

* @author: xingcheng

* @create: 2018-11-17 19:36

**/

public class Location {

String name;

public Location() {

}

public Location(String name) {

this.name = name;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

}

package cn.chinotan.controller;

import cn.chinotan.dto.Location;

import org.apache.commons.lang3.StringUtils;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

import java.util.List;

/**

* @program: test

* @description: 位置服务

* @author: xingcheng

* @create: 2018-11-17 19:42

**/

@RestController

public class LocationController {

@GetMapping("/location")

public List getLocations() {

List locations = new ArrayList<>();

for (int i = 0; i < 10; i++) {

locations.add(new Location(StringUtils.join("London", i)));

}

return locations;

}

}

定义一个温度类和服务,其中温度值介于30到50之间。 在实现中添加500 ms的延迟以模拟耗时操作

package cn.chinotan.dto;

/**

* @program: test

* @description: 温度

* @author: xingcheng

* @create: 2018-11-17 19:35

**/

public class Temperature {

private Double temperature;

private String scale;

public Double getTemperature() {

return temperature;

}

public void setTemperature(Double temperature) {

this.temperature = temperature;

}

public String getScale() {

return scale;

}

public void setScale(String scale) {

this.scale = scale;

}

}

package cn.chinotan.controller;

import cn.chinotan.dto.Temperature;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RestController;

import java.util.Random;

/**

* @program: test

* @description: 温度服务

* @author: xingcheng

* @create: 2018-11-17 19:45

**/

@RestController

public class TemperatureController {

@GetMapping("/temperature/{city}")

public Temperature getAverageTemperature(@PathVariable("city") String city) {

Temperature temperature = new Temperature();

temperature.setTemperature((double) (new Random().nextInt(20) + 30));

temperature.setScale("Celsius");

try {

Thread.sleep(500);

} catch (InterruptedException ignored) {

}

return temperature;

}

}

定义一个天气预报类和响应

package cn.chinotan.dto;

/**

* @program: test

* @description: 天气预报

* @author: xingcheng

* @create: 2018-11-17 19:37

**/

public class Forecast implements Comparab

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值