哪位能给我一个简单的多线程实例:用个变量N控制线程数,N个线程去读取一个数组的每一行,将数组全部输出就可以了。最好能带点注释,用控制台程序就可以,谢谢!
解决方案 »
- SQL技术实现,超级复杂,需要大牛
- C#用API设置另存为框值的问题,百分求助
- GDI+对图像判断横竖
- 哪个高手帮帮忙,C# 使用fastreport studio4.6中如何设置中字段别名(分不够可以在加全部送都可以)
- C#2005中連結oracle
- C# 的完成端口辅助教材
- C#中通过修改注册表更改IE代理设置,如何做到无需重启IE即可使代理生效
- 关于在TreeView中删除选中结点.而结点有多个时怎么删.求高手帮忙写个算法.100分相赠!
- 求助: 怎么才能设置新的鼠标的位置
- 请教如果对象存在堆中,那这个对象如何运用栈中的变量?
- sending mail
- 请教Winform:一个窗体如何切换多个MainMenu?
for (int m = 0; m < N; m++)
{
Thread t = new Thread(new ThreadStart(GetArray()));
t.Start();
}
{
private Thread threadOne;
private Thread threadTwo;
private ArrayList stringList;
private event EventHandler OnNumberClear;//数据删除完成引发的事件
public static void Main()
{
ThreadDemo demo = new ThreadDemo(1000);
demo.Action();
}
public ThreadDemo(int number)
{
Random random = new Random(1000000);
stringList = new ArrayList(number);
for (int i = 0; i < number; i++)
{
stringList.Add(random.Next().ToString());
}
threadOne = new Thread(new ThreadStart(Run));//两个线程共同做一件事情
threadTwo = new Thread(new ThreadStart(Run));//两个线程共同做一件事情
threadOne.Name = "线程1";
threadTwo.Name = "线程2";
OnNumberClear += new EventHandler(ThreadDemo_OnNumberClear); }
/// <summary>
/// 开始工作
/// </summary>
public void Action()
{
threadOne.Start();
threadTwo.Start();
}
/// <summary>
/// 共同做的工作
/// </summary>
private 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);
}
} //执行完成之后,停止所有线程
void ThreadDemo_OnNumberClear(object sender, EventArgs e)
{
Console.WriteLine("执行完了,停止了所有线程的执行。");
threadTwo.Abort();
threadOne.Abort(); }
}
我的GetArray()函数内容类似如下:
if (intTxtI < 20)
{
Console.WriteLine("{0}", arrTxt[intTxtI]);
intTxtI++;
}
但有N个线程就输出N行,数组不止N行,不知道为什么?
HDNGO 的:好像只有2个线程,并不是N个。
能不能像我发的帖子的样子弄一个简单的就好,明天继续加分,今天加不了,谢谢!!
private static int arrindex = 0;
const int N = 5;
private static ManualResetEvent ent = new ManualResetEvent(false); static void Main()
{
for (int i = 0; i < 1000; i++)
{
Arr[i] = i.ToString();
} for (int threadnum = 0; threadnum < N; threadnum++)
{
Thread s = new Thread(OutputN);
s.Name = "thread" + threadnum.ToString();
s.IsBackground = true;
s.Start();
Thread.Sleep(0);
}
Console.Read(); } private static void OutputN()
{
while (arrindex < 1000)
{
lock (Arr)
{
Console.WriteLine("Thread{0} Arr{1} is: {2}", Thread.CurrentThread.Name, arrindex, Arr[arrindex]);
arrindex++;
}
Thread.Sleep(0);
}
}
...我那个是DEMO你完全可以用数组弄个循环什么的哇
using System.Collections.Generic;
using System.Text;
using System.Threading;namespace ConsoleApplication5
{
class Program
{
static int[,] A = new int[,] { { 1, 11, 111 }, { 2, 22, 222 }, { 3, 33, 333 } };
static void Main(string[] args)
{
for (int i = 0; i <= A.GetUpperBound(0); i++)
{
Thread OutputAThread = new Thread(new ThreadStart(OutputA));
OutputAThread.Name = i.ToString();
OutputAThread.IsBackground = true;
OutputAThread.Start();
}
Console.Read();
} static void OutputA()
{
lock (A)
{
for (int i = 0; i < 3; i++)
Console.Write(A[Convert.ToInt32(Thread.CurrentThread.Name), i].ToString() + " ");
Console.WriteLine();
}
}
}
}
much0726 的我有两个地方不怎么明白,
private static ManualResetEvent ent = new ManualResetEvent(false);
这句应该是没用的吧,
OutputN()函数里的 while (arrindex < 1000)好像会越界,这个其实我也觉得是对的,
可是就是越界了,不知何故。
lock(Arr)换成lock(ent)效果是一样的,而且ent的效率比Arr快.特别是Arr的结构比较复杂的时候很明显,这里使用的是一个简单的数组,所以感觉不出来.
while (arrindex < 1000)不会越界的.while (arrindex < 1000)
{
lock (Arr)
{
Console.WriteLine("Thread{0} Arr{1} is: {2}", Thread.CurrentThread.Name, arrindex, Arr[arrindex]);
arrindex++;
}
//Thread.Sleep(0);
}这样才会越界,看来你都差不多懂了,我就不写注释了,其实也很简单啦.
while (true)
{
arrindex = 0;
for (int threadnum = 0; threadnum < N; threadnum++)
{
Thread s = new Thread(OutputN);
s.Name = "thread" + threadnum.ToString();
s.IsBackground = true;
s.Start();
Thread.Sleep(0);
}
}我这样执行都不会出错。