package jingdong;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import view.Util.Util;
public class ThreadOne360buy extends Thread{
java.util.concurrent.CountDownLatch c;
ArrayList al;//记录秒杀产品页面
float price=0.0f;//商品价格
float discount=0.0f;//商品折扣
//用于保存线程信息,在这个项目里用处不大
private static List runningThreads = new ArrayList();
//这个是一个计数器(不太会用,线程这方面一直感觉是比较复杂的)
public ThreadOne360buy(java.util.concurrent.CountDownLatch c) {
this.c=c;
}
@Override
public void run() {
regist(this);// 线程开始时注册
// 打印开始标记
System.out.println(Thread.currentThread().getName() + "开始...");
try {
//抓取京东手机秒杀页面
this.getMessage("http://sale.360buy.com/act/8VTHFGr10CjMDyZ.html#01");
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
c.countDown();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
c.countDown();
}
c.countDown();
unRegist(this);// 线程结束时取消注册
// 打印结束标记
System.out.println(Thread.currentThread().getName() + "结束.");
}
public void regist(Thread t) {
synchronized (runningThreads) {
runningThreads.add(t);
}
}
public void unRegist(Thread t) {
synchronized (runningThreads) {
runningThreads.remove(t);
}
}
public static boolean hasThreadRunning() {
// 通过判断runningThreads是否为空就能知道是否还有线程未执行完
return (runningThreads.size() > 0);
}
/**
* 从手机秒杀页面获取 prodcut链接,product skuid,skuidkey,price,store信息
* @param url:手机秒杀页面
* @throws ClientProtocolException
* @throws IOException
*/
public void getMessage(String url) throws ClientProtocolException, IOException{
al=getMainUrl(down(url));
Util.println(al);
if(al.size()==0){