threads and httpconnection
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   kyo_rugal
Posted On:   Thursday, July 11, 2002 01:01 AM

Hi,how can i create 10 threads that make 10 httpconnections simultaneously?

thanks

Re: threads and httpconnection

Posted By:   Gautam_Marwaha  
Posted On:   Thursday, July 11, 2002 01:44 AM

Have a look at this:

import java.io.*;
import java.net.*;

public class HttpStuff
{
private int maxConcurrent = 10;

public static void main(String[] args)
{
if (args.length == 0)
{
System.out.println("Usage: java HttpStuff ");
System.exit(0);
}
HttpStuff h = new HttpStuff();
h.doIt(args[0]);
}

public void doIt(String strUrl)
{
URL url = null;
try
{
url = new URL(strUrl);
}
catch(MalformedURLException me)
{
me.printStackTrace();
}

Worker w = null;
for (int i = 0; i < maxConcurrent; i++)
{
w = new Worker(url);
w.start();
}
}

class Worker extends Thread
{
URL url;

Worker(URL url)
{
this.url = url;
}

public void run()
{
URLConnection urlConn = null;
//PrintWriter out = null;
BufferedReader in = null;
String str = null;

try
{
urlConn = url.openConnection();

//urlConn.setDoOutput(true);
//out = new PrintWriter(urlConn.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));

while ((str = in.readLine()) != null)
{
System.out.println(str);
}
}
catch (IOException e)
{
e.printStackTrace();
}

try
{
//out.close();
in.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}

About | Sitemap | Contact