Thread.Join 메서드

정의

이 인스턴스가 나타내는 스레드가 종료될 때까지 호출 스레드를 차단합니다.

오버로드

Name Description
Join()

표준 COM 및 SendMessage 펌핑을 계속 수행하면서 이 인스턴스가 나타내는 스레드가 종료될 때까지 호출 스레드를 차단합니다.

Join(Int32)

표준 COM 및 SendMessage 펌핑을 계속 수행하면서 이 인스턴스가 나타내는 스레드가 종료되거나 지정된 시간이 경과할 때까지 호출 스레드를 차단합니다.

Join(TimeSpan)

표준 COM 및 SendMessage 펌핑을 계속 수행하면서 이 인스턴스가 나타내는 스레드가 종료되거나 지정된 시간이 경과할 때까지 호출 스레드를 차단합니다.

Join()

Source:
Thread.cs
Source:
Thread.cs
Source:
Thread.cs
Source:
Thread.cs
Source:
Thread.cs

표준 COM 및 SendMessage 펌핑을 계속 수행하면서 이 인스턴스가 나타내는 스레드가 종료될 때까지 호출 스레드를 차단합니다.

public:
 void Join();
public void Join();
member this.Join : unit -> unit
Public Sub Join ()

예외

호출자가 상태에 있는 스레드를 조인하려고 했습니다 Unstarted .

대기하는 동안 스레드가 중단됩니다.

설명

Join 는 메서드가 호출된 스레드가 완료될 때까지 호출 스레드(즉, 메서드를 호출하는 스레드 Join )를 차단하는 동기화 메서드입니다. 이 메서드를 사용하여 스레드가 종료되었는지 확인합니다. 스레드가 종료되지 않으면 호출자가 무기한 차단됩니다. 다음 예제에서 스레드는 Thread1 완료될 때까지 Join() 차단되는 Thread2 메서드Thread1를 호출 Thread2 합니다.

using System;
using System.Threading;

public class Example
{
   static Thread thread1, thread2;
   
   public static void Main()
   {
      thread1 = new Thread(ThreadProc);
      thread1.Name = "Thread1";
      thread1.Start();
      
      thread2 = new Thread(ThreadProc);
      thread2.Name = "Thread2";
      thread2.Start();   
   }

   private static void ThreadProc()
   {
      Console.WriteLine("\nCurrent thread: {0}", Thread.CurrentThread.Name);
      if (Thread.CurrentThread.Name == "Thread1" && 
          thread2.ThreadState != ThreadState.Unstarted)
         thread2.Join();
      
      Thread.Sleep(4000);
      Console.WriteLine("\nCurrent thread: {0}", Thread.CurrentThread.Name);
      Console.WriteLine("Thread1: {0}", thread1.ThreadState);
      Console.WriteLine("Thread2: {0}\n", thread2.ThreadState);
   }
}
// The example displays output like the following:
//       Current thread: Thread1
//       
//       Current thread: Thread2
//       
//       Current thread: Thread2
//       Thread1: WaitSleepJoin
//       Thread2: Running
//       
//       
//       Current thread: Thread1
//       Thread1: Running
//       Thread2: Stopped
open System.Threading

let mutable thread1, thread2 =
    Unchecked.defaultof<Thread>, Unchecked.defaultof<Thread>

let threadProc () =
    printfn $"\nCurrent thread: {Thread.CurrentThread.Name}"

    if
        Thread.CurrentThread.Name = "Thread1"
        && thread2.ThreadState <> ThreadState.Unstarted
    then
        thread2.Join()

    Thread.Sleep 4000
    printfn $"\nCurrent thread: {Thread.CurrentThread.Name}"
    printfn $"Thread1: {thread1.ThreadState}"
    printfn $"Thread2: {thread2.ThreadState}\n"

thread1 <- Thread threadProc
thread1.Name <- "Thread1"
thread1.Start()

thread2 <- Thread threadProc
thread2.Name <- "Thread2"
thread2.Start()

