62,266
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net;
namespace 多线程列队
{
public partial class Form4 : Form
{
public Form4()
{
InitializeComponent();
}
static public ManualResetEvent synchro = null;
private int n ; //并发数
private int num = -1;
private int TT ;
private void Start()
{
for (int m = 0; m < TT; m++)//共有100个
{
ThreadStart myThreadDelegate = new ThreadStart(this.go);
Thread myThread = new Thread(myThreadDelegate);
if (n > 0)
{
n--;
Add("@启动线程- " + m);
myThread.Start();
}
else
{
synchro = new ManualResetEvent(false);
synchro.WaitOne(); //等待
n--;
Add("@启动线程- " + m);
myThread.Start();
}
}
}
public void go()
{
int i = 0;
i = Interlocked.Increment(ref num);
Add2("执行线程- " + i + " " + DateTime.Now);
//模拟任务执行时间
//Thread.Sleep(RandomTime());
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.163.com");
request.ProtocolVersion = HttpVersion.Version11;
request.Credentials = CredentialCache.DefaultCredentials;
request.Timeout = 60 * 1000;//60秒钟
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
n++;
if (n > 0)
{
synchro.Set();//激活等待
}
Add2("结束线程- " + i + " " + DateTime.Now);
Remove("@启动线程- " + i);
}
private int RandomTime()
{
Random R = new Random();
return R.Next(1000, 12000);
}
#region 跨线程控制控件
public delegate void OutPutDelegate(object outputStr);
public void Add(object outputStr)
{
if (this.InvokeRequired)
{
OutPutDelegate opd = new OutPutDelegate(Add);
this.BeginInvoke(opd, new object[] { outputStr });
}
else
{
listBox1.Items.Add(outputStr);
}
}
public void Remove(object outputStr)
{
if (this.InvokeRequired)
{
OutPutDelegate opd = new OutPutDelegate(Remove);
this.BeginInvoke(opd, new object[] { outputStr });
}
else
{
listBox1.Items.Remove(outputStr);
}
}
public delegate void OutPutDelegate2(object outputStr);
public void Add2(object outputStr)
{
if (this.InvokeRequired)
{
OutPutDelegate2 opd = new OutPutDelegate2(Add2);
this.BeginInvoke(opd, new object[] { outputStr });
}
else
{
listBox2.Items.Add(outputStr);
}
}
public void Remove2(object outputStr)
{
if (this.InvokeRequired)
{
OutPutDelegate2 opd = new OutPutDelegate2(Remove2);
this.BeginInvoke(opd, new object[] { outputStr });
}
else
{
listBox2.Items.Remove(outputStr);
}
}
#endregion
private void button1_Click(object sender, EventArgs e)
{
n = Convert.ToInt32(textBox1.Text.Trim());
TT = Convert.ToInt32(textBox2.Text.Trim());
Thread th = new Thread(new ThreadStart(Start));
th.Start();
}
}
}
using System;
using System.Collections.Generic;
using System.Threading;
using System.Collections;
namespace Test
{
class Program
{
static ArrayList arraylist = new ArrayList();
static Thread[] threads = new Thread[5];
/// <summary>
/// clear arraylist finished self-defined method
/// </summary>
static event EventHandler OnFinished;
static void Main()
{
#region 初始化数据
Random r = new Random();
for (int i = 0; i < 50; i++)
{
arraylist.Add(r.Next(1000));
}
#endregion
Console.WriteLine("clear arraylist begin!");
for(int i=0;i<threads.Length;i++)
{
threads[i] = new Thread(new ThreadStart(DelMorethen50));
threads[i].Name = "thread" + i.ToString();
threads[i].Start();
}
OnFinished += new EventHandler(Program_OnFinished);
}
/// <summary>
/// 清除完成执行事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
static void Program_OnFinished(object sender, EventArgs e)
{
Console.WriteLine("clear arraylist finished!");
for (int i = 0; i < threads.Length; i++)
{
threads[i].Abort();//终止线程
}
}
/// <summary>
/// 线程调用方法
/// </summary>
static void DelMorethen50()
{
while (true)
{
Monitor.Enter(arraylist.SyncRoot);
if (arraylist.Count == 0) Program_OnFinished(arraylist.SyncRoot, new EventArgs());
int num = (int)arraylist[0];
arraylist.RemoveAt(0);
Console.WriteLine("{0}\t{1}\t{2}", Thread.CurrentThread.Name, "deleted", num);
Monitor.Exit(arraylist.SyncRoot);
Thread.Sleep(20);
}
}
}
}