多线程编程中Join与WaitOne的区别

本文通过实例演示了两种线程同步方法:使用Join方法使主线程等待子线程完成;利用WaitOne配合AutoResetEvent实现主线程阻塞直至子线程信号触发。这两种方式为多线程编程提供了有效手段。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

举例说明:
Join:在一个线程MainThread中开启一个新的线程NewThread,在完成初始化并启动NewThread的操作后,调用Join,则MainThread堵塞,直到NewThread执行完毕,MainThread才继续执行。

None.gifusing System;
None.gif
using System.Threading;
None.gif
None.gif
class IsThreadPool
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        Console.WriteLine(
"MainThread start.");
InBlock.gif
InBlock.gif        AutoResetEvent autoEvent 
= new AutoResetEvent(false);
InBlock.gif
InBlock.gif        Thread NewThread 
=
InBlock.gif            
new Thread(new ThreadStart(ThreadMethod));
InBlock.gif        NewThread.Start();
InBlock.gif
InBlock.gif        
// Wait for foreground thread to end.
InBlock.gif
        NewThread.Join();
InBlock.gif
InBlock.gif        Console.WriteLine(
"MainThread end.");
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
static void ThreadMethod()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        Console.WriteLine(
"haha,NewMethod");
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif

运行结果:
MainThread start.
haha,NewMethod
MainThread end.

WaitOne:在一个线程MainThread中开启一个新的线程NewThread,在完成初始化并启动NewThread的操作后,调用WaitOne,则MainThread堵塞,直到在NewThread中调用Set,MainThread才继续执行。

None.gifusing System;
None.gif
using System.Threading;
None.gif
None.gif
class WaitOne
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
static AutoResetEvent autoEvent = new AutoResetEvent(false);
InBlock.gif
InBlock.gif    
static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        Console.WriteLine(
"MainThread start.");
InBlock.gif
InBlock.gif        ThreadPool.QueueUserWorkItem(
InBlock.gif            
new WaitCallback(WorkMethod), autoEvent);
InBlock.gif
InBlock.gif        
// Wait for work method to signal.
InBlock.gif
        autoEvent.WaitOne();
InBlock.gif        Console.WriteLine(
"MainThread continue.");
InBlock.gif
InBlock.gif        Console.WriteLine(
"MainThread end.");
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
static void WorkMethod(object stateInfo)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        Console.WriteLine(
"Work start.");
InBlock.gif
InBlock.gif        
// Simulate time spent working.
InBlock.gif
        Thread.Sleep(new Random().Next(1002000));
InBlock.gif
InBlock.gif        
// Signal that work is finished.
InBlock.gif
        Console.WriteLine("Work end.");
InBlock.gif        ((AutoResetEvent)stateInfo).Set();
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif

运行结果:
MainThread start.
Work start.
Work end.
MainThread end.

转载于:https://www.cnblogs.com/bartholomew/archive/2006/07/10/447408.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值