打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
多线程访问网页 高并发测试网站
多线程访问网页+高并发测试网站
2012-06-06 14:17:14     我来说两句      
收藏   
我要投稿

多线程访问网页+高并发测试网页
仅供学习,请勿用于非法用途。  

线程类如下

[java]
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(); 
          } 
      } 

启动类如下

[java]
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主线程--系统异常!"); 
        }        
    } 
 

 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
android 封装抓取网页信息的实例代码
多线程高并发【实战开发注意】
5天不再惧怕多线程——第一天 尝试Thread
JAVA多线程同步wait、notify、synchronized
java多线程并发访问解决方案
关于Java多线程实现生产者和消费者的问题
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服