62,614
社区成员
发帖
与我相关
我的任务
分享
synchronized(syncqURL)
{
et=s_qURL.poll();
if(et==null)
{
System.out.println(thread.getId()+"is on requiring URL failed");
thread.SetIdle();
if(DownloadThread.AskForIdle())
{
DownloadThread.InterruptAll();
}
else
{
syncqURL.wait();
continue;
}
}
}
synchronized(syncqURL)
{
s_qURL.add(et);
DownloadThread.NotifyAll();
syncqURL.notifyAll();
}
public static void RunDownloadThread(DownloadThread thread) throws InterruptedException//run函数里面的类静态函数
{
while(!DownloadThread.GetisQuit())
{
System.out.println(thread.getId()+"is on requiring URL");
ElementType et;
synchronized(syncqURL)
{
et=s_qURL.poll();
if(et==null)
{
System.out.println(thread.getId()+"is on requiring URL failed");
thread.SetIdle();
if(DownloadThread.AskForIdle())
{
DownloadThread.InterruptAll();
}
else
{
syncqURL.wait();
continue;
}
}
}
if(et!=null)
{
System.out.println(thread.getId()+"is on requiring URL succeed");
synchronized(syncMap)
{
if(s_map.containsKey(et.url)==false)
{
s_map.put(et.url,et);
}
else
{
continue;
}
}
DownLoadPage(et);
System.out.println(et.url+" "+et.title+" "+et.deep+"线程ID"+thread.getId());
}
}
}
private static boolean DownLoadPage(ElementType et)
{
try
{
System.out.println(Thread.currentThread().getId()+"is on downloading page");
URL url=new URL(et.url);
URLConnection connection=url.openConnection();
connection.connect();
Scanner scanner=new Scanner(connection.getInputStream());
StringBuffer str=new StringBuffer();
System.out.println(Thread.currentThread().getId()+"is on save String");
for(;scanner.hasNextLine();)
{
str.append(scanner.nextLine());
str.append("\n");
}
//需要统一编码
et.title=FindTitle(str);
if(et.deep<1)
{
AnalyzeStream(str,et.deep);
}
return true;
}
catch(IOException ex)
{
}
return false;
}
private static void AnalyzeStream(StringBuffer str,int deep)
{
System.out.println(Thread.currentThread().getId()+"is on analyzing String");
for(int curPos=str.indexOf(s_matchLink,0);curPos!=-1;curPos=str.indexOf(s_matchLink,curPos))
{
String temp=new String();
curPos+=s_matchLink.length();
int endPos=str.indexOf("\"", curPos);
temp=str.substring(curPos, endPos);
if(temp.indexOf("http")!=-1&&temp.indexOf("qq")!=-1)
{
ElementType et=new ElementType();
et.url=temp;
et.deep=deep+1;
synchronized(syncqURL)
{
s_qURL.add(et);
DownloadThread.NotifyAll();
syncqURL.notifyAll();
}
}
}
}
public bool bNotified = false;
//...
else
{
bNotified = false;
while(!bNotified){
try{
Thread.sleep(1000);
}catch(ThreadInterruptedException e){
break;
}
}
}