当前位置: 代码迷 >> J2SE >> 扫链接速度慢,该怎么解决
  详细解决方案

扫链接速度慢,该怎么解决

热度:74   发布时间:2016-04-24 02:07:48.0
扫链接速度慢
Java code
import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.IOException;import java.net.HttpURLConnection;import java.net.SocketTimeoutException;import java.net.URL;import java.util.concurrent.BlockingQueue;import java.util.concurrent.LinkedBlockingQueue;public class BookmarkChecker {    public static void main(final java.lang.String[] args) throws Exception {        BlockingQueue<URL> urls = new LinkedBlockingQueue<URL>();        String [] d=readFileByLines("C:/Documents and Settings/Owner/桌面/xx.txt");        for (int i=0;i<d.length-2;i++){             if(d[i]!=null){            urls.put(new URL(d[i]));             }else{                 break;             }            }          for(int i=0;i<100;i++)        new Thread( new CheckURLWorker(Thread.currentThread().getName(),urls)).start();          }    public static String[] readFileByLines(String fileName){           File file = new File(fileName);           String [] kk=new String[50000];           BufferedReader reader = null;           try {          //  System.out.println("以行为单位读取文件内容,一次读一整行:");            reader = new BufferedReader(new FileReader(file));            String tempString = null;            int line = 1;            //一次读入一行,直到读入null为文件结束            while ((tempString = reader.readLine()) != null){             //显示行号         //    System.out.println("line " + line + ": " + tempString);             kk[line]=tempString;             line++;            }            reader.close();           } catch (IOException e) {            e.printStackTrace();           } finally {            if (reader != null){             try {              reader.close();             } catch (IOException e1) {             }            }           }           return kk;}    private static class CheckURLWorker implements Runnable {        private BlockingQueue<URL> queue;        private String name;        public CheckURLWorker(String name,BlockingQueue<URL> q){            this.name = name;            this.queue = q;        }        public void run() {            for (URL url = queue.poll(); url != null; url = queue.poll()) {                try{                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();                    connection.setConnectTimeout(5000);                   connection.setReadTimeout(13000);                    connection.setRequestMethod("GET");                    connection.connect();                    int code = connection.getResponseCode();                    connection.disconnect();                    System.out.printf("[%s]%s:%d%n",name,url.toString(),code);                } catch (SocketTimeoutException e) {                    System.out.printf("[%s]%s:%d%n",name,url.toString(),-1);                } catch (IOException e) {                    System.err.println(e);                }            }        }    }}


运行很慢啊我扫3万个链接要一个小时(网速是一定的原因,我的网速100k不到)

------解决方案--------------------
最简单的,用时间打印语句看下每一步花费的时间 currentMillis
找出哪些步骤花费时间多再分析
------解决方案--------------------
探讨

我试了一下如果只有一个线程
connection.connect();
int code = connection.getResponseCode();

耗时1000毫秒内
线程开多了就耗的时间很长

------解决方案--------------------
探讨
引用:

最简单的,用时间打印语句看下每一步花费的时间 currentMillis
找出哪些步骤花费时间多再分析


connection.connect();
int code = connection.getResponseCode();
这个地方耗时间

------解决方案--------------------
楼主把for循环创建线程的循环次数,由100改到10试试,效果可能要好些。
操作系统虽然是支持多线程的,但是,平常也就300左右各线程在运行。
楼主一下增加了1/3倍的负载量,并且,大部分都是CPU空闲型的任务,综合效果并不会很强。


楼主说的慢,可能由以下几个原因造成的:
1.网络原因。比如,中间某些个节点延迟过高。(暂无解决方案)
2.系统原因。操作系统负载过大,响应过慢。(可减少系统负载)
3.程序原因。程序中出现瓶颈,造成效率低下。(更改算法)

当然,上述貌似都是废话。
实在的,就是说,
1.楼主建了100个线程,一般点的电脑,多跑这么多的线程,会使整个系统性能降低。
2.每个线程在执行每个任务的周期当中, 大部分时间,都在等待IO的数据响应。使得线程效率不高。
楼主可以考虑异步IO的通信方式,一个线程,维护多个链接。


  相关解决方案