C# 多线程学习 二:join()和Sleep()

join and sleep

  1. 调用join()方法,就可以等待另一个线程结束。
  2. 调用join(参数) ,可以设置一个超时,用毫秒或者TimeSpan. 此时有返回值 在超时时间内,如果线程执行结束了返回true 否false
  3. Thread.Sleep(参数)方法。 线程阻塞,到设定时间后继续执行。
    注意:
    Thread.Sleep(0) 这样调用会导致线程会立即放弃本身当前时间片,自动将CPU移交给其他线程。和Thread.YieId()做同样事。 但是YIEID只会把执行交给同一处理器的其他线程。

for text One

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ConsolePlan
{
    class Program
    {
        static void Main(string[] args)
        {
            Thread.CurrentThread.Name = "Main Thread ConsolePlan...";
            Thread threadText = new Thread(PrintChar);
            threadText.IsBackground = true;
            threadText.Name = "SubThread Name...";
            threadText.Start();
			//等待子线程执行完毕在执行主线程
            threadText.Join();

            Console.WriteLine(Thread.CurrentThread.Name);
            for (int i = 0; i < 1000; i++)
            {
                Console.Write('X');
            }

            Console.ReadKey(true);
        }

        static void PrintChar()
        {
            Console.WriteLine(Thread.CurrentThread.ThreadState);
            Console.WriteLine(Thread.CurrentThread.Name);
            for (int i = 0; i < 1000; i++)
            {
                Console.Write('Y');
            }
        }
    }
}

for text Two

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ConsolePlan
{
    class Program
    {
        private static Thread SubThreadOne, SubThreadTwo;
        private readonly static string SubThrOneName = "Thread1";
        private const string SubThrTwoName = "Thread2";

        static void Main(string[] args)
        {
            SubThreadOne = new Thread(Go);
            SubThreadOne.Name = SubThrOneName;
            SubThreadOne.Start();

            SubThreadTwo = new Thread(Go);
            SubThreadTwo.Name = SubThrTwoName;
            SubThreadTwo.Start();

            Console.ReadKey(true);
        }

        static void Go()
        {
            string curName = Thread.CurrentThread.Name;
            Console.WriteLine("\nThread Name {0} Thread ID {1}", curName, Thread.CurrentThread.ManagedThreadId);
            if (curName == SubThrOneName && SubThreadTwo.ThreadState != ThreadState.Unstarted)
            {
                SubThreadTwo.Join();
            }

            Thread.Sleep(4000);
            Console.WriteLine("\n Current Thread Name {0}",curName);
            Console.WriteLine(SubThreadOne.ThreadState);
            Console.WriteLine(SubThreadTwo.ThreadState);
        }

    }
}

当前执行结果:
在这里插入图片描述
for Text Three

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ConsolePlan
{
    class Program
    {
        private static TimeSpan SleepTime = new TimeSpan(0, 0, 1);

        static void Main(string[] args)
        {
            Thread thread = new Thread(Text);
            thread.Name = "SubThread";
            thread.Start();

            if (thread.Join(SleepTime))
            {
                Console.WriteLine("thread run end...");
            }
            else
            {
                Console.WriteLine("{0} Join Timed out..",thread.Name);
                Console.WriteLine("ThreadState: {0}  ThreadIsalive: {1}",thread.ThreadState,thread.IsAlive);
            }
            Console.ReadKey(true);
        }

        static void Text()
        {
            Thread.Sleep(SleepTime);
        }

    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

望天hous

你的鼓励是我最大动力~谢谢啦!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值