惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Jina AI
Jina AI
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
B
Blog
T
The Blog of Author Tim Ferriss
量子位
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - Franky
小众软件
小众软件
Recent Announcements
Recent Announcements
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
I
InfoQ
美团技术团队
G
Google Developers Blog
Engineering at Meta
Engineering at Meta
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
V
Visual Studio Blog
云风的 BLOG
云风的 BLOG
博客园 - 【当耐特】
IT之家
IT之家
Microsoft Security Blog
Microsoft Security Blog
博客园 - 聂微东
Last Week in AI
Last Week in AI
H
Hackread – Cybersecurity News, Data Breaches, AI and More
H
Help Net Security

博客园 - bartholomew

new作为修饰符时的使用,以及接口的显式实现 关键词:应用程序扩展,通配符应用程序映射 Silverlight 2 Beta 1在Firefox下显示时的一点小问题~ Visual Studio也有调试禁区?! 关于SQL Server 2005的版本号 使用动态SQL的一点小技巧 通过本地IIS SMTP服务器发送邮件时提示“邮箱不可用”的解决办法 使用System.Net.Mail.SmtpClient发送邮件时出现的乱码问题 写代码的心情 在XP上安装SQL Server 2000、Visual studio .net 2003、Visual studio 2005、SQL Server 2005…… XPS M1210到了~~~~ 在dell的网上订购了XPS M1210,耐心等待中…… 对PropertyGrid控件中PropertyValueChanged事件的探讨 关于邮件群发 关于Dotnet中的线程池 Dotnet中强行关闭多线程应用程序的所有线程 工作之余,自省~ 创建某控件的线程之外的其他线程试图调用该控件引发的问题 古怪的ConfigurationManager类
多线程编程中Join与WaitOne的区别
bartholomew · 2006-07-10 · via 博客园 - bartholomew

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

using System;
using System.Threading;

class IsThreadPool
{
    
static void Main()
    
{
        Console.WriteLine(
"MainThread start.");

        AutoResetEvent autoEvent 
= new AutoResetEvent(false);

        Thread NewThread 
=
            
new Thread(new ThreadStart(ThreadMethod));
        NewThread.Start();

        
// Wait for foreground thread to end.
        NewThread.Join();

        Console.WriteLine(
"MainThread end.");
    }


    
static void ThreadMethod()
    
{
        Console.WriteLine(
"haha,NewMethod");
    }

}


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

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

using System;
using System.Threading;

class WaitOne
{
    
static AutoResetEvent autoEvent = new AutoResetEvent(false);

    
static void Main()
    
{
        Console.WriteLine(
"MainThread start.");

        ThreadPool.QueueUserWorkItem(
            
new WaitCallback(WorkMethod), autoEvent);

        
// Wait for work method to signal.
        autoEvent.WaitOne();
        Console.WriteLine(
"MainThread continue.");

        Console.WriteLine(
"MainThread end.");
    }


    
static void WorkMethod(object stateInfo)
    
{
        Console.WriteLine(
"Work start.");

        
// Simulate time spent working.
        Thread.Sleep(new Random().Next(1002000));

        
// Signal that work is finished.
        Console.WriteLine("Work end.");
        ((AutoResetEvent)stateInfo).Set();
    }

}


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