2016-07-04 102 views
-2

我正在尝试制作一个多线程应用程序。但是输入只是一个线程。但我试图用三个线程来完成。这是程序:多线程,输出错误

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(); 
     } 
    } 

但输出是:看图片: enter image description here

所以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."); 
+0

你为什么评论'do/while'循环?你的'if'条件让你不用等待结果就退出 –

+0

嗨,谢谢你的回答。我编辑帖子 – InfinityGoesAround

+0

你的代码完全适合我,我认为你的OS/CPU设置有问题吗? – prospector

回答

0

您拥有的if语句不正确,根本不起作用。当我使用“while”版本时,你的代码适用于我,除非你检测线程完成的策略是错误的(甚至可能是你的问题的来源)。试试这段代码,看看你的问题是否消失。如果没有,那么你可能会遇到本地可用的内存/ CPU /线程池的问题。重新启动计算机并重试:)

static void Main(string[] args) 
{ 
    Console.WriteLine("Main thread starting."); 

    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(); 

    Console.WriteLine("Main thread ending."); 
} 
+0

谢谢你的回复。看到我的评论 – InfinityGoesAround

+0

Oke,谢谢。我尝试重新启动。 – InfinityGoesAround

+0

所以我重新启动了电脑。但没有太大的变化 – InfinityGoesAround

2

您正在创建三个类。每个类创建一个增加自己的属性的新线程。但你退出控制台应用程序,如果所有的数字都小于10。换句话说,如果别人

while (mt1.Count < 10 && mt2.Count < 10 && mt3.Count < 10); 

之前这些河段10中的任何一个,则Main()将结束。在这个条件被检查之前,不能保证任何一个计数都会达到10,更不用说其中的三个。其中一人甚至可以在其他人开始前达到10人。

你会得到更接近预期的影响,如果你把它改成这样:

while (mt1.Count < 10 || mt2.Count < 10 || mt3.Count < 10); 

换句话说,如果它们中的任何一个小于10,继续前进。当他们全部达到10时,停止。

你会发现(或已经有)的东西是,除非你小心,多线程应用程序可以以不可预测,不一致的方式行事。没有什么比99%的时间完美运行的程序更令人沮丧的,但是突然做了一些不同的事情,然后再次按预期工作。 Here's an example您可以在其中测试并查看输出。

+0

谢谢你的回复。我按照你的建议尝试。但它给出了相同的结果。 – InfinityGoesAround

0

我找到了解决方案。看我的帖子

+0

建议 - 确保你知道为什么它以前没有工作。原因在于,因为这种类型的编程有点棘手,它可能会起作用,但偶尔会失效。我认为,你所得到的结果每次都会发挥作用(只要顺序无关紧要)。但是要弄明白为什么要通过反复试验来达到目的是很重要的。 –

+0

当然,你是对的。起初我不明白。但现在我更了解它。 – InfinityGoesAround