[C#基础]线程同步技术之Join方法

原文链接:http://www.cnblogs.com/lxblog/archive/2012/12/26/2833974.html

sleep:静态方法,只能暂停当前线程,不能暂停其他线程。
join:实例方法,阻塞调用线程,直到该线程终止或经过了指定时间为止。

using System;
using System.Threading;

class ThreadTest14
{
    static void Main(string[] args)
    {
        Calculate calculate = new Calculate();
        Console.ForegroundColor = ConsoleColor.White;
        Console.WriteLine("主线程输出:准备进行加法运算:");
        calculate.threadAdd.Start();

        calculate.threadAdd.Join();
        Console.ForegroundColor = ConsoleColor.White;
        Console.WriteLine("主线程输出:运算完毕");
        Console.ReadKey();
    }
}

//计算类
public class Calculate
{
    public Thread threadAdd;

    public Calculate()
    {
        threadAdd = new Thread(new ThreadStart(Add));
    }

    //加法运算
    public void Add()
    {
        Console.ForegroundColor = ConsoleColor.Blue;
        Console.WriteLine("进入加法计算");
        Thread.Sleep(5000);
        Console.ForegroundColor = ConsoleColor.Blue;
        Console.WriteLine("加法运算结果: x={0} y={1} x+y={2}", 1, 2, 1 + 2);
    }
}



分析:
调用线程是主线程,当主线程调用了calculate.threadAdd.Join()的时候,就发生了阻塞,直到加法线程运行完毕之后,才继续运行。


现在我们在来看看Join的另外两个重载方法:Join(Int32)  和 Join(TimeSpan),这两个方法其实是一样的,输入参数说白了就是设置阻塞的等待时间,返回值是bool类型,如果线程已终止,则为 true,否则返回 false 。

using System;
using System.Threading;

class ThreadTest15
{
    static void Main(string[] args)
    {
        Calculate calculate = new Calculate();
        Console.ForegroundColor = ConsoleColor.White;
        Console.WriteLine("主线程输出:准备进行加法和减法两种运算:");

        calculate.threadAdd.Start();
        calculate.threadSub.Start();
        calculate.threadAdd.Join();
        calculate.threadSub.Join();

        Console.ForegroundColor = ConsoleColor.White;
        Console.WriteLine("主线程输出:所有运算完毕");
        Console.ReadKey();
    }
}

//计算类
public class Calculate
{
    public Thread threadAdd;
    public Thread threadSub;
    public Calculate()
    {
        threadAdd = new Thread(new ThreadStart(Add));
        threadSub = new Thread(new ThreadStart(Sub));
    }

    //加法运算
    public void Add()
    {
        Console.ForegroundColor = ConsoleColor.Blue;
        Console.WriteLine("进入加法计算");
        Thread.Sleep(5000);
        Console.ForegroundColor = ConsoleColor.Blue;
        Console.WriteLine("加法运算结果: x={0} y={1} x+y={2}", 1, 2, 1 + 2);
    }

    //新增减法运算 
    public void Sub()
    {
        //主要是这里
        bool b = threadAdd.Join(1000);
        Console.ForegroundColor = ConsoleColor.Red;
        if (b)
        {
            Console.WriteLine("加法运算已经完成,进入减法法计算");
        }
        else
        {
            Console.WriteLine("加法运算超时,先进入减法法计算");
        }

        Thread.Sleep(2000);
        Console.WriteLine("进入减法运算");
        Console.ForegroundColor = ConsoleColor.Red;
        Console.WriteLine("减法运算结果: x={0} y={1} x-y={2}", 10, 2, 10 - 2);
    }
}



分析:
首先,主线程遇到 calculate.threadAdd.Join();  和 calculate.threadSub.Join(); 肯定会发生阻塞,等待这两个线程完成后,才会继续执行,这个不容质疑。然后我们看加法线程和减法线程,这两个线程几乎同时执行,谁先执行,我们是不可预期的。比如先执行加法线程,当执行到Thread.Sleep(5000)的时候,加法线程休眠5s,减法线程由于调用了 threadAdd.Join( 1000 ); 所以减法线程会阻塞1s(此时调用线程是减法线程) ,1s 之后由于加法线程还没有执行完成,所以返回值为 false,减法线程继续执行,减法线程执行完毕后,又过了一会,加法线程才继续执行。这样就会得出我们上面的运行结果。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值