例子1
ForkJoinPool forkJoinPool = new ForkJoinPool(10,
ForkJoinPool.defaultForkJoinWorkerThreadFactory,
null,
true);
List list = new ArrayList();
try {
forkJoinPool.submit(() ->
list.parallelStream().forEach(e->{
System.out.println(e);
})).get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
例子2
package com.qinqiu.repository;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
@Slf4j
public class Test {
public static void main(String [] args){
Listtest = new ArrayList<>();
test.add(1);
test.add(2);
ExecutorService executor = Executors.newFixedThreadPool(10);
List> futures = test.stream()
.map(t -> CompletableFuture.supplyAsync(() -> test(t), executor))
.collect(Collectors.toList());
Listresult =
futures.stream()
.map(CompletableFuture::join)
.collect(Collectors.toList());
log.info("result into:{}", JSON.toJSONString(result));
}
private static Integer test(Integer integer){
log.info("test enter into this:{}", integer);
return integer;
}
}
redis限流
// 这个不准
/* private static final String LIMIT_LUA_1 = "" +
"local is_exists = redis.call(\"EXISTS\", KEYS[1])\n" +
"if is_exists == 1 then\n" +
" if redis.call(\"INCR\", KEYS[1]) > tonumber(ARGV[1]) then\n" +
" return 0\n" +
" else\n" +
" return 1\n" +
" end\n" +
"else\n" +
" redis.call(\"SET\", KEYS[1], 1)\n" +
" redis.call(\"EXPIRE\", KEYS[1], ARGV[2])\n" +
" return 1\n" +
"end";*/
private static final String LIMIT_LUA = "local limit = tonumber(ARGV[1])\n" +
"local expire_time = ARGV[2] \n" +
"local result = redis.call('SETNX',KEYS[1],1); \n" +
"if result == 1 then\n" +
" redis.call('expire',KEYS[1],expire_time)\n" +
" return 1\n" +
"else\n" +
" if tonumber(redis.call(\"GET\", KEYS[1])) >= limit then\n" +
" return 0\n" +
" else\n" +
" redis.call(\"INCR\", KEYS[1])\n" +
" return 1\n" +
" end\n" +
"end";
/**
* IP限流
*
* @param key
* @param limit
* @param timeout
* @return
* @throws IOException
*/
public boolean accessLimit(String key, int limit, int timeout) {
Listkeys = Collections.singletonList(key);
Listargv = Arrays.asList(String.valueOf(limit), String.valueOf(timeout));
Long result = redisTemplate.execute((RedisCallback) connection -> {
Object nativeConnection = connection.getNativeConnection();
//分集群和单机模式(JedisCluster 和 Jedis的eval方法没有共同父接口)
if (nativeConnection instanceof JedisCluster) {
return (Long) ((JedisCluster) nativeConnection).eval(LIMIT_LUA, keys, argv);
} else if (nativeConnection instanceof Jedis) {
return (Long) ((Jedis) nativeConnection).eval(LIMIT_LUA, keys, argv);
}
return 0L;
});
System.out.println(key + "," + result);
return 1 == result;
}
应用实战
@Test
void baiduExpressQueryTrace() {
int count = 0;
ForkJoinPool forkJoinPool = new ForkJoinPool(5,
ForkJoinPool.defaultForkJoinWorkerThreadFactory,
null,
true);
Listlist = new ArrayList();
list.add("JDVC11867842730");
list.add("JDVC11867842730");
list.add("JDVC11867842730");
list.add("JDVC11867842730");
list.add("JDVC11867842730");
list.add("JDVC11867842730");
list.add("JDVC11867842730");
list.add("JDVC11867842730");
list.add("JDVC11867842730");
list.add("JDVC11867842730");
while (true) {
try {
forkJoinPool.submit(() ->
list.parallelStream().forEach(e -> {
System.out.println(e);
BaseTraceSearchDTO baseTraceSearchDTO = new BaseTraceSearchDTO();
ListwayBillNos = new ArrayList<>();
wayBillNos.add(e);
baseTraceSearchDTO.setWayBillNos(wayBillNos);
baiDuCommonService.queryWaybillTrace(baseTraceSearchDTO);
})).get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
/* try {
String waybillNo = "JDVC11867842730";
String expressCompanyCode = "";
BaseExpressRequestEntityrequestEntity = new BaseExpressRequestEntity<>();
requestEntity.setParam(waybillNo);
BaiDuInfo baiDuInfo = baiDuAbility.queryWaybillTrace(requestEntity, expressCompanyCode);
log.info("{},从百度获取物流轨迹:{}", count, baiDuInfo == null ? "null" : JSON.toJSONString(baiDuInfo.getContext()));
BaseTraceSearchDTO baseTraceSearchDTO = new BaseTraceSearchDTO();
ListwayBillNos = new ArrayList<>();
wayBillNos.add(waybillNo);
baseTraceSearchDTO.setWayBillNos(wayBillNos);
baiDuCommonService.queryWaybillTrace(baseTraceSearchDTO);
log.info("{}", JSON.toJSONString(list));
count++;
} catch (Exception ex) {
log.error("异常", ex);
}*/
}