多线程访问网页+高并发测试网站

多线程访问网页+高并发测试网页

仅供学习,请勿用于非法用途。


线程类如下


import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class TestThread extends Thread{
    
    private String httpurl;
    
    public TestThread(String httpurl){
        this.httpurl = httpurl;
    }
    
    public void run() {
        HttpURLConnection connection = null;
        InputStream urlStream = null;
        URL url = null;
          try {
            url = new URL(httpurl);

            connection = (HttpURLConnection)url.openConnection();
            connection.connect();
            urlStream = connection.getInputStream();
            Thread.sleep(10L); } catch (InterruptedException e) {
          }
          catch (MalformedURLException e)
          {
            e.printStackTrace();
          }
          catch (IOException e) {
            e.printStackTrace();
          }
      }
}


启动类如下


public class Test {
	
	public static void main(String[] args) {
		while (true) {
			//仅供学习请勿用于非法用途
			startThead();
		}
	}
	
	public static int threadCount = 55;   //线程数量
	private static boolean isRunGrabThread = true;     //抓取线程是否执行完毕
	static int  num = 1; //动态参数
	
	public static void startThead(){
		Thread[] grabThreads= new Thread[threadCount];	
		try{		  
			//开启-数据抓取子线程
		  	for(int i=0;i<grabThreads.length;i++){
		  		int numL = num*100;
		  		System.out.println(numL);
		  		String url = "http url ?id=" + numL;
			  	Thread searchThread=new TestThread(url);
			  	num++;
		  		grabThreads[i] = searchThread;
		  		grabThreads[i].setDaemon(true);
		  		grabThreads[i].start();
		  	}
			
		  	isRunGrabThread = true;
		  	
		  	//监控子线程,全部完成程序退出
		    WhileLoop:		    			    	
		    	
		    while(true){
		    	
		    	//拨号策略控制
		    	//reconnectControl();	
		    	
				//判断子线程是否运行完成
		    	for(int i=0;i<threadCount;i++){
		    		if(grabThreads[i].isAlive()){
		    			Thread.sleep(10);
		    			continue WhileLoop;
		    		}
		    	}

		    	//所有抓取线程执行完毕
		    	isRunGrabThread = false;
		    	
		    	//子线程执行完毕则退出
		    	break;
		    }
		} 
		catch (Exception e) {
			System.out.println("main主线程--系统异常!");
		}		
	}

}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值