using System; using System.Collections.Generic; using System.Text; using System.Collections; using System.Threading;namespace ConsoleApplication2 { #region 多线程间同步的例子(两个线程同时执行删除操作) public class ThreadDemo { private Thread threadOne; private Thread threadTwo; private ArrayList stringList; private event EventHandler OnNumberClear;//引发的事件 public static void Main(string[] args) { ThreadDemo t = new ThreadDemo(100); t.Action(); } public ThreadDemo(int number) { Random ran = new Random(1000); stringList = new ArrayList(number); for (int i = 0; i < number; i++) { stringList.Add(ran.Next(i).ToString()); } threadOne = new Thread(new ThreadStart(Run)); threadTwo = new Thread(new ThreadStart(Run)); threadOne.Name = "线程1"; threadTwo.Name = "线程2"; OnNumberClear += new EventHandler(ThreadDemo_OnNumberClear); } #region 开始启动 public void Action() { threadOne.Start(); threadTwo.Start(); } #endregion #region 共同执行的操作 public void Run() { string stringValue = null; while (true) { Monitor.Enter(this);//锁定,保持同步. stringValue =(string) stringList[0]; Console.WriteLine(Thread.CurrentThread.Name + "删除了" + stringValue); stringList.RemoveAt(0);//删除ArrayList里的元素 if (stringList.Count == 0) { OnNumberClear(this, new EventArgs());//引发完成事件 } Monitor.Exit(this);//取消锁定 Thread.Sleep(5); } } #endregion #region 执行完后,停止所有线程 void ThreadDemo_OnNumberClear(Object sender, EventArgs e) { Console.WriteLine("执行完了,停止所有线程!"); Console.ReadLine(); threadTwo.Abort(); threadOne.Abort(); } #endregion } #endregion }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.IO; class Range { int m_start; int m_end; public Range(int start,int end) { m_start = start; m_end = end; } public int Start { get { return m_start; } set { m_start = value; } } public int End { get { return m_end; } set { m_end = value; } } }class Orz { List<string> m_delNameList; List<Range> m_RangeList; int m_threadCount; Orz(){} public Orz(List<string> delList,int threadCount) { m_delNameList = delList; m_threadCount = threadCount; m_RangeList = new List<Range>(); } public void Start() { int count = m_delNameList.Count / m_threadCount; for(int i = 0; i < m_threadCount ; i++) { Thread t = new Thread(new ThreadStart(DeleteProc)); t.Name = i.ToString(); m_RangeList.Add(new Range(i * count, (i + 1) * count)); t.Start(); } } void DeleteProc() { int index = Convert.ToInt32(Thread.CurrentThread.Name); Range range = m_RangeList[index]; for (int i = range.Start; i < (index == m_threadCount-1 ? m_delNameList.Count : range.End); i++) { File.Delete(m_delNameList[i]); } } } 這樣?
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Threading;namespace ConsoleApplication2
{
#region 多线程间同步的例子(两个线程同时执行删除操作)
public class ThreadDemo
{ private Thread threadOne;
private Thread threadTwo;
private ArrayList stringList;
private event EventHandler OnNumberClear;//引发的事件 public static void Main(string[] args)
{
ThreadDemo t = new ThreadDemo(100);
t.Action();
} public ThreadDemo(int number)
{
Random ran = new Random(1000);
stringList = new ArrayList(number);
for (int i = 0; i < number; i++)
{
stringList.Add(ran.Next(i).ToString());
} threadOne = new Thread(new ThreadStart(Run));
threadTwo = new Thread(new ThreadStart(Run));
threadOne.Name = "线程1";
threadTwo.Name = "线程2";
OnNumberClear += new EventHandler(ThreadDemo_OnNumberClear);
} #region 开始启动
public void Action()
{
threadOne.Start();
threadTwo.Start();
}
#endregion #region 共同执行的操作
public void Run()
{
string stringValue = null;
while (true)
{
Monitor.Enter(this);//锁定,保持同步.
stringValue =(string) stringList[0];
Console.WriteLine(Thread.CurrentThread.Name + "删除了" + stringValue);
stringList.RemoveAt(0);//删除ArrayList里的元素
if (stringList.Count == 0)
{
OnNumberClear(this, new EventArgs());//引发完成事件
}
Monitor.Exit(this);//取消锁定
Thread.Sleep(5);
}
}
#endregion #region 执行完后,停止所有线程
void ThreadDemo_OnNumberClear(Object sender, EventArgs e)
{
Console.WriteLine("执行完了,停止所有线程!");
Console.ReadLine();
threadTwo.Abort();
threadOne.Abort();
}
#endregion }
#endregion
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO;
class Range
{
int m_start;
int m_end;
public Range(int start,int end)
{
m_start = start;
m_end = end;
}
public int Start
{
get { return m_start; }
set { m_start = value; }
}
public int End
{
get { return m_end; }
set { m_end = value; }
}
}class Orz
{
List<string> m_delNameList;
List<Range> m_RangeList;
int m_threadCount;
Orz(){}
public Orz(List<string> delList,int threadCount)
{
m_delNameList = delList;
m_threadCount = threadCount;
m_RangeList = new List<Range>();
}
public void Start()
{
int count = m_delNameList.Count / m_threadCount;
for(int i = 0; i < m_threadCount ; i++)
{
Thread t = new Thread(new ThreadStart(DeleteProc));
t.Name = i.ToString();
m_RangeList.Add(new Range(i * count, (i + 1) * count));
t.Start();
}
} void DeleteProc()
{
int index = Convert.ToInt32(Thread.CurrentThread.Name);
Range range = m_RangeList[index];
for (int i = range.Start; i < (index == m_threadCount-1 ? m_delNameList.Count : range.End); i++)
{
File.Delete(m_delNameList[i]);
}
}
}
這樣?