先用java实现HTTP访问网页
public class HttpCon {
public void Test(String url)
{
StringBuffer html=new StringBuffer();
URL url1=null;
URLConnection uc=null;
BufferedReader br=null;
InputStreamReader isr=null;
//创建URL对象
try {
//创建URL链接对象
url1= new URL(url);
//打开链接
uc= url1.openConnection();
//获取输入流
isr=new InputStreamReader(uc.getInputStream(), "utf-8");
//建立缓冲流
br=new BufferedReader(isr);
//建立临时字符串
String temp =null;
while((temp=br.readLine())!=null)
{
html.append(temp);//将读取的数据存入html中
}
System.out.println(html);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(isr!=null)
{
try {
isr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
写一个用于访问的线程
class MyThread extends Thread
{
public void run()
{
while(true)
{
new HttpCon().Test("URL");
}
}
}
在Main函数中调用启动这个线程
public static void main(String[] args) {
MyThread mt=new MyThread();
mt.start();
}
我们来试试并发3000个线程
public static void main(String[] args) {
for(int i=0;i<3000;i++)
{
MyThread mt=new MyThread();
mt.start();
}
}
閱讀更多 騎著豬豬的CodeMonkey 的文章