• 多线程之搬运货物1:不分堆搬


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

    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Program p = new Program();
                p.Move();
                Console.ReadLine();
            }

            List<string> productList = new List<string>() { "AAAA""BBBB""CCCC""DDDD""EEEE" };//货物
            
            private void Move()
            {
                for(int i = 1; i<= 10;i++)
                {
                    productList.Add(i.ToString());
                }

                ThreadPool.SetMaxThreads(1010);
                lock (this)
                {
                    while (productList.Count > 0)
                    {
                        lock (this)
                        {
                            ThreadPool.QueueUserWorkItem(new WaitCallback(RealMove),productList[0]);
                            productList.RemoveAt(0);
                        }
                    }
                }
            }

            private void RealMove(object product)
            {
                System.Threading.Thread.Sleep(1000);
                Console.WriteLine("货物" + product.ToString() + "已经被成功送达目的地!" + System.DateTime.Now.ToString());//YYYY-MM-DD HH-MM-mm
            }
        }
    }
  • 相关阅读:
    C# Lambda表达式
    .NET轻量级MVC框架:Nancy入门教程(一)——初识Nancy
    SQL中的case when then else end用法
    WPF gif图片不动问题解决
    async(C# 参考)
    File类 ReadAllBytes() ReadAllLines() ReadAllText()
    二维码生成的常用数据格式
    在chrome console加入jquery库
    Reflector反编译WinForm程序重建项目资源和本地资源
    使用Settings.settings存储用户的个性化配置
  • 原文地址:https://www.cnblogs.com/pnljs/p/3523691.html
Copyright © 2020-2023  润新知