Runnable 传参异常 定义如下 线程
public class RankingSync implements Runnable {
private Dao dao;
private String url;
public RankingSync(Dao dao, String url) {
this.dao =dao;
this.url = url;
}
}
定义线程池
public synchronized void syncData() {
int maxpage = 132;
ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
.setNameFormat("ranking-data-%d").build();
//Common Thread Pool
ExecutorService pool = new ThreadPoolExecutor(8, 16,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(1024), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());
for(int i=0;i<=maxpage;i++){
StringBuffer url =new StringBuffer();
if(0==i){
url.append("https://xxx.com");
}else {
url.append("https://xxx.com"+ i +"-1-.htm?rank=&uprank=");
}
//ioc才能实现依赖注入
RankingSync rankingSync = new RankingSync(this.dao(),url.toString());
pool.execute(()->rankingSync.run());
}
pool.shutdown();
}
执行的url 出现重复 这个是为什么呢