using
System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Diagnostics;
using System.Collections;
using System.IO;
using System.Net;
namespace Multithreading.Console
{
public class MainClass
{
///
/// 设置在这个函数里
///
private void Settings()
{
URL_DIC . Add( "http://localhost:29649/Default.aspx" , 50);
URL_DIC . Add( "http://localhost:29649/Default3.aspx" , 10);
}
///
/// 启动该函数
///
public void Start()
{
this . Settings();
this . Init();
this . Main();
}
///
/// 更改展示在这个函数里
///
private void InitThreadFunc()
{
for ( int index = 0; index < 100; index ++)
{
//延迟500ms
System . Threading . Thread . Sleep( 500);
Stopwatch stopwatch = new Stopwatch();
try
{
//stopwatch.Start();
string result = DownloadFromUrl( Url());
//stopwatch.Stop();
//System.Console.WriteLine(Thread.CurrentThread.Name + "耗时:" + stopwatch.ElapsedMilliseconds);
System . Console . Write( ".");
}
catch ( Exception ex)
{
//stopwatch.Stop();
System . Console . Write( "*");
//System.Console.WriteLine("-------" + Thread.CurrentThread.Name + "耗时:" + stopwatch.ElapsedMilliseconds);
}
}
}
///
/// 下载超时时间,单位ms(毫秒)
///
int DOWNLOAD_TIMEOUT = 1000;
///
/// 最大线程数,并发量
///
int MAX_THREAD = 100;
///
/// 每个线程下载的数量
///
int EVERY_THREAD_DOWNLOAD_COUNT = 100;
int MAX_DENOMINATOR = 10000;
private void Init()
{
int max = 0;
foreach ( KeyValuePair < string , int > item in URL_DIC)
{
max += item . Value;
}
int n = 0;
foreach ( KeyValuePair < string , int > item in URL_DIC)
{
int length = ( int)((( decimal) item . Value) / (( decimal) max) * MAX_DENOMINATOR);
for ( int index = 0; index < length; index ++)
{
dic . Add(n , item . Key);
n ++;
}
}
MAX_DENOMINATOR = dic . Count;
}
///
/// 测试访问的URL第一个参数为URL,第二个参数为所占的比例
///
Dictionary < string , int > URL_DIC = new Dictionary < string , int >();
private List < Thread > threads = new List < Thread >();
Dictionary < int , string > dic = new Dictionary < int , string >();
public void Main()
{
for ( int index = 0; index < MAX_THREAD; index ++)
{
Thread thread = new Thread( new ThreadStart( InitThreadFunc));
thread . Name = "thread" + index;
threads . Add( thread);
thread . Start();
}
while ( threads . Count > 0)
{
foreach ( Thread thread in threads)
{
if ( thread . Join( 1000))
{
threads . Remove( thread);
break;
}
}
}
}
///
/// 此函数随机取地址需要改进一下
///
///
private string Url()
{
Random random = new Random();
return dic [ random . Next( 0 , MAX_DENOMINATOR )];
}
public string DownloadFromUrl( string url)
{
try
{
if (! url . Contains( "http://"))
{ url = "http://" + url; }
url . TrimEnd( new char [] { '//' , '/' });
HttpWebRequest request = WebRequest . Create( url) as HttpWebRequest;
request . Timeout = DOWNLOAD_TIMEOUT;
HttpWebResponse response = request . GetResponse() as HttpWebResponse;
System . IO . Stream responseStream = response . GetResponseStream();
System . IO . StreamReader reader = new System . IO . StreamReader( responseStream , Encoding . UTF8);
string srcString = reader . ReadToEnd();
return srcString;
}
catch ( Exception ex)
{
throw ex;
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Diagnostics;
using System.Collections;
using System.IO;
using System.Net;
namespace Multithreading.Console
{
public class MainClass
{
///
/// 设置在这个函数里
///
private void Settings()
{
URL_DIC . Add( "http://localhost:29649/Default.aspx" , 50);
URL_DIC . Add( "http://localhost:29649/Default3.aspx" , 10);
}
///
/// 启动该函数
///
public void Start()
{
this . Settings();
this . Init();
this . Main();
}
///
/// 更改展示在这个函数里
///
private void InitThreadFunc()
{
for ( int index = 0; index < 100; index ++)
{
//延迟500ms
System . Threading . Thread . Sleep( 500);
Stopwatch stopwatch = new Stopwatch();
try
{
//stopwatch.Start();
string result = DownloadFromUrl( Url());
//stopwatch.Stop();
//System.Console.WriteLine(Thread.CurrentThread.Name + "耗时:" + stopwatch.ElapsedMilliseconds);
System . Console . Write( ".");
}
catch ( Exception ex)
{
//stopwatch.Stop();
System . Console . Write( "*");
//System.Console.WriteLine("-------" + Thread.CurrentThread.Name + "耗时:" + stopwatch.ElapsedMilliseconds);
}
}
}
///
/// 下载超时时间,单位ms(毫秒)
///
int DOWNLOAD_TIMEOUT = 1000;
///
/// 最大线程数,并发量
///
int MAX_THREAD = 100;
///
/// 每个线程下载的数量
///
int EVERY_THREAD_DOWNLOAD_COUNT = 100;
int MAX_DENOMINATOR = 10000;
private void Init()
{
int max = 0;
foreach ( KeyValuePair < string , int > item in URL_DIC)
{
max += item . Value;
}
int n = 0;
foreach ( KeyValuePair < string , int > item in URL_DIC)
{
int length = ( int)((( decimal) item . Value) / (( decimal) max) * MAX_DENOMINATOR);
for ( int index = 0; index < length; index ++)
{
dic . Add(n , item . Key);
n ++;
}
}
MAX_DENOMINATOR = dic . Count;
}
///
/// 测试访问的URL第一个参数为URL,第二个参数为所占的比例
///
Dictionary < string , int > URL_DIC = new Dictionary < string , int >();
private List < Thread > threads = new List < Thread >();
Dictionary < int , string > dic = new Dictionary < int , string >();
public void Main()
{
for ( int index = 0; index < MAX_THREAD; index ++)
{
Thread thread = new Thread( new ThreadStart( InitThreadFunc));
thread . Name = "thread" + index;
threads . Add( thread);
thread . Start();
}
while ( threads . Count > 0)
{
foreach ( Thread thread in threads)
{
if ( thread . Join( 1000))
{
threads . Remove( thread);
break;
}
}
}
}
///
/// 此函数随机取地址需要改进一下
///
///
private string Url()
{
Random random = new Random();
return dic [ random . Next( 0 , MAX_DENOMINATOR )];
}
public string DownloadFromUrl( string url)
{
try
{
if (! url . Contains( "http://"))
{ url = "http://" + url; }
url . TrimEnd( new char [] { '//' , '/' });
HttpWebRequest request = WebRequest . Create( url) as HttpWebRequest;
request . Timeout = DOWNLOAD_TIMEOUT;
HttpWebResponse response = request . GetResponse() as HttpWebResponse;
System . IO . Stream responseStream = response . GetResponseStream();
System . IO . StreamReader reader = new System . IO . StreamReader( responseStream , Encoding . UTF8);
string srcString = reader . ReadToEnd();
return srcString;
}
catch ( Exception ex)
{
throw ex;
}
}
}
}