62,620
社区成员
发帖
与我相关
我的任务
分享
import java.io.*;
import java.net.*;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Timer;
import java.util.TimerTask;
public class TestGetHtml {
public static void main(String[] args) throws IOException {
// while(获取下一个连接,直到所有链接处理完毕,此处忽略){
String urllink1 = "http://www.baidu.com"; // for test only
while(true) //读取网页,指导成功为止
{
// 启动读线程
Thread thread = new MyReadThread(urllink1);
thread.start();
// 启动定时器, 我不知道定时器怎样通知主线程 ????????????
MyTimer timer = new MyTimer(1000); //设定超时时间,自己调整测试
timer.start();
// 等待读线程
try{
thread.join(); // 用join 等待可能不太好吧,但我不知道用什么方法。 ???????????
}
catch(InterruptedException e)
{
e.printStackTrace();
}
// 如果定时器超时,主线程要重新执行该循环,但我不知主线程怎样接受定时器消息 ?????????
// 如果读网页成功,则cancel 定时器,
System.out.println("Read html OK");
timer.stop();
break; //正常结束了,可以退出循环
}
// 忽略}
}
}
class MyReadThread extends Thread
{
private static String urlName;
private static URL url;
private static URLConnection connection;
private static String type;
MyReadThread(String s)
{
urlName = s;
type = null;
url = null;
connection = null;
}
@Override
public void run()
{
System.out.println("connentting...");
connect(urlName);
System.out.println("reading...");
readContents();
}
static void connect( String urlString ) {
try {
url = new URL(urlString);
connection = url.openConnection();
//System.out.println(connection.getClass());
} catch (MalformedURLException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
static void readContents() {
BufferedReader reader = null;
try {
reader = new BufferedReader( new InputStreamReader( connection.getInputStream()));
String inputLine;
while ( (inputLine = reader.readLine()) != null)
{
System.out.println(inputLine);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class MyTimer
{
int miliseconds;
Timer timer;
MyTimer(int miliseconds)
{
this.miliseconds = miliseconds;
}
public void start()
{
class MyTimerTask extends TimerTask{
@Override
public void run()
{
System.out.println("ha ha! time over .... reconnecting ...");
timer.cancel();
}
}
timer = new Timer();
timer.schedule(new MyTimerTask(), miliseconds);
System.out.println("start timer "+miliseconds);
}
public void stop()
{
// System.out.println("want stop timer");
timer.cancel(); // when has receive ok
}
}
class ShareObject
{
private boolean timeover;
private boolean readhtmlok;
ShareObject()
{
timeover = false;
readhtmlok = false;
}
public synchronized void TimerOver()
{
timeover = true;
notifyAll(); // 通知已经设好
}
public synchronized void ReadHtmlOK()
{
readhtmlok = true;
notifyAll(); // 通知已经设好
}
public synchronized boolean GetReadResult()
{
if((timeover == false) && (readhtmlok == false))
{
try{
wait(); //等待完成
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
if(readhtmlok == true)
{
return true;
}
else if(timeover == true)
{
return false; //read time over
}
else
{
System.out.println("I don't think will got here!");
}
return false;
}
}