zoukankan      html  css  js  c++  java
  • 多线程操作的例子

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Threading;
    namespace ConsoleApplication5
    {
        public class Contract
        {
            private string id;
            private string from;
            private string to;
            private decimal fee;
            public string ID { get { return id; } private set { id = value; } }
            public string From { get { return from; } set { from = value; } }
            public string To { get { return to; } set { to = value; } }
            public decimal Fee { get { return fee; } set { fee = value; } }
            public Contract(string from,string to,decimal fee)
            {
                this.From=from;
                this.To=to;
                this.Fee=fee;
                this.ID = DateTime.Now.ToBinary().ToString().Replace("-", String.Empty);
            }
        }
        public class HouseMovingCompany 
        { 
            private static HouseMovingCompany _instance = null; 
            public static HouseMovingCompany Instance 
            {  
                get { return (_instance == null ? _instance = new HouseMovingCompany() : _instance); } 
            }
            private List<Contract> cc;
            public List<Contract> Contracts
            {
                get { return cc; }
                private set{cc=value;} }
            public HouseMovingCompany() 
            { 
                this.Contracts = new List<Contract>();
            } 
            public void MoveHouse() 
            {
                if (this.Contracts == null || this.Contracts.Count == 0)
                {
                    return;
                }
                Contract contract;
                lock (Contracts)
                {
                     contract = this.Contracts[0];
                }
                this.Contracts.RemoveAt(0); 
                if (!String.IsNullOrEmpty(contract.From) && !String.IsNullOrEmpty(contract.To)) 
                { 
                    Console.WriteLine("Move the house from {0} to {1}.", contract.From, contract.To);
                } 
                Thread.Sleep(5000); 
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
               // Contract cc=new Contract();

                HouseMovingCompany.Instance.Contracts.Add(new Contract ("WuDaokou", "LinDa Road", 500 ));
                HouseMovingCompany.Instance.Contracts.Add(new Contract ("XiDan",  "WangFujing",1000 ));
                HouseMovingCompany.Instance.Contracts.Add(new Contract ("XiangShan",  "The Forbidden City", 10000 )); 
                Thread thread = null;
                while (HouseMovingCompany.Instance.Contracts.Count > 0) 
                { 
                    thread = new Thread(new ThreadStart(HouseMovingCompany.Instance.MoveHouse)); 
                    thread.Start(); 
                }
                Console.Read();
            }
        }
    }

  • 相关阅读:
    关于加密程序
    C++11的新特性lambda的小试牛刀RAII
    自动生成makefile的脚本
    关于pcre正则表达式库libpcre
    利用PHP执行SQL文件,将SQL文件导入到数据库
    Linux 系统是否适合于您?
    一个少年电脑病毒作者的独白
    PHP编程效率的20个要点
    JVM源码分析之javaagent原理完全解读
    如何更好的利用Node.js的性能极限
  • 原文地址:https://www.cnblogs.com/lijinchang/p/1961303.html
Copyright © 2011-2022 走看看