• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C# 多线程

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

//----控制台程序代码

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

namespace duoxiancheng
{
class Program
{
static void Main(string[] args)
{
//HouseMovingCompany.Instance.Contracts.Add(new Contract { From = "WuDaokou", To = "LinDa Road", Fee = 500 });
//HouseMovingCompany.Instance.Contracts.Add(new Contract { From = "XiDan", To = "WangFujing", Fee = 1000 });
//HouseMovingCompany.Instance.Contracts.Add(new Contract { From = "XiangShan", To = "The Forbidden City", Fee = 10000 });
//while (HouseMovingCompany.Instance.Contracts.Count > 0)
//{
// HouseMovingCompany.Instance.MoveHouse();
//}


HouseMovingCompany.Instance.Contracts.Add(new Contract { From = "五道口", To = "林大道", Fee = 500 });
HouseMovingCompany.Instance.Contracts.Add(new Contract { From = "西单", To = "王府井", Fee = 1000 });
HouseMovingCompany.Instance.Contracts.Add(new Contract { From = "香山", To = "故宫", Fee = 10000 });

Thread thread = null;

while (HouseMovingCompany.Instance.Contracts.Count > 0)
{
thread = new Thread(new ThreadStart(HouseMovingCompany.Instance.MoveHouse));

thread.Start();
}

Console.ReadLine();
}
}
}

//--Contract类

public class Contract
{
public string ID { get; private set; }
public string From { get; set; }
public string To { get; set; }
public decimal Fee { get; set; }

public Contract()
{
this.ID = Guid.NewGuid().ToString();
}
}

 

//---------HouseMovingCompany 类

public class HouseMovingCompany
{
private static HouseMovingCompany _instance = null;
public static HouseMovingCompany Instance
{
get { return (_instance == null ? _instance = new HouseMovingCompany() : _instance); }
}

public List<Contract> Contracts { get; private set; }

public HouseMovingCompany()
{
this.Contracts = new List<Contract>();
}

//public void MoveHouse()
//{
// if (this.Contracts == null || this.Contracts.Count == 0)
// {
// return;
// }

// Contract contract = 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);
// Console.WriteLine("时间:{2} 从 {0} 搬家至 {1}.", contract.From, contract.To, System.DateTime.Now.ToString("yyyy-mm-dd hh:mm:ss.ffff"));

// }

// Thread.Sleep(5000);
//}


public void MoveHouse()
{
if (this.Contracts == null || this.Contracts.Count == 0)
{
return;
}

Contract contract = null;

lock (this.Contracts)
{
contract = this.Contracts[0];
this.Contracts.RemoveAt(0);
}

if (!String.IsNullOrEmpty(contract.From) && !String.IsNullOrEmpty(contract.To))
{
Console.WriteLine("时间:{2} 从 {0} 搬家至 {1}.", contract.From, contract.To, System.DateTime.Now.ToString("yyyy-mm-dd hh:mm:ss.ffff"));


}

Thread.Sleep(5000);
}


}


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
c++ 显示类型转换发布时间:2022-07-13
下一篇:
C#中的DLL注入 (转)发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap