日期:2014-05-20  浏览次数:20723 次

多线程从sina网采集股票实时数据
小弟从sina网采集股票的实时数据,写了一个多线程的采集,但总有些股票数据采集失败,如果一条一条的采集是没有问题的,不知道下面代码和方式那些存在问题,希望高手指点指点。

            // 启动多个线程同时采集数据
            for (int i = 0; i < totalThreadAmount; i++) {
                GetDataThread getDataThread = new GetDataThread();
                new Thread(getDataThread).start();
            }


    class GetDataThread implements Runnable {

        public GetDataThread() {
        }

        @Override
        public void run() {
            URL url = null;
            HttpURLConnection conn = null;
            String curUrl = "";

            while (!sinaGetDataUrls.isEmpty()) {

                curUrl = (String) sinaGetDataUrls.get(0);
                sinaGetDataUrls.remove(curUrl);

                getedCount++;

                try {
                    url = new URL(curUrl);
                    conn = (HttpURLConnection) url.openConnection();

                    conn.setUseCaches(false);
                    //设定请求方式为GET
                    conn.setRequestMethod("GET");
                    //一定要设为true,因为要发送数据
                    conn.setDoOutput(true);
                    conn.setDoInput(true);

                    conn.connect();
                    // 设置连接主机超时
                    conn.setConnectTimeout(50000);
                    // 设置从主机读取数据超时
                    conn.setReadTimeout(50000);

                    BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "gbk"));
                    String s = "";
                    while ((s = br.readLine()) != null) {

                            //此处采集数据省略...

                            krStockRealtimeDataDAO.save(krStockRealtimeData);

                        }
                        txCurrentMakeCount.setText(" 已采集股票数:" + getedCount + " 未采集数:" + (stockTotal - getedCount));
                    }
                } catch (Exception e) {
                    int startPosition = curUrl.indexOf("=");