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

来源:互联网 发布:淘宝类目选错了 编辑:程序博客网 时间:2024/05/14 08:01

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

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


线程类如下


[java] view plaincopy
  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. }  


启动类如下


[java] view plaincopy
  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. }  



第二种办法:

[java] view plaincopy
  1. package com.yjf.soso;  
  2.   
  3.   
  4. import java.io.IOException;  
  5. import java.io.InputStream;  
  6. import java.io.PrintStream;  
  7. import java.net.HttpURLConnection;  
  8. import java.net.MalformedURLException;  
  9. import java.net.URL;  
  10. import java.util.Scanner;  
  11.   
  12. public class Shua  
  13.   implements Runnable  
  14. {  
  15.   private static String strUrl = null;  
  16.   private static long numL = 0L;  
  17.   private static Scanner scanner = new Scanner(System.in);  
  18.   
  19.   public void run() {  
  20.     HttpURLConnection connection = null;  
  21.     InputStream urlStream = null;  
  22.     URL url = null;  
  23.     while (true)  
  24.       try {  
  25.         url = new URL(strUrl + numL);  
  26.   
  27.         connection = (HttpURLConnection)url.openConnection();  
  28.         connection.connect();  
  29.         urlStream = connection.getInputStream();  
  30.         if (urlStream != null) {  
  31.           numL += 1L;  
  32.           urlStream.close();  
  33.           System.out.print("\t" + numL);  
  34.           if (numL % 30L == 0L) {  
  35.             System.out.println();  
  36.           }  
  37.         }  
  38.         Thread.sleep(10L); } catch (InterruptedException e) {  
  39.         while (true) {  
  40.           e.getMessage();  
  41.           try {  
  42.             Thread.sleep(1000L); } catch (InterruptedException ie) {ie.printStackTrace(); }  
  43.         }  
  44.           
  45.       }  
  46.       catch (MalformedURLException e)  
  47.       {  
  48.         e.printStackTrace();  
  49.       }  
  50.       catch (IOException e) {  
  51.         e.printStackTrace();  
  52.       }  
  53.   }  
  54.   
  55.   public static void main(String[] args) throws MalformedURLException  
  56.   {  
  57.     System.out.println("");  
  58.     int threadNum;  
  59.     while (true)  
  60.     {  
  61.       System.out.println("请输入要生成的线程数:");  
  62.       threadNum = scanner.nextInt();  
  63.       System.out.println("请输入网址:");  
  64.       String str = scanner.next();  
  65.   
  66.       if ((!(str.startsWith("http://"))) && (!(str.startsWith("https://")))) {  
  67.         strUrl = "http://" + str;  
  68.         System.out.println(strUrl);  
  69.       } else {  
  70.         strUrl = str;  
  71.       }  
  72.   
  73.       if (str.indexOf("?") >= 0)  
  74.         strUrl += "&num=";  
  75.       else {  
  76.         strUrl += "?num=";  
  77.       }  
  78.       System.out.println("--------------------------------------");  
  79.       System.out.println("线程数:" + threadNum);  
  80.       System.out.println("地址" + str);  
  81.       System.out.println("请再次确认(Y/N):");  
  82.       String tmp = scanner.next();  
  83.       if ("Y".equalsIgnoreCase(tmp))  
  84.         break;  
  85.       if ("N".equalsIgnoreCase(tmp)) {  
  86.         continue;  
  87.       }  
  88.       System.out.println("输入错误,请重新输入(Y/N):");  
  89.     }  
  90.   
  91.     for (int i = 0; i < threadNum; ++i) {  
  92.         Shua at = new Shua();  
  93.       Thread t = new Thread(at);  
  94.       t.start();  
  95.     }  
  96.   }  
  97. }  

0 0
原创粉丝点击