zoukankan      html  css  js  c++  java
  • 代码段

    json反序列化集中简单方式比较 资源分配多线程demo
    class
    Program { public static object mux = new object(); static List<Resource> resoureList = new List<Resource>(); static void Main(string[] args) { Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); for (int i = 0; i < 5000; i++) { string json = "{name:"li",detail:{age:"28",height:"186"}}"; dynamic jsonobject = JsonConvert.DeserializeObject<dynamic>(json); string s = jsonobject.detail.age.ToString(); } Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); for (int i = 0; i < 5000; i++) { string json = "{name:"li",detail:{age:"28",height:"186"}}"; jsonclass jsonobject = JsonConvert.DeserializeObject<jsonclass>(json); string s = jsonobject.detail.age.ToString(); } Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); for (int i = 0; i < 5000; i++) { string json = "{name:"li",detail:{age:"28",height:"186"}}"; Dictionary<string, object> jsonobject = JsonConvert.DeserializeObject<Dictionary<string, object>>(json); Dictionary<string, string> detailobject = JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonobject["detail"].ToString()); string s = detailobject["age"].ToString(); } Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); string sn = string.Empty; return; int worker = GetInputNumber("please input workers number:"); int resoureses = GetInputNumber("please input resoureses number:"); for (int i = 0; i < resoureses; i++) { Resource re = new Resource(); re.name = "resource" + i; resoureList.Add(re); } for (int i = 0; i < worker; i++) { Worker workers = new Worker(); workers.name = "work" + i; workers.TryToGetResourceEvent += workers_TryToGetResourceEvent; workers.DoWorkEvent += workers_DoWorkEvent; workers.ReleaseResourceEvent += workers_ReleaseResourceEvent; Thread td = new Thread(new ThreadStart(workers.GetResourse)); td.IsBackground = true; td.Start(); } Console.ReadKey(); } static void workers_ReleaseResourceEvent(Worker currentObject) { Console.WriteLine("{0} release {1} complete !!!!!!!!!!!!!!!!!!", currentObject.name, currentObject.distributeResource.name); lock (mux) { Console.WriteLine("{0} current resoucelist {1};", currentObject.name, resoureList.Count); resoureList.Add(currentObject.distributeResource); Console.WriteLine("{0} current resoucelist {1};", currentObject.name, resoureList.Count); } //throw new NotImplementedException(); } static void workers_DoWorkEvent(Worker currentObject) { Console.WriteLine("{0} do work sleep", currentObject.name); Thread.Sleep(2000); workers_ReleaseResourceEvent(currentObject); //throw new NotImplementedException(); } static void workers_TryToGetResourceEvent(Worker currentObject) { while (!currentObject.haveResouceFlag) { //Console.WriteLine("{0} try to get resource ", currentObject.name); lock (mux) { //Console.WriteLine("{0} current resoucelist {1};" ,currentObject.name, resoureList.Count); if (resoureList.Count > 0) { currentObject.distributeResource = resoureList[0]; resoureList.RemoveAt(0); Console.WriteLine("{0} current resoucelist {1};", currentObject.name, resoureList.Count); Console.WriteLine("{0} get resource {1}", currentObject.name, currentObject.distributeResource.name); currentObject.haveResouceFlag = true; } } if (currentObject.haveResouceFlag) { workers_DoWorkEvent(currentObject); } } } private static int GetInputNumber(string message) { Console.WriteLine(message); string workers = Console.ReadLine(); int workersInt; if (!int.TryParse(workers, out workersInt)) { Console.WriteLine("number must be number"); return GetInputNumber(message); } else { return workersInt; } } } public class jsonclass { public string name; public detailclass detail; public class detailclass { public string age; public string height; } } public class Resource { public string name { get; set; } } public class Worker { public string name { get; set; } public delegate void ReleaseResource(Worker currentObject); public event ReleaseResource ReleaseResourceEvent; public delegate void GettedResource(); public event GettedResource GettedResourceEvent; public delegate void TryToGetResource(Worker currentObject); public event TryToGetResource TryToGetResourceEvent; public delegate void DoWork(Worker currentObject); public event DoWork DoWorkEvent; public Resource distributeResource { get; set; } public bool haveResouceFlag = false; public void GetResourse() { if (TryToGetResourceEvent != null) { TryToGetResourceEvent(this); } else { Console.WriteLine("{0} don't should to Get Resource", name); } } }
  • 相关阅读:
    局部人物磨皮(二)
    可选颜色--六中色调的调整(二)
    可选颜色--六中色调的调整(一)
    通道混合器
    系统提权
    在NSObject类中,和继承它的类中,弹出UIAlertcontroller和push、present到其它界面
    oc中代理的简单运用
    单击和双击手势冲突的解决,取消页面所有手势
    iOS中主题切换模拟
    iOS 中各种手势的用法
  • 原文地址:https://www.cnblogs.com/blueker-li/p/3337255.html
Copyright © 2011-2022 走看看