Csharp/CSharp Tutorial/Thread/Thread Join

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Block until a thread finishes, or timeout after 2 seconds

using System;
using System.Threading;
class MainClass
{
    private static void DoCount()
    {
        for (int i = 0; i < 5; i++)
        {
            Console.WriteLine("{0} : DoCount thread",DateTime.Now.ToString("HH:mm:ss.ffff"));
            // Sleep for 1 second.
            Thread.Sleep(1000);
        }
    }
    public static void Main()
    {
        Thread thread = new Thread(DoCount);
        Console.WriteLine("{0} : Starting DoCount thread.",DateTime.Now.ToString("HH:mm:ss.ffff"));
        thread.Start();
        if (!thread.Join(2000))
        {
            Console.WriteLine("{0} : Join timed out !!", DateTime.Now.ToString("HH:mm:ss.ffff"));
        }
        // Block again until the DoCount thread finishes with no timeout.
        thread.Join();
    }
}
14:49:28.6093 : Starting DoCount thread.
14:49:28.6250 : DoCount thread
14:49:29.6250 : DoCount thread
14:49:30.6250 : Join timed out !!
14:49:30.6250 : DoCount thread
14:49:31.6250 : DoCount thread
14:49:32.6250 : DoCount thread

Thread joins

using System;
using System.Threading;
class MainClass
{
  private static int ThreadCount = 5;
  public static void DoCount()
  {
    for ( int i = 0; i < 3; i++ )
    {
      System.Console.WriteLine( "{0} reached {1}", Thread.CurrentThread.Name, i );
    }
  }
  [STAThread]
  static void Main(string[] args)
  {
    Thread[] t = new Thread[ ThreadCount ];
    for( int k = 0; k < ThreadCount; k++ )
    {
      t[ k ] = new Thread( new ThreadStart( DoCount ) );
      t[ k ].Name = "Thread " + k;
      t[ k ].Start();
    }
    // wait for all the Threads to finish
    for( int k = 0; k < ThreadCount; k++ )
    {
      t[ k ].Join();
    }
  }
}
Thread 0 reached 0
Thread 0 reached 1
Thread 0 reached 2
Thread 1 reached 0
Thread 1 reached 1
Thread 1 reached 2
Thread 2 reached 0
Thread 2 reached 1
Thread 2 reached 2
Thread 3 reached 0
Thread 3 reached 1
Thread 3 reached 2
Thread 4 reached 0
Thread 4 reached 1
Thread 4 reached 2

Threads Joining

using System;
using System.Threading;
class Util
{
    public void DoSleep()
    {
        Console.WriteLine("Sleepping {0} seconds", 5);
        Thread.Sleep(5 * 1000);
    }
    
    public static Thread Sleep()
    {
        Util ts = new Util();
        Thread thread = new Thread(new ThreadStart(ts.DoSleep));
        thread.Start();
        return(thread);
    }
}
class MainClass
{
    public static void Main()
    {
        Thread thread = Util.Sleep();
        
        Console.WriteLine("Waiting for thread to join");
        thread.Join();
        Console.WriteLine("Thread Joined");
    }
}
Sleepping 5 seconds
Waiting for thread to join
Thread Joined

Use Join() to wait for threads to end

using System; 
using System.Threading; 
 
class MyThread { 
  public int count; 
  public Thread thrd; 
 
  public MyThread(string name) { 
    count = 0; 
    thrd = new Thread(this.run); 
    thrd.Name = name; 
    thrd.Start(); 
  } 
 
  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 MainClass { 
  public static void Main() { 
    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(); 
    Console.WriteLine("Child #1 joined."); 
 
    mt2.thrd.Join(); 
    Console.WriteLine("Child #2 joined."); 
 
    mt3.thrd.Join(); 
    Console.WriteLine("Child #3 joined."); 
 
    Console.WriteLine("Main thread ending."); 
  } 
}
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.
Child #1 joined.
Child #2 joined.
Child #3 joined.
Main thread ending.