主题 : Java开源CMS系统--JEECMS v3.0.2正式版系统安装包下载帖 |
级别: 白丁
![]() |
801# 发表于:2011-01-06 21:13:50 IP:113.14.*.*
|
||||
---|---|---|---|---|---|
级别: 白丁
![]() |
806# 发表于:2011-01-06 21:32:27 IP:61.247.*.*
package com.eteda.test;
import java.net.HttpURLConnection; import java.io.IOException; import java.net.URL; import java.io.InputStream; import java.io.BufferedReader; import java.io.InputStreamReader; public class TomcatTest { private static String url = "http://10.226.144.175:8080/hello.jsp"; private static Integer error = 0; private static Integer threads = 800; private static Long startTime; private static Long totleTime; private static long end = 0; private static class WorkThread extends Thread { public void run() { startTime = System.currentTimeMillis(); synchronized (threads) { try { URL u = new URL(url); HttpURLConnection urlConn = (HttpURLConnection) u .openConnection(); urlConn.setUseCaches(false); urlConn.setRequestProperty("Content-type", "text/html; charset=UTF-8"); urlConn.connect(); InputStream is = urlConn.getInputStream(); StringBuffer buffer = new StringBuffer(); readToBuffer(buffer, is); end = System.currentTimeMillis(); totleTime = end - startTime; } catch (Exception e) { error++; // return; e.printStackTrace(); // return; } threads--; System.out.printf("还有%d个未完线程, 耗时%d毫秒\n", threads, (System.currentTimeMillis() - startTime)); if (threads == 0) { System.out.printf("总耗时:%d毫秒\n", totleTime); System.out.printf("连接失败数量:%d\n", error); } } } public void readToBuffer(StringBuffer buffer, InputStream is) throws IOException { String line; // 用来保存每行读取的内容 BufferedReader reader = new BufferedReader( new InputStreamReader(is)); line = reader.readLine(); // 读取第一行 while (line != null) { // 如果 line 为空说明读完了 buffer.append(line); // 将读到的内容添加到 buffer 中 buffer.append("\n"); // 添加换行符 line = reader.readLine(); // 读取下一行 } } } public static void main(String[] args) { WorkThread[] workThreads = new WorkThread[threads]; for (int i = 0; i < threads; i++) { workThreads[i] = new WorkThread(); } for (int i = 0; i < threads; i++) { workThreads[i].start(); System.out.println("启动" + (i + 1) + "个线程!"); } } } |
||
---|---|---|---|
级别: 童生
![]() |
807# 发表于:2011-01-06 21:40:52 IP:113.150.*.*
看看
|
||
---|---|---|---|