C#线程示例二

这是一个线简单明了的线程小程序,用于输出给定数内的奇、偶数。

using System;

using System.Collections.Generic;

using System.Text;

using System.Threading;

namespace JiOuShu

{

    class Program

    {

        static void Main(string[] args)

        {

            //ThreadStart thrSta = new ThreadStart();

            //实例化一个有参委托

            ParameterizedThreadStart ParTerThrSta1 = new ParameterizedThreadStart(ShuChuJishu);

            Thread thr1 = new Thread(ParTerThrSta1);       //实例化一个线程

            thr1.Name = "线程A:";      //给线程起个名字

            thr1.Priority = ThreadPriority.Highest;   //设置A线程的级别为最高

          

            ParameterizedThreadStart parTerThrSta2 = new ParameterizedThreadStart(ShuChuOushu);

            Thread thr2 = new Thread(parTerThrSta2);

            thr2.Name = "线程B:";

            thr2.Priority = ThreadPriority.Lowest;     //设置B线程的级别为最低

            thr1.Start(50);       //开启线程并传入一个参数

            thr2.Start(50);

        }

        public static void ShuChuJishu(object num)

        {

            for (int i = 0; i <= (int)num; i++)

            {

                if (i % 2 != 0)

                {

                    Console.Write("{0}输出奇数:{1}",Thread.CurrentThread.Name,i);

                    Console.WriteLine();

                    Console.WriteLine();

                    Thread.Sleep(1000);  //设备间隔时间

                }

            }

        }

        public static void ShuChuOushu(object num)

        {

            for (int i = 0; i <= (int)num; i++)

            {

                if (i % 2 == 0)

                {

                    Console.Write("{0}输出偶数:{1}",Thread.CurrentThread.Name,i);

                    Console.WriteLine();

                    Console.WriteLine();

                    Thread.Sleep(1000);  //设备间隔时间

                }

            }

        }

    }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值