hi!
i am working with search engine.i have tried out an example, in which i started a
thread initially and then passed the url it is showing a magnificant difference?ahy is it so.the code folloes here:
public class ThreadTest1 {
public synchronized static void main(
String args[]) throws MalformedURLException
{
ThreadTest1 t=new ThreadTest1();
}
public ThreadTest1() throws MalformedURLException
{
PStartTime=System.currentTimeMillis();
PrimeThread p=new PrimeThread(new URL ("http://www.webveda.com"));
p.start();
try{
p.join();
}
catch(Exception e)
{
System.out.println("SSS");
}
PEndTime=System.currentTimeMillis();
SecondThread s=new SecondThread();
Thread st=new Thread(s);
st.start();
s.setURL(new URL("http://webveda.com/index.html"),true);
//s.setURL(new URL("http://yahoo.com/index.html"),true);
//s.setURL(new URL("http://webcrawler/index.html"),true);
//s.setURL(new URL
try{
//notifyAll();
//st.join();
}
catch(Exception e)
{
System.out.println("SSS "+e);
}
try{
//notifyAll();
System.out.println("sleeping");
Thread.currentThread().sleep(5000);
}
catch(Exception e)
{
System.out.println("SSS "+e);
}
}
};
class PrimeThread extends Thread
{
URL url;
URLConnection con=null;
int k;
public PrimeThread(URL url)
{
this.url=url;
}
public void run()
{
try
{
con=url.openConnection();
BufferedReader instream=new BufferedReader(new InputStreamReader(con.getInputStream()));
String result="";
StringBuffer finalresult=new StringBuffer();
while((result=instream.readLine())!=null)
{
finalresult.append(result);
}
}
catch(Exception ex)
{
System.out.println(ex.toString());
}
}
};
class SecondThread implements Runnable
{
URL[] url=new URL[10];
boolean flag=false;
URLConnection con=null;
int j;
public synchronized void setURL(URL url,boolean flag)
{
this.url[j]=url;
this.flag=flag;
try{
notifyAll();
// st.join();
}
catch(Exception e)
{
System.out.println("SSS "+e);
}
System.out.println("jin set url end");
}
public synchronized void run()
{
System.out.println("in run");
while(true)
{System.out.println("in while");
if(!flag)
{try{
System.out.println("in flag");
wait();
System.out.println("in waiting");
}
catch(Exception e){
System.out.println("erroer here " +e);
}
}
try
{
int i=0;
long l=System.currentTimeMillis();
System.out.println("in try block");
con=url[i].openConnection();
BufferedReader instream=new BufferedReader(new InputStreamReader(con.getInputStream()));
String result="";
StringBuffer finalresult=new StringBuffer();
while((result=instream.readLine())!=null)
{
finalresult.append(result);
}
flag=false;
long z=System.currentTimeMillis();
System.out.println(+z-l +"the difference is");
}
catch(Exception ex)
{
System.out.println(ex.toString());
flag=false;
}
}
}};