您的位置:首页 > 运维架构 > 网站架构

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

2012-06-06 09:22 211 查看
多线程访问网页+高并发测试网页
仅供学习,请勿用于非法用途。

线程类如下

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


第二种办法:

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


本文出自 “无证程序猿” 博客,请务必保留此出处http://yjflinchong.blog.51cto.com/6851233/1165008
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: