下面是例子,代码不是很多,看一下,然后,按自己的要求修改就可以使用了。
public class JudgeURLJob {
private int threadNum = 8;
public JudgeURLJob(int nThread) {
threadNum = nThread;
}
private List<String> getTask(int currentThreadIndex,
List<String> urls) {
if (urls.size() < threadNum) {
threadNum = 1;
}
int avgNum = urls.size() / threadNum;
List<String> li = new ArrayList<String>();
if (currentThreadIndex == (threadNum - 1)) {
for (int i = currentThreadIndex * avgNum; i < urls.size(); i++) {
li.add(urls.get(i));
}
} else {
for (int i = currentThreadIndex * avgNum; i < (currentThreadIndex + 1)
* avgNum; i++) {
li.add(urls.get(i));
}
}
return li;
}
public List<String> doJob(List<String> urls) {
List<String> invalidURLs = new ArrayList<String>();
Future<List<String>>[] result = new Future[threadNum];
List<String> iht = null;
System.out.println("urls:" + urls.size());
try {
ExecutorService pool = Executors.newFixedThreadPool(threadNum);
// 给线程分配任务
for (int i = 0; i < threadNum; i++) {
List<String> sublst = getTask(i, urls);
result[i] = pool.submit(new JudgeURLThread(sublst));
}
for (int i = 0; i < threadNum; i++) {
iht = result[i].get();
invalidURLs.addAll(iht);
}
pool.shutdownNow();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
return invalidURLs;
}
}
public class JudgeURLThread implements Callable<List<String>>
{
private List<String> urls = new ArrayList<String>();
private static int i = 0;
public JudgeURLThread(List<String> urls)
{
this.urls = urls;
}
public List<String> call() throws Exception
{
List<String> badUrls = new ArrayList<String>();
HttpURLConnection con = null;
if (urls == null || urls.size() < 1)
{
throw new Exception("imageList must not be empty! please check the input parameter of" + this.getClass());
}
for (String url : urls)
{
try{
String[] strings = url.split(";;;;;@@@@@");
HttpURLConnection.setFollowRedirects(false);
con = (HttpURLConnection) new URL(strings[1]).openConnection();
con.setRequestMethod("HEAD");
if(con.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND){
System.out.println("错误URL:" + strings[1]);
badUrls.add(strings[0]);
}
System.out.println(++i);
}catch(Exception e){
e.printStackTrace();
}
}
return badUrls;
}
}
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- kqyc.cn 版权所有 赣ICP备2024042808号-2
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务