多线程学习之多线程的控制

1.Thread的构造参数是一个委托delegate

2.thread.Join()用来堵塞thread直到完成所有

3.thread.Abort()用来停止线程,终止线程,原理是抛出异常的办法强制停止线程,危险,不建议使用。

4.可以使用bool变量控制多线程的运行。for循环,需要bool变量在for循环里面控制,防止出现继续执行的bug。

5.线程使用Priority控制优先级

6.多线程状态:

        Running = 0,
        //
        // 摘要:
        //     正在请求线程停止。这仅用于内部。
        StopRequested = 1,
        //
        // 摘要:
        //     正在请求线程挂起。
        SuspendRequested = 2,
        //
        // 摘要:
        //     线程正作为后台线程执行(相对于前台线程而言)。此状态可以通过设置 System.Threading.Thread.IsBackground 属性来控制。
        Background = 4,
        //
        // 摘要:
        //     尚未对线程调用 System.Threading.Thread.Start 方法。
        Unstarted = 8,
        //
        // 摘要:
        //     线程已停止。
        Stopped = 16,
        //
        // 摘要:
        //     线程已被阻止。这可能是因为:调用 System.Threading.Thread.Sleep(System.Int32) 或 System.Threading.Thread.Join、请求锁定(例如通过调用
        //     System.Threading.Monitor.Enter(System.Object) 或 System.Threading.Monitor.Wait(System.Object,System.Int32,System.Boolean))或等待线程同步对象(例如
        //     System.Threading.ManualResetEvent)。
        WaitSleepJoin = 32,
        //
        // 摘要:
        //     线程已挂起。
        Suspended = 64,
        //
        // 摘要:
        //     已对线程调用了 System.Threading.Thread.Abort(System.Object) 方法,但线程尚未收到试图终止它的挂起的 System.Threading.ThreadAbortException。
        AbortRequested = 128,
        //
        // 摘要:
        //     线程状态包括 System.Threading.ThreadState.AbortRequested 并且该线程现在已死,但其状态尚未更改为 System.Threading.ThreadState.Stopped。
        Aborted = 256


using System;
using System.Threading;

namespace 多线程学习
{
    class Program
    {
        static void RunThreads()
        {
           
        }
        static void Main(string[] args)
        {
            try
            {
                ThreadSample sample = new ThreadSample();
                Thread thread = new Thread(sample.CountNumbers);
                Console.WriteLine("[instance a object] child thread ID = {0},state = {1}", thread.ManagedThreadId, thread.ThreadState);
                thread.Start();
                Console.WriteLine("[start] child thread ID = {0},state = {1}", thread.ManagedThreadId, thread.ThreadState);
                Thread.Sleep(TimeSpan.FromSeconds(6));//等待6秒后,即刻执行下面步骤
                Console.WriteLine("[thread sleep] child thread ID = {0},state = {1}", thread.ManagedThreadId, thread.ThreadState);
                //thread.Abort();//Thread 以抛出异常的方法停止线程运行,不建议这样停止线程
                //thread.Join();//堵塞线程,直到子线程完成。
                sample.Stop();//正常停止,使用bool型变量控制线程运行,但是线程还在执行,需要赋值null,完全停止线程。
                Console.WriteLine("[stop] child thread ID = {0},state = {1}", thread.ManagedThreadId, thread.ThreadState);
                //安全的关闭线程
                if (thread != null || thread.IsAlive)
                {
                    thread = null;
                    Console.WriteLine("线程关闭成功");
                }
                else
                {
                    Console.WriteLine("线程关闭失败");
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine("Error " + ex.ToString());
            }
            Console.ReadKey();
        }


    }

    class ThreadSample
    {
        private bool isStopped = false;

        public void Stop()
        {
            isStopped = true;
        }

        //int i = 0;
        public void CountNumbers()
        {
            try
            {
                //while(!isStopped)
                {
                    for (int i = 0; i < 10; i++)
                    {
                        if (isStopped)
                            break;
                        Console.WriteLine("支线任务。。。。" + i);
                        Thread.Sleep(TimeSpan.FromSeconds(1));
                    }
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine("计数器 Error " + ex.ToString());
            }
        }
    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值