码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-07-11 09:10:15      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:java

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

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


线程类如下


  1. import java.io.IOException;  
  2. import java.io.InputStream;  
  3. import java.net.HttpURLConnection;  
  4. import java.net.MalformedURLException;  
  5. import java.net.URL;  
  6.   
  7. public class TestThread extends Thread{  
  8.       
  9.     private String httpurl;  
  10.       
  11.     public TestThread(String httpurl){  
  12.         this.httpurl = httpurl;  
  13.     }  
  14.       
  15.     public void run() {  
  16.         HttpURLConnection connection = null;  
  17.         InputStream urlStream = null;  
  18.         URL url = null;  
  19.           try {  
  20.             url = new URL(httpurl);  
  21.   
  22.             connection = (HttpURLConnection)url.openConnection();  
  23.             connection.connect();  
  24.             urlStream = connection.getInputStream();  
  25.             Thread.sleep(10L); } catch (InterruptedException e) {  
  26.           }  
  27.           catch (MalformedURLException e)  
  28.           {  
  29.             e.printStackTrace();  
  30.           }  
  31.           catch (IOException e) {  
  32.             e.printStackTrace();  
  33.           }  
  34.       }  
  35. }  


启动类如下


  1. public class Test {  
  2.       
  3.     public static void main(String[] args) {  
  4.         while (true) {  
  5.             //仅供学习请勿用于非法用途  
  6.             startThead();  
  7.         }  
  8.     }  
  9.       
  10.     public static int threadCount = 55;   //线程数量  
  11.     private static boolean isRunGrabThread = true;     //抓取线程是否执行完毕  
  12.     static int  num = 1//动态参数  
  13.       
  14.     public static void startThead(){  
  15.         Thread[] grabThreads= new Thread[threadCount];    
  16.         try{            
  17.             //开启-数据抓取子线程  
  18.             for(int i=0;i<grabThreads.length;i++){  
  19.                 int numL = num*100;  
  20.                 System.out.println(numL);  
  21.                 String url = "http url ?id=" + numL;  
  22.                 Thread searchThread=new TestThread(url);  
  23.                 num++;  
  24.                 grabThreads[i] = searchThread;  
  25.                 grabThreads[i].setDaemon(true);  
  26.                 grabThreads[i].start();  
  27.             }  
  28.               
  29.             isRunGrabThread = true;  
  30.               
  31.             //监控子线程,全部完成程序退出  
  32.             WhileLoop:                                
  33.                   
  34.             while(true){  
  35.                   
  36.                 //拨号策略控制  
  37.                 //reconnectControl();     
  38.                   
  39.                 //判断子线程是否运行完成  
  40.                 for(int i=0;i<threadCount;i++){  
  41.                     if(grabThreads[i].isAlive()){  
  42.                         Thread.sleep(10);  
  43.                         continue WhileLoop;  
  44.                     }  
  45.                 }  
  46.   
  47.                 //所有抓取线程执行完毕  
  48.                 isRunGrabThread = false;  
  49.                   
  50.                 //子线程执行完毕则退出  
  51.                 break;  
  52.             }  
  53.         }   
  54.         catch (Exception e) {  
  55.             System.out.println("main主线程--系统异常!");  
  56.         }         
  57.     }  
  58.   
  59. }  

版权声明:本文为博主http://www.zuiniusn.com 原创文章,未经博主允许不得转载。

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

标签:java

原文地址:http://blog.csdn.net/u013141940/article/details/46838961

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!