我正在尝试制作一个多线程应用程序。但是输入只是一个线程。但我试图用三个线程来完成。这是程序:多线程,输出错误
class MyThread
{
public int Count;
public Thread Thrd;
public MyThread(string name)
{
Count = 0;
Thrd = new Thread(this.Run);
Thrd.Name = name;
Thrd.Start();
}
// Entry point of thread.
void Run()
{
Console.WriteLine(Thrd.Name + " starting.");
do
{
Thread.Sleep(500);
Console.WriteLine("In " + Thrd.Name +
", Count is " + Count);
Count++;
} while (Count < 10);
Console.WriteLine(Thrd.Name + " terminating.");
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Main thread starting.");
// Construct three threads.
MyThread mt1 = new MyThread("Child #1");
MyThread mt2 = new MyThread("Child #2");
MyThread mt3 = new MyThread("Child #3");
while (true)
{
Console.Write(".");
Thread.Sleep(100);
if (mt1.Count < 10 && mt2.Count < 10 && mt3.Count < 10)
{
break;
}
Console.WriteLine("Main thread ending.");
}
//do
//{
// Console.Write(".");
// Thread.Sleep(100);
//}
//while (mt1.Count < 10 && mt2.Count < 10 && mt3.Count < 10);
//Console.WriteLine("Main thread ending.");
// Console.ReadKey();
}
}
所以onely显示一个线程。而不是三个线程。
感谢您
这必须是输出:
Main thread starting.
.Child #1 starting.
Child #2 starting.
Child #3 starting.
....In Child #1, Count is 0
In Child #2, Count is 0
In Child #3, Count is 0
.....In Child #1, Count is 1
In Child #2, Count is 1
In Child #3, Count is 1
.....In Child #1, Count is 2
In Child #2, Count is 2
In Child #3, Count is 2
.....In Child #1, Count is 3
In Child #2, Count is 3
In Child #3, Count is 3
.....In Child #1, Count is 4
In Child #2, Count is 4
In Child #3, Count is 4
.....In Child #1, Count is 5
In Child #2, Count is 5
In Child #3, Count is 5
.....In Child #1, Count is 6
In Child #2, Count is 6
In Child #3, Count is 6
.....In Child #1, Count is 7
In Child #2, Count is 7
In Child #3, Count is 7
.....In Child #1, Count is 8
In Child #2, Count is 8
In Child #3, Count is 8
.....In Child #1, Count is 9
Child #1 terminating.
In Child #2, Count is 9
Child #2 terminating.
In Child #3, Count is 9
Child #3 terminating.
Main thread ending.
但是,如果我不喜欢这样写道:
static void Main(string[] args)
{
Console.WriteLine("Main thread starting.");
// Construct three threads.
MyThread mt1 = new MyThread("Child #1");
MyThread mt2 = new MyThread("Child #2");
MyThread mt3 = new MyThread("Child #3");
//while (true)
//{
// Console.Write(".");
// Thread.Sleep(100);
// if (mt1.Count < 10 && mt2.Count < 10 && mt3.Count < 10)
// {
// break;
// }
// Console.WriteLine("Main thread ending.");
//}
do
{
Console.Write(".");
Thread.Sleep(100);
}
while (mt1.Count < 10 && mt2.Count < 10 && mt3.Count < 10);
Console.WriteLine("Main thread ending.");
Console.ReadKey();
}
它给出了相同的结果。
如果我这样做:
Console.WriteLine("Main thread starting.");
// Construct three threads.
MyThread mt1 = new MyThread("Child #1");
MyThread mt2 = new MyThread("Child #2");
MyThread mt3 = new MyThread("Child #3");
mt1.Thrd.Join();
mt2.Thrd.Join();
mt3.Thrd.Join();
do
{
Console.Write(".");
Thread.Sleep(100);
} while (mt1.Thrd.IsAlive && mt2.Thrd.IsAlive && mt3.Thrd.IsAlive);
Console.WriteLine("Main thread ending.");
相同的结果。只有一个线程。
我尝试这样的:
static void Main(string[] args)
{
Console.WriteLine("Main thread starting.");
// Construct three threads.
MyThread mt1 = new MyThread("Child #1");
MyThread mt2 = new MyThread("Child #2");
MyThread mt3 = new MyThread("Child #3");
mt1.Thrd.Join();
mt2.Thrd.Join();
mt3.Thrd.Join();
do
{
Console.Write(".");
Thread.Sleep(100);
} while (mt1.Count < 10 || mt2.Count < 10 || mt3.Count < 10);
Console.WriteLine("Main thread ending.");
但还是同样的结果。
oh:
这对我有效!
Console.WriteLine("Main thread starting.");
// Construct three threads.
MyThread mt1 = new MyThread("Child #1");
MyThread mt2 = new MyThread("Child #2");
MyThread mt3 = new MyThread("Child #3");
mt1.Thrd.Join();
mt2.Thrd.Join();
mt3.Thrd.Join();
do
{
Console.Write(".");
Thread.Sleep(100);
} while (mt1.Thrd.IsAlive || mt2.Thrd.IsAlive || mt3.Thrd.IsAlive);
Console.WriteLine("Main thread ending.");
你为什么评论'do/while'循环?你的'if'条件让你不用等待结果就退出 –
嗨,谢谢你的回答。我编辑帖子 – InfinityGoesAround
你的代码完全适合我,我认为你的OS/CPU设置有问题吗? – prospector