Thread.Join 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
이 인스턴스가 나타내는 스레드가 종료될 때까지 호출 스레드를 차단합니다.
오버로드
Join() |
표준 COM 및 |
Join(Int32) |
표준 COM 및 SendMessage 펌프를 계속 수행하면서 이 인스턴스가 나타내는 스레드가 종료되거나 지정된 시간이 경과할 때까지 호출 스레드를 차단합니다. |
Join(TimeSpan) |
표준 COM 및 SendMessage 펌프를 계속 수행하면서 이 인스턴스가 나타내는 스레드가 종료되거나 지정된 시간이 경과할 때까지 호출 스레드를 차단합니다. |
Join()
표준 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
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 합니다. 상태의 스레드에서는를 호출할 수 없습니다 Join
ThreadState.Unstarted .
추가 정보
적용 대상
Join(Int32)
표준 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
Thread2
이 완료 되거나 2 초가 경과할 때까지 차단 합니다.
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
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)
표준 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
매개 변수
반환
스레드가 종료되면 true
이고, false
매개 변수에서 지정한 기간이 경과된 후에도 스레드가 종료되지 않으면 timeout
입니다.
예외
호출자가 Unstarted 상태의 스레드에 연결하려고 했습니다.
예제
다음 코드 예제에서는 메서드를 사용 하 여 값을 사용 하는 방법을 보여 줍니다 TimeSpan
Join
.
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.
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
Thread2
이 완료 되거나 2 초가 경과할 때까지 차단 합니다.
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
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 .