C#多线程整理
来源:互联网 发布:php导航源码 编辑:程序博客网 时间:2024/06/13 17:30
C#多线程整理
Thread
//直接运行new Thread((s)=>Console.WriteLine(s)).Start("thread1")Thread t = new Thread(delegate () { Console.WriteLine("fuck♂you2"); Thread.Sleep(1000); });t.IsBackground = true; //程序推出后结束,默认falset.Start(); //运行t.Abort(); //停止t.Join();//直到t执行完才进行主线程int[] a = { 1, 3, 5, 2, 4, 6 }; //延时排序for (int i = 0; i < a.Count(); i++){ new Thread(ss).Start(a[i]);}wh.Set();
static EventWaitHandle wh = new EventWaitHandle(false, EventResetMode.ManualReset); private static void ss(object num){ int n = (int)num; wh.WaitOne(); Thread.Sleep(n*100); Console.Write(n.ToString() + ' ');}
ThreadPool
for (int i = 0; i < 5; i++){ ThreadPool.QueueUserWorkItem(fun,"Hello from threadpool"+i.ToString());}
private static void fun(object var) { string s = (string)var; Console.WriteLine(var);}
Task
Task<string> t1 = Task.Run(() => "task1");t1.Wait();Console.WriteLine(t1.Result + " form task");Task<int> t3 = new Task<int>((x)=> { int num = (int)x; return num * 2; }, 1);t3.Start();Task<int> t4 = t3.ContinueWith<int>((x) => { int num = 2*x.Result; Console.WriteLine(num.ToString());return num; });//t3执行后执行Task.Delay(TimeSpan.FromSeconds(2)).ContinueWith(dl => { Console.WriteLine("bye"); }); //延时后执行
async await
asfun();
private static async void asfun(){ int res = await cal(); Console.WriteLine(res.ToString()+" async end");}private static async Task<int> cal(){ int a=1; for(int i=0;i<10;i++) { a *= 2; } return a; }
0 0
- C#多线程整理
- C#多线程整理
- C#多线程整理(一)
- C#多线程整理(二)
- C# 多线程文件读写整理总结
- 这篇文章主要是为了整理 记录 c# 多线程 的理解
- 多线程整理
- c#多线程
- C#多线程
- C#多线程
- C#多线程
- C#多线程
- 多线程c#
- c#多线程
- c# 多线程
- C# 多线程
- C#多线程
- c# 多线程
- Hdu-5886 Tower Defence(树形DP)
- leetcode刷题笔记-Reverse Integer
- STL迭代器
- HDU1996 汉诺塔VI
- 共享内存
- C#多线程整理
- hibernate事务
- Leetcode 107. Binary Tree Level Order Traversal II 二叉树层次遍历2 解题报告
- elasticsearch之mapping配置
- BGRABitmap图像操作12:四行不同效果文本输出
- 在Centos7上安装mongodb-linux-i686-2.6.7.tgz
- |洛谷|排序|P1309 瑞士轮
- 原生NavigationView菜单中添加消息提醒(小红点)
- (document).height()、$(document).scrollTop()