マルチスレッドWebページへのアクセス+高同時テストWebサイト

4454 ワード

マルチスレッドアクセスWebページ+高同時テストWebページ
学習用のみ、不正な用途には使用しないでください.  
スレッドクラスは次のとおりです.
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

2つ目の方法:
package com.yjf.soso;   import java.io.IOException; import java.io.InputStream; import java.io.PrintStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.Scanner;  public class Shua   implements Runnable {   private static String strUrl = null;   private static long numL = 0L;   private static Scanner scanner = new Scanner(System.in);    public void run() {     HttpURLConnection connection = null;     InputStream urlStream = null;     URL url = null;     while (true)       try {         url = new URL(strUrl + numL);          connection = (HttpURLConnection)url.openConnection();         connection.connect();         urlStream = connection.getInputStream();         if (urlStream != null) {           numL += 1L;           urlStream.close();           System.out.print("\t" + numL);           if (numL % 30L == 0L) {             System.out.println();           }         }         Thread.sleep(10L); } catch (InterruptedException e) {         while (true) {           e.getMessage();           try {             Thread.sleep(1000L); } catch (InterruptedException ie) {ie.printStackTrace(); }         }                }       catch (MalformedURLException e)       {         e.printStackTrace();       }       catch (IOException e) {         e.printStackTrace();       }   }    public static void main(String[] args) throws MalformedURLException   {     System.out.println("");     int threadNum;     while (true)     {       System.out.println(" :");       threadNum = scanner.nextInt();       System.out.println(" :");       String str = scanner.next();        if ((!(str.startsWith("http://"))) && (!(str.startsWith("https://")))) {         strUrl = "http://" + str;         System.out.println(strUrl);       } else {         strUrl = str;       }        if (str.indexOf("?") >= 0)         strUrl += "&num=";       else {         strUrl += "?num=";       }       System.out.println("--------------------------------------");       System.out.println(" :" + threadNum);       System.out.println(" " + str);       System.out.println(" (Y/N):");       String tmp = scanner.next();       if ("Y".equalsIgnoreCase(tmp))         break;       if ("N".equalsIgnoreCase(tmp)) {         continue;       }       System.out.println(" , (Y/N):");     }      for (int i = 0; i < threadNum; ++i) {     	Shua at = new Shua();       Thread t = new Thread(at);       t.start();     }   } }

転載先:https://blog.51cto.com/yjflinchong/1165008