Thread.Join 메서드

정의

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

오버로드

Join()

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

Join(Int32)

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

Join(TimeSpan)

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

Join()

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() 호출하여 Thread1 가 완료될 때까지 Thread2 차단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 스레드가 이미 종료된 경우 메서드는 즉시 반환됩니다.

경고

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

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

추가 정보

적용 대상

Join(Int32)

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() 호출하여 Thread1 가 완료되거나 2초가 경과할 때까지 Thread2Thread2를 차단합니다.

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

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

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

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

추가 정보

적용 대상

Join(TimeSpan)

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 상태의 스레드에 연결하려고 했습니다.

예제

다음 코드 예제를 사용 하는 방법에 설명 합니다 값을 사용 JoinTimeSpan 여는 메서드.

using namespace System;
using namespace System::Threading;

static TimeSpan waitTime = TimeSpan(0,0,1);

ref class Test
{
public:
   static void Work()
   {
      Thread::Sleep( waitTime );
   }

};

int main()
{
   Thread^ newThread = gcnew Thread( gcnew ThreadStart( Test::Work ) );
   newThread->Start();
   if ( newThread->Join( waitTime + waitTime ) )
   {
      Console::WriteLine( "New thread terminated." );
   }
   else
   {
      Console::WriteLine( "Join timed out." );
   }
}
// The example displays the following output:
//        New thread terminated.
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() 호출하여 Thread1 가 완료되거나 2초가 경과할 때까지 Thread2Thread2를 차단합니다.

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가 지정된 경우 Timeout.Infinite 이 메서드는 반환 값을 제외하고 메서드 오버로드와 동일하게 Join() 동작합니다.

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

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

추가 정보

적용 대상