• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Is thread pooling faster than non pooling?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
}
}
}};
 
Trailboss
Posts: 23778
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
PROPER NAMES ARE NOW REQUIRED!
See http://www.javaranch.com/ubb/Forum10/HTML/000180.html for details.
 
You may have just won ten million dollars! Or, maybe a tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic