zoukankan      html  css  js  c++  java
  • 多线程之搬运货物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)
            {
                List<string> productList = new List<string>() {"AAAA","BBBB","CCCC","DDDD","EEEE" };//货物
                MoveHelper moveHelper = new MoveHelper();

                //两个人或多个人时
                int personCount = 6;
                Dictionary<string, List<string>> person_Product = new Dictionary<string, List<string>>();//人与货物的对应表
                
    //给人分配任务
                while (productList.Count > 0)
                {
                    for (int i = 0; i < personCount; i++)
                    {
                        if (productList.Count <= 0break;
                        string key = "P" + (i+1).ToString();
                        List<string> plist=new List<string>();
                        if (!person_Product.Keys.Contains(key))
                            person_Product.Add(key, plist);
                        else plist = person_Product[key];
                        plist.Add(productList[0]);
                        productList.RemoveAt(0);
                    }
                }
                //有多个人就创建多个个线程
                foreach (string str in person_Product.Keys)
                {
                    Thread thread = new Thread(new ParameterizedThreadStart(moveHelper.MoveThings));
                    MoveParameters ps = new MoveParameters(str, person_Product[str]);
                    thread.Start(ps);
                    //thread.Join();//当加上join后,所用时间是单线程的时间。并不会少时间。
                }

                Console.WriteLine("ddddddddddddddddddddddddddddddddddd");
                Console.ReadLine();
            }
        }

        public class MoveHelper
        {
            public void MoveThings(object product)
            {
                MoveParameters ps = product as MoveParameters;

                if (ps.Products != null && ps.Products.Count > 0)
                {
                    foreach (string pro in ps.Products)
                    {
                        System.Threading.Thread.Sleep(1000);
                        Console.WriteLine("货物" + pro + "已经被" + ps.PersonName + "成功送达目的地!" + System.DateTime.Now.ToString());//YYYY-MM-DD HH-MM-mm
                    }
                }
            }
        }

        public class MoveParameters
        {
            public string PersonName { getset; }
            public List<string> Products { getset; }

            public MoveParameters() { }

            public MoveParameters(string personName, List<string> products)
            {
                this.PersonName = personName;
                this.Products = products;
            }
        }
    }
  • 相关阅读:
    PAT 1065. A+B and C (64bit) (20)
    PAT 1042. Shuffling Machine (20)
    PAT 1001. A+B Format (20)
    HDU 2082 找单词 母函数
    NYOJ 138 找球号(二) bitset 二进制的妙用
    POJ 1151 Wormholes spfa+反向建边+负环判断+链式前向星
    POJ 1511 Invitation Cards 链式前向星+spfa+反向建边
    zzuli 2130: hipercijevi 链式前向星+BFS+输入输出外挂
    NYOJ 323 Drainage Ditches 网络流 FF 练手
    POJ 1273 Drainage Ditches 网络流 FF
  • 原文地址:https://www.cnblogs.com/pnljs/p/3520934.html
Copyright © 2011-2022 走看看