// The example displays output like the following:
//       Current thread: Thread1
//
//       Current thread: Thread2
//
//       Current thread: Thread2
//       Thread1: WaitSleepJoin
//       Thread2: Running
//
//
//       Current thread: Thread1
//       Thread1: Running
//       Thread2: Stopped
Imports System.Threading

Module Example
   Dim thread1, thread2 As Thread

   Public Sub Main()
      thread1 = new Thread(AddressOf ThreadProc)
      thread1.Name = "Thread1"
      thread1.Start()
      
      thread2 = New Thread(AddressOf ThreadProc)
      thread2.Name = "Thread2"
      thread2.Start()   
   End Sub

   Private Sub ThreadProc()
      Console.WriteLine()
      Console.WriteLine("Current thread: {0}", Thread.CurrentThread.Name)
      If (Thread.CurrentThread.Name = "Thread1" And 
          thread2.ThreadState <> ThreadState.Unstarted)
         thread2.Join()
      End If
      Thread.Sleep(4000)
      Console.WriteLine()
      Console.WriteLine("Current thread: {0}", Thread.CurrentThread.Name)
      Console.WriteLine("Thread1: {0}", thread1.ThreadState)
      Console.WriteLine("Thread2: {0}", thread2.ThreadState)
      Console.WriteLine()
   End Sub
End Module
' The example displays output like the following :
'       Current thread: Thread1
'       
'       Current thread: Thread2
'       
'       Current thread: Thread2
'       Thread1: WaitSleepJoin
'       Thread2: Running
'       
'       
'       Current thread: Thread1
'       Thread1: Running
'       Thread2: Stopped

스레드가 호출될 때 Join 이미 종료된 경우 메서드는 즉시 반환됩니다.

Warning

현재 스레드에서 Join 현재 스레드를 Thread 나타내는 개체의 메서드를 호출해서는 안 됩니다. 이로 인해 현재 스레드가 무기한 대기하므로 앱이 응답하지 않습니다.

이 메서드는 호출 스레드의 상태를 포함하도록 변경합니다 ThreadState.WaitSleepJoin. 상태에 있는 Join 스레드에서는 호출 ThreadState.Unstarted 할 수 없습니다.

추가 정보

적용 대상

Join(Int32)

Source:
Thread.CoreCLR.cs
Source:
Thread.cs
Source:
Thread.cs
Source:
Thread.cs
Source:
Thread.cs

표준 COM 및 SendMessage 펌핑을 계속 수행하면서 이 인스턴스가 나타내는 스레드가 종료되거나 지정된 시간이 경과할 때까지 호출 스레드를 차단합니다.

public:
 bool Join(int millisecondsTimeout);
public bool Join(int millisecondsTimeout);
member this.Join : int -> bool
Public Function Join (millisecondsTimeout As Integer) As Boolean

매개 변수

millisecondsTimeout
Int32

스레드가 종료될 때까지 대기할 시간(밀리초)입니다.

반품

true 스레드가 종료되었으면 이고, false 매개 변수에 지정된 millisecondsTimeout 시간이 경과한 후 스레드가 종료되지 않은 경우

예외

값은 음수 millisecondsTimeout 이고 밀리초 단위가 아닙니다 Infinite .

스레드가 시작되지 않았습니다.

millisecondsTimeout 가 -1 미만입니다(Timeout.Infinite).

대기하는 동안 스레드가 중단되었습니다.

설명

Join(Int32) 는 메서드가 호출된 스레드가 완료되거나 제한 시간 간격이 경과할 때까지 호출 스레드(즉, 메서드를 호출하는 스레드 Join )를 차단하는 동기화 메서드입니다. 다음 예제에서 스레드는 Thread1 메서드Join()Thread2 호출하여 Thread1 완료되거나 2초가 경과할 때까지 Thread2 차단됩니다.

using System;
using System.Threading;

public class Example
{
   static Thread thread1, thread2;
   
   public static void Main()
   {
      thread1 = new Thread(ThreadProc);
      thread1.Name = "Thread1";
      thread1.Start();
      
      thread2 = new Thread(ThreadProc);
      thread2.Name = "Thread2";
      thread2.Start();   
   }

   private static void ThreadProc()
   {
      Console.WriteLine("\nCurrent thread: {0}", Thread.CurrentThread.Name);
      if (Thread.CurrentThread.Name == "Thread1" && 
          thread2.ThreadState != ThreadState.Unstarted)
         if (thread2.Join(2000))
            Console.WriteLine("Thread2 has termminated.");
         else
            Console.WriteLine("The timeout has elapsed and Thread1 will resume.");   
      
      Thread.Sleep(4000);
      Console.WriteLine("\nCurrent thread: {0}", Thread.CurrentThread.Name);
      Console.WriteLine("Thread1: {0}", thread1.ThreadState);
      Console.WriteLine("Thread2: {0}\n", thread2.ThreadState);
   }
}
// The example displays the following output:
//       Current thread: Thread1
//       
//       Current thread: Thread2
//       The timeout has elapsed and Thread1 will resume.
//       
//       Current thread: Thread2
//       Thread1: WaitSleepJoin
//       Thread2: Running
//       
//       
//       Current thread: Thread1
//       Thread1: Running
//       Thread2: Stopped
open System.Threading

let mutable thread1, thread2 =
    Unchecked.defaultof<Thread>, Unchecked.defaultof<Thread>

let threadProc () =
    printfn $"\nCurrent thread: {Thread.CurrentThread.Name}"

    if
        Thread.CurrentThread.Name = "Thread1"
        && thread2.ThreadState <> ThreadState.Unstarted
    then
        if thread2.Join 2000 then
            printfn "Thread2 has termminated."
        else
            printfn "The timeout has elapsed and Thread1 will resume."

    Thread.Sleep 4000
    printfn $"\nCurrent thread: {Thread.CurrentThread.Name}"
    printfn $"Thread1: {thread1.ThreadState}"
    printfn $"Thread2: {thread2.ThreadState}\n"

thread1 <- Thread threadProc
thread1.Name <- "Thread1"
thread1.Start()

thread2 <- Thread threadProc
thread2.Name <- "Thread2"
thread2.Start()

// The example displays the following output:
//       Current thread: Thread1
//
//       Current thread: Thread2
//       The timeout has elapsed and Thread1 will resume.
//
//       Current thread: Thread2
//       Thread1: WaitSleepJoin
//       Thread2: Running
//
//
//       Current thread: Thread1
//       Thread1: Running
//       Thread2: Stopped
Imports System.Threading

Module Example
   Dim thread1, thread2 As Thread

   Public Sub Main()
      thread1 = new Thread(AddressOf ThreadProc)
      thread1.Name = "Thread1"
      thread1.Start()
      
      thread2 = New Thread(AddressOf ThreadProc)
      thread2.Name = "Thread2"
      thread2.Start()   
   End Sub

   Private Sub ThreadProc()
      Console.WriteLine()
      Console.WriteLine("Current thread: {0}", Thread.CurrentThread.Name)
      If (Thread.CurrentThread.Name = "Thread1" And 
          thread2.ThreadState <> ThreadState.Unstarted)
         If thread2.Join(TimeSpan.FromSeconds(2))
            Console.WriteLine("Thread2 has termminated.")
         Else
            Console.WriteLine("The timeout has elapsed and Thread1 will resume.")
         End If      
      End If
      Thread.Sleep(4000)
      Console.WriteLine()
      Console.WriteLine("Current thread: {0}", Thread.CurrentThread.Name)
      Console.WriteLine("Thread1: {0}", thread1.ThreadState)
      Console.WriteLine("Thread2: {0}", thread2.ThreadState)
      Console.WriteLine()
   End Sub
End Module
' The example displays the following output:
'       Current thread: Thread1
'       
'       Current thread: Thread2
'       
'       Current thread: Thread2
'       Thread1: WaitSleepJoin
'       Thread2: Running
'       
'       
'       Current thread: Thread1
'       Thread1: Running
'       Thread2: Stopped

매개 변수에 Timeout.Infinite 대해 지정된 경우 millisecondsTimeout 이 메서드는 반환 값을 제외하고 메서드 오버로드와 동일하게 Join() 동작합니다.

스레드가 호출될 때 Join 이미 종료된 경우 메서드는 즉시 반환됩니다.

이 메서드는 호출 스레드의 상태를 포함하도록 변경합니다 ThreadState.WaitSleepJoin. 상태에 있는 Join 스레드에서는 호출 ThreadState.Unstarted 할 수 없습니다.

추가 정보

적용 대상

Join(TimeSpan)

Source:
Thread.cs
Source:
Thread.cs
Source:
Thread.cs
Source:
Thread.cs
Source:
Thread.cs

표준 COM 및 SendMessage 펌핑을 계속 수행하면서 이 인스턴스가 나타내는 스레드가 종료되거나 지정된 시간이 경과할 때까지 호출 스레드를 차단합니다.

public:
 bool Join(TimeSpan timeout);
public bool Join(TimeSpan timeout);
member this.Join : TimeSpan -> bool
Public Function Join (timeout As TimeSpan) As Boolean

매개 변수

timeout
TimeSpan

TimeSpan 스레드가 종료되기를 기다리는 시간으로 설정된 것입니다.

반품

true 스레드가 종료되면 이고, false 매개 변수에 지정된 timeout 시간이 경과한 후 스레드가 종료되지 않은 경우

예외

값이 음수 timeout 이고 밀리초 단위가 아니 Infinite 거나 Int32.MaxValue 밀리초보다 큽니다.

호출자가 상태에 있는 스레드를 조인하려고 했습니다 Unstarted .

예제

다음 코드 예제에서는 메서드와 함께 값을 사용 TimeSpan 하는 방법을 보여 줍니다 Join .

using System;
using System.Threading;

class Test
{
    static TimeSpan waitTime = new TimeSpan(0, 0, 1);

    public static void Main() 
    {
        Thread newThread = new Thread(Work);
        newThread.Start();

        if(newThread.Join(waitTime + waitTime)) {
            Console.WriteLine("New thread terminated.");
        }
        else {
            Console.WriteLine("Join timed out.");
        }
    }

    static void Work()
    {
        Thread.Sleep(waitTime);
    }
}
// The example displays the following output:
//        New thread terminated.
open System
open System.Threading

let waitTime = TimeSpan(0, 0, 1)

let work () =
    Thread.Sleep waitTime

let newThread = Thread work
newThread.Start()

if waitTime + waitTime |> newThread.Join then
    printfn "New thread terminated."
else
    printfn "Join timed out."

// The example displays the following output:
//        New thread terminated.
Imports System.Threading

Public Module Test
    Dim waitTime As New TimeSpan(0, 0, 1)

    Public Sub Main() 
        Dim newThread As New Thread(AddressOf Work)
        newThread.Start()

        If newThread.Join(waitTime + waitTime) Then
            Console.WriteLine("New thread terminated.")
        Else
            Console.WriteLine("Join timed out.")
        End If
    End Sub

    Private Sub Work()
        Thread.Sleep(waitTime)
    End Sub
End Module
' The example displays the following output:
'       New thread terminated.

설명

Join(TimeSpan) 는 메서드가 호출된 스레드가 완료되거나 제한 시간 간격이 경과할 때까지 호출 스레드(즉, 메서드를 호출하는 스레드 Join )를 차단하는 동기화 메서드입니다. 다음 예제에서 스레드는 Thread1 메서드Join()Thread2 호출하여 Thread1 완료되거나 2초가 경과할 때까지 Thread2 차단됩니다.

using System;
using System.Threading;

public class Example
{
   static Thread thread1, thread2;
   
   public static void Main()
   {
      thread1 = new Thread(ThreadProc);
      thread1.Name = "Thread1";
      thread1.Start();
      
      thread2 = new Thread(ThreadProc);
      thread2.Name = "Thread2";
      thread2.Start();   
   }

   private static void ThreadProc()
   {
      Console.WriteLine("\nCurrent thread: {0}", Thread.CurrentThread.Name);
      if (Thread.CurrentThread.Name == "Thread1" && 
          thread2.ThreadState != ThreadState.Unstarted)
         if (thread2.Join(TimeSpan.FromSeconds(2)))
            Console.WriteLine("Thread2 has termminated.");
         else
            Console.WriteLine("The timeout has elapsed and Thread1 will resume.");   
      
      Thread.Sleep(4000);
      Console.WriteLine("\nCurrent thread: {0}", Thread.CurrentThread.Name);
      Console.WriteLine("Thread1: {0}", thread1.ThreadState);
      Console.WriteLine("Thread2: {0}\n", thread2.ThreadState);
   }
}
// The example displays the following output:
//       Current thread: Thread1
//       
//       Current thread: Thread2
//       The timeout has elapsed and Thread1 will resume.
//       
//       Current thread: Thread2
//       Thread1: WaitSleepJoin
//       Thread2: Running
//       
//       
//       Current thread: Thread1
//       Thread1: Running
//       Thread2: Stopped
open System
open System.Threading

let mutable thread1, thread2 =
    Unchecked.defaultof<Thread>, Unchecked.defaultof<Thread>

let threadProc () =
    printfn $"\nCurrent thread: {Thread.CurrentThread.Name}"

    if
        Thread.CurrentThread.Name = "Thread1"
        && thread2.ThreadState <> ThreadState.Unstarted
    then
        if TimeSpan.FromSeconds 2 |> thread2.Join then
            printfn "Thread2 has termminated."
        else
            printfn "The timeout has elapsed and Thread1 will resume."

    Thread.Sleep 4000
    printfn $"\nCurrent thread: {Thread.CurrentThread.Name}"
    printfn $"Thread1: {thread1.ThreadState}"
    printfn $"Thread2: {thread2.ThreadState}\n"

thread1 <- Thread threadProc
thread1.Name <- "Thread1"
thread1.Start()

thread2 <- Thread threadProc
thread2.Name <- "Thread2"
thread2.Start()

// The example displays the following output:
//       Current thread: Thread1
//
//       Current thread: Thread2
//       The timeout has elapsed and Thread1 will resume.
//
//       Current thread: Thread2
//       Thread1: WaitSleepJoin
//       Thread2: Running
//
//
//       Current thread: Thread1
//       Thread1: Running
//       Thread2: Stopped
Imports System.Threading

Module Example
   Dim thread1, thread2 As Thread

   Public Sub Main()
      thread1 = new Thread(AddressOf ThreadProc)
      thread1.Name = "Thread1"
      thread1.Start()
      
      thread2 = New Thread(AddressOf ThreadProc)
      thread2.Name = "Thread2"
      thread2.Start()   
   End Sub

   Private Sub ThreadProc()
      Console.WriteLine()
      Console.WriteLine("Current thread: {0}", Thread.CurrentThread.Name)
      If (Thread.CurrentThread.Name = "Thread1" And 
          thread2.ThreadState <> ThreadState.Unstarted)
         If thread2.Join(2000)
            Console.WriteLine("Thread2 has termminated.")
         Else
            Console.WriteLine("The timeout has elapsed and Thread1 will resume.")
         End If      
      End If
      Thread.Sleep(4000)
      Console.WriteLine()
      Console.WriteLine("Current thread: {0}", Thread.CurrentThread.Name)
      Console.WriteLine("Thread1: {0}", thread1.ThreadState)
      Console.WriteLine("Thread2: {0}", thread2.ThreadState)
      Console.WriteLine()
   End Sub
End Module
' The example displays the following output:
'       Current thread: Thread1
'       
'       Current thread: Thread2
'       
'       Current thread: Thread2
'       Thread1: WaitSleepJoin
'       Thread2: Running
'       
'       
'       Current thread: Thread1
'       Thread1: Running
'       Thread2: Stopped

지정된 경우 Timeout.Infinite 이 메서드는 timeout반환 값을 제외하고 메서드 오버로드와 동일하게 Join() 동작합니다.

스레드가 호출될 때 Join 이미 종료된 경우 메서드는 즉시 반환됩니다.

이 메서드는 현재 스레드의 상태를 포함하도록 변경합니다 WaitSleepJoin. 상태에 있는 Join 스레드에서는 호출 ThreadState.Unstarted 할 수 없습니다.

추가 정보

적용 대상