2015-11-07 105 views
6

我是C#的新手,仍在學習線程概念。我寫了一個程序,它是如下C語言中的線程概念#

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Threading; 

namespace ConsoleApplication17 
{ 
    class Program 
    { 
    static void Main(string[] args) 
    { 
     System.Threading.ThreadStart th1 = new System.Threading.ThreadStart(prnt); 
     Thread th = new Thread(th1); 
     th.Start(); 
     bool t = th.IsAlive; 
     for (int i = 0; i < 10; i++) 
     { 
      Console.WriteLine(i + "A"); 
     } 
    } 

    private static void prnt() 
    { 
     for (int i = 0; i < 10; i ++) 
     { 
      Console.WriteLine(i + "B"); 
     } 
    } 
    } 
} 

我期待的輸出,如: -

1A 
2A 
1B 
3A 
2B... 

,因爲我相信,這兩個主線程和新創建的線程應該同時執行。

但是輸出是有序的,首先調用prnt函數,然後執行循環的Main方法。

+3

你的代碼看起來罰款;大概它只是需要更長的時間才能完成主線程。爲每個'for for循環添加'Thread.Sleep(500);';這將有助於確保他們花費更長時間運行,以便您可以看到發生了什麼。 – JohnLBevan

+1

線程調度是不可預測的,許多程序都會失敗(比賽條件),因爲程序員假定線程不存在某些理想行爲,例如假設線程始終並行運行並處於完美同步狀態。他們不。考慮您的程序如何在帶有大型(100ms +)時間片的操作系統的單線程處理器上運行。 – Dai

回答

3

可能的10 cicle是不夠的,看到兩個線程同時運行。 使用更長的循環或在循環迭代中放置Thread.Sleep(100)。 啓動線程非常昂貴。它會發生什麼,也許是你主迴路得到螺紋循環開始之前執行,由於一個線程需要的時間來啓動

+0

謝謝,它有100次迭代。 – user2235487

2

我和你需要添加的Thread.Sleep以前的答案同意(一些秒* 1000)

我建議你閱讀這篇文章關於線程 https://msdn.microsoft.com/ru-ru/library/system.threading.thread(v=vs.110).aspx

static void Main(string[] args) 
    { 
     Thread th = new Thread(prnt); 
     th.Start(); 
     for (int i = 0; i < 10; i++) 
     { 
      //sleep one second 
      Thread.Sleep(1000); 
      Console.WriteLine(i + "A"); 
     } 
     //join the basic thread and 'th' thread 
     th.Join(); 
    } 
    private static void prnt() 
    { 
     for (int i = 0; i < 10; i++) 
     { 
      //sleep one second 
      Thread.Sleep(1000); 
      Console.WriteLine(i + "B"); 
     } 
    } 
1

更新:一個進程被用來隔離應用程序和線程在該進程的上下文中運行。

通過這種方式,它使得更容易爲OS管理的應用程序不同,管理崩潰和上下文切換(所提供的每個應用程序由CPU執行的時間時隙)。後面的想法是在預定時間段內簡單地運行線程,當允許應用程序運行的時間結束時,CPU切換到其他線程執行。

在應用程序中爲小型異步任務使用Threading類。

瞭解如何在下面的例子中,你可以使用System.Threading命名空間創建一個新的線程。注意如何調用t.Join()以等待新線程完成。

注意,Spleep(1)將迫使改變的背景下,這就是是什麼使你例子的區別。嘗試將其更改回零並查看結果。

using System; 
using System.Threading; 

namespace Chapter1 
{ 
    public static class Threads1 
    { 
     public static void ThreadMethod() 
     { 
      for (int i = 0; i < 10; i++) 
      { 
       Console.WriteLine("ThreadProc: {0}", i); 
       Thread.Sleep(1); 
      } 
     } 
     public static void Main(string[] args) 
     { 
      Thread t = new Thread(new ThreadStart(ThreadMethod)); 
      t.Start(); 
      for (int i = 0; i < 4; i++) 
      { 
       Console.WriteLine("Main thread: Do some work."); 
       Thread.Sleep(1); 
      } 
      t.Join(); 

      Console.Read(); 
     } 
    } 
} 
// OUTPUT: 
//Main thread: Do some work. 
//ThreadProc: 0 
//ThreadProc: 1 
//Main thread: Do some work. 
//ThreadProc: 2 
//Main thread: Do some work. 
//ThreadProc: 3 
//Main thread: Do some work. 
//ThreadProc: 4 
//ThreadProc: 5 
//ThreadProc: 6 
//ThreadProc: 7 
//ThreadProc: 8 
//ThreadProc: 9 
+1

*「線程」用於隔離應用程序並讓它們在自己的進程中運行。*不,線程不是一個進程,也不會隔離「應用程序」。線程是一個執行單元,允許多個工作*並行*完成。線程仍處於進程的邏輯上下文中。 –

+0

已更新@YuvalItzchakov – Rober