C#线程

Program.cs

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

namespace Base14
{
    class Program
    {
        static void Main(string[] args)
        {
            //Thread.CurrentThread.Name = "当前运行的线程";
            //Console.WriteLine(Thread.CurrentThread.Name);
            //Console.WriteLine(Thread.CurrentThread.IsBackground);
            //Console.ReadKey();

            //Console.WriteLine(Process.GetCurrentProcess().ProcessName);
            //Console.ReadKey();

            //显示任务资源管理器的进程
            //foreach (Process item in Process.GetProcesses())
            //{
            //    if (item.ProcessName.Contains("ie"))
            //    {
            //        Console.WriteLine(item.ProcessName);
            //    }
            //}
            //Console.ReadKey();

            //网址进程进入
            // Process.Start("iexplore", "http://www.baidu.com");

            //委托
            //ThreadStart ts = new ThreadStart(show);
            //Thread myThread = new Thread(ts);
            //myThread.Name = "newThread";
            启动
            //myThread.Start();
            终止
            myThread.Abort();
            挂起 先不执行
            //myThread.Suspend();
            //Console.WriteLine("哈哈哈哈");
            唤醒 执行线程
            //myThread.Resume();
            //Console.ReadKey();

            //执行线程
            //Thread mythread = new Thread(show);
            //mythread.Start();
            //Console.ReadKey();

            //Thread mythread = new Thread(ThreadMethod);
            //mythread.Start();
            //Thread.Sleep(2000);
            //mythread.Abort();
            //Console.ReadKey();

            //Thread mt = new Thread(ThreadMethod);
            //mt.Start("我是一个参数");
            //Console.ReadKey();


            //Console.WriteLine(Thread.GetDomain().FriendlyName);
            //Console.WriteLine(Thread.GetDomainID());
            //Console.ReadKey();


            //中断
            //Thread mt = new Thread(ThreadMethod);
            //mt.Start();
            //Thread.Sleep(10);
            //mt.Interrupt();
            //Console.ReadKey();



            //Book book = new Book();
            //Thread t1 = new Thread(book.Sale);
            //t1.Name = "第一个线程";
            //Thread t2 = new Thread(book.Sale);
            //t2.Name = "第二个线程";
            //t1.Start();
            //t2.Start();
            //Console.ReadKey();


            for (int i = 0; i < 5; i++)
            {
                Thread thread = new Thread(MyReadom);
                thread.Start();
            }
            Console.ReadKey();



        }

        public static void MyReadom()
        {
            byte[] bytes = Guid.NewGuid().ToByteArray();
            int index = BitConverter.ToInt32(bytes, 0);
            if (index < 0)
            {
                //递归 自己调用自己
                MyReadom();
            }
            else
            {
                Random r = new Random();
                Console.WriteLine(r.Next(index)/10000);
            }

            //Random r = new Random();
            //int index = r.Next(0, 99);
            //Console.WriteLine(index);
        }

        //public static void ThreadMethod(object objects)
        //{
        //    try
        //    {
        //        for (int i = 0; i < 10; i++)
        //        {
        //            //Thread.Sleep(500);
        //            Console.WriteLine(objects);
        //        }
        //    }
        //    catch (Exception e)
        //    {
        //        Console.WriteLine("执行的线程被强制终止");
        //    }

        //}

        //public static void ThreadMethod()
        //{
        //    try
        //    {
        //        for (int i = 0; i < 10; i++)
        //        {
        //            Thread.Sleep(500);
        //            Console.WriteLine(i);
        //        }
        //    }
        //    catch (Exception e)
        //    {
        //        Console.WriteLine("执行的线程被强制终止");
        //    }

        //}

        //public static void show()
        //{
        //    Console.WriteLine("独立休眠5秒");
        //    Thread.Sleep(5000);
        //}
    }
}

Book.cs

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

namespace Base14
{
    class Book
    {
        public int sum = 100;

        public void Sale()
        {
            //this 哪一个线程 在调用 就是哪一个线程
            //lock (this)
            //{
                Thread.Sleep(1000);
                if (sum == 0)
                {
                    Console.WriteLine("书本已售罄");
                    return;

                }
                while (sum > 0)
                {
                    Console.WriteLine("卖出第{0}本书,谁买的{1}", sum, Thread.CurrentThread.Name);
                    sum -= 1;
                }
            //}
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值