java如何统计文章阅读量_Java多线程刷取简书阅读量
导入hutool工具maven项目导入,下面三种导入方式cn.hutoolhutool-all5.4.0Gradle项目导入compile 'cn.hutool:hutool-all:5.5.0'image.png下面是实现代码import cn.hutool.http.HttpRequest;import cn.hutool.json.JSONUtil;import java.util.Hash
导入hutool工具
maven项目导入,下面三种导入方式
cn.hutool
hutool-all
5.4.0
Gradle项目导入
compile 'cn.hutool:hutool-all:5.5.0'
image.png
下面是实现代码
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class JianShuReadNum {
private static final int CORE_POOL_SIZE = 4;
private static final int MAX_POOL_SIZE = 10;
private static final int QUEUE_CAPACITY = 100;
private static final Long KEEP_ALIVE_TIME = 1L;
public static void main(String[] args) {
ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(
CORE_POOL_SIZE,
MAX_POOL_SIZE,
KEEP_ALIVE_TIME,
TimeUnit.SECONDS,
new ArrayBlockingQueue<>(QUEUE_CAPACITY),
new ThreadPoolExecutor.AbortPolicy()
);
String articleId = "7058fc449cb4";//文章id
//开启4个线程执行
for (int i = 0; i < CORE_POOL_SIZE; i++) {
poolExecutor.execute(() -> {
int num = 10000;//每个线程刷取的阅读量
for (int j = 0; j < num; j++) {
System.out.println(Thread.currentThread()+"====="+j);
Map map = new HashMap<>();
map.put("fuck",1);
HttpRequest.post("https://www.jianshu.com/shakespeare/notes/"+articleId+"/mark_viewed")
.header("Host", "www.jianshu.com")
.header("Origin", "https://www.jianshu.com")
.header("Referer", "https://www.jianshu.com/p/"+articleId)
.body(JSONUtil.toJsonStr(map), "application/json")
.execute();
}
});
}
poolExecutor.shutdown();
while (!poolExecutor.isTerminated()) {
}
System.out.println("线程执行完成");
}
}
代码中的articleId变量替换成你的文章id,不知道自己文章id的可以打开自己的文章,浏览器地址栏斜杠后的字符串就是你的文章id
image.png
点赞支持一下吧!
更多推荐



所有评论(0)