日期:2014-05-20 浏览次数:20723 次
// 启动多个线程同时采集数据
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("=");