多线程

线程

class Program
{
    static void Main(string[] args)
    {
        //可以传入不带参数的方法
        Thread backThread = new Thread(new ThreadStart(Worker));
        //设置为后台线程,默认为前台线程
        backThread.IsBackground = true;
        backThread.Start();
        //后台线程结束后才执行主线程
        backThread.Join();
        Console.WriteLine("从主线程退出");
        //可以传入带一个参数的方法
        Thread paramThread = new Thread(new ParameterizedThreadStart(Worker));
        paramThread.Name = "线程1";
        paramThread.Start("123");
    }

    public static void Worker()
    {
        Thread.Sleep(1000);
        Console.WriteLine("从后台线程退出");
    }

    public static void Worker(object data)
    {
        Thread.Sleep(1000);
        Console.WriteLine("传入的参数为{0}", data.ToString());
        Console.WriteLine("{0}退出", Thread.CurrentThread.Name);
        Console.Read();
    }
}

线程池

class Program
{        
    static void Main(string[] args)
    {
        Console.WriteLine("主线程ID={0}", Thread.CurrentThread.ManagedThreadId);
        ThreadPool.QueueUserWorkItem(CallBackWorkItem);
        ThreadPool.QueueUserWorkItem(CallBackWorkItem, "back");
        Thread.Sleep(3000);
        Console.WriteLine("线程退出");
    }

    public static void CallBackWorkItem(object state)
    {
        Console.WriteLine("线程池线程开始运行");
        if (state != null)
        {
            Console.WriteLine("线程池线程ID为{0},传入的参数为{1}", Thread.CurrentThread.ManagedThreadId, state.ToString());
        }
        else
        {
            Console.WriteLine("线程池线程ID为{0}", Thread.CurrentThread.ManagedThreadId);
        }
    }
}    

协作式取消线程池线程

class Program
{    
    static void Main(string[] args)
    {
        Console.WriteLine("主线程运行");
        CancellationTokenSource cts = new CancellationTokenSource();
        ThreadPool.QueueUserWorkItem(CallBack, cts.Token);
        Console.WriteLine("按下任意键取消操作");
        Console.ReadKey();
        cts.Cancel();//取消请求
        Console.ReadKey();
    }

    public static void CallBack(object state)
    {
        CancellationToken token = (CancellationToken)state;
        Console.WriteLine("开始计数");
        Count(token, 1000);//开始计数
    }

    public static void Count(CancellationToken token, int countto)
    {
        for (int i = 0; i < countto; i++)
        {
            if (token.IsCancellationRequested)
            {
                Console.WriteLine("计数取消");
                return;
            }
            Console.WriteLine("计数为{0}", i);
            Thread.Sleep(300);
        }
        Console.WriteLine("计数完成");
    }
}        

线程同步

class Program
{
    static int tickets = 100;
    static object gloalObj = new object();//辅助对象
    static void Main(string[] args)
    {
        Thread thread1 = new Thread(SaleTicketThread1);
        Thread thread2 = new Thread(SaleTicketThread2);
        thread1.Start();
        thread2.Start();
        Console.ReadKey();
    }

    public static void SaleTicketThread1()
    {
        while (true)
        {
            lock (gloalObj)
            {
                if (tickets > 0)
                {
                    Console.WriteLine("线程1出票:{0}", tickets--);
                }
                else
                {
                    break;
                }
            }
        }
    }

    public static void SaleTicketThread2()
    {
        while (true)
        {
            lock (gloalObj)
            {
                if (tickets > 0)
                {
                    Console.WriteLine("线程2出票:{0}", tickets--);
                }
                else
                {
                    break;
                }
            }
        }
    } 
}    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值