C#线程

        C#中对线程进行操作时,主要用到了Thread 类 ,该类位于System.Threading命名空间下,创建一个线程非常简单,只需要将其声明并为其提供线程起始点的方法委托即可,创建新的线程时,需要用到Thread 类,Thread类具有接受一个ThreadStart 委托或者ParameterizedThreadStart委托的构造函数(两种特殊的构造函数),


Thread(ThreadStart) 示例

class Program
    {
        public static void ShowInfo() {
            Console.WriteLine("呵呵哒");
        }

        static void Main(string[] args)
        {
            Thread thread = new Thread(new ThreadStart(ShowInfo));
            thread.Start();
            Console.ReadLine();
        }
    }

效果:



该委托包装了调用 Start 方法时由新线程调用的方法,创建了Thread类对象后,线程就已存在并配置,但并未创造实际的线程,这时,只有调用Start方法后,才会创建实际的线程.


线程的加入:

 thread.Join(); //等待该线程终止,原线程才继续运行

 thread.Join( int millis);// 用来设置最久等多少毫秒,超过这个时间后就不等了,如果不写就会一直等下去

 

实际情景就好像:

A线程正在执行  这个创建B线程,并且执行开始执行:Start();并且执行了Join()加入了线程:

 

这个时候A线程会中断执行,B线程执行完后才会继续执行A剩余的操作如下所示:

 

加入后:


示例:

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace ConsoleApp1
{
    class Program
    {
       static Thread A, B;

        /// <summary>
        /// A线程委托事件
        /// </summary>
        public static void ShowInfo()
        {
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("====A=====线程完成:" + (i + 1) * 10 + "%");
                Thread.Sleep(500);
                if (i == 5)
                {
                    B.Start();
                    B.Join();
                }
            }
        }

        /// <summary>
        /// B线程委托事件
        /// </summary>
        public static void showHollow() {
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("====B=====线程完成:" + (i+1) * 10 + "%");
                Thread.Sleep(500);
            }
        }
        static void Main(string[] args)
        {

            A = new Thread(new ThreadStart(ShowInfo));
            B = new Thread(new ThreadStart(showHollow));
            A.Start();
            Console.Read(); 
        }
    }
}

效果:

可见A线程被B线程中断,执行完B后又继续执行A


        再看看先一起执行,然后执行B再执行A的情况

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace ConsoleApp1
{
    class Program
    {
       static Thread A, B;

        /// <summary>
        /// A线程委托事件
        /// </summary>
        public static void ShowInfo()
        {
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("====A=====线程完成:" + (i + 1) * 10 + "%");
                Thread.Sleep(500);
                if (i == 5)
                {
                    B.Join();
                }
            }
        }

        /// <summary>
        /// B线程委托事件
        /// </summary>
        public static void showHollow() {
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("====B=====线程完成:" + (i+1) * 10 + "%");
                Thread.Sleep(500);
            }
        }
        static void Main(string[] args)
        {
            A = new Thread(new ThreadStart(ShowInfo));
            B = new Thread(new ThreadStart(showHollow));
            A.Start();
            B.Start();
            Console.Read(); 
        }
    }
}

效果:

效果是一起执行,然后只执行B直到结束,然后再继续执行A


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值