Parallel.Invoke 基本使用
来源:互联网 发布:成都英文seo外包 编辑:程序博客网 时间:2024/06/05 20:56
using System;using System.Collections.Generic;using System.Text;using System.Threading;using System.Threading.Tasks;namespace Test{ class Program { static void Main() { CurrentThreadInfo("主线程Main()的"); Parallel.Invoke(() => { Work1(); }, () => { Work2(); }); Console.ReadLine(); } private static void Work1() { Console.WriteLine("Work1 run {"); for (int i = 0; i < 10; i++) { Console.WriteLine("\tWork1 ***---{0} {1} 线程Id:{2}", i.ToString(), DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), Thread.CurrentThread.ManagedThreadId); } Console.WriteLine("end Work1 }"); CurrentThreadInfo("Work1()的方法"); } private static void Work2() { Console.WriteLine("Work2 run {"); for (int i = 100; i < 110; i++) { Console.WriteLine("\tWork2 ###_{0} {1} 线程Id:{2}", i.ToString(), DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), Thread.CurrentThread.ManagedThreadId); } Console.WriteLine("end Work2 }"); CurrentThreadInfo("Work2()的方法"); } /// <summary> /// 当前线程信息 /// </summary> private static void CurrentThreadInfo(string name) { StringBuilder builder = new StringBuilder(); builder.AppendLine(""); builder.AppendLine(String.Format("{0}线程Id:\t\t{1}", name, Thread.CurrentThread.ManagedThreadId)); builder.AppendLine(String.Format("{0}是否使用线程池:\t{1}", name, Thread.CurrentThread.IsThreadPoolThread)); builder.AppendLine(String.Format("{0}是否后台线程:\t{1}", name, Thread.CurrentThread.IsBackground)); builder.AppendLine(String.Format("{0}线程状态:\t\t{1}", name, Thread.CurrentThread.ThreadState.ToString())); builder.AppendLine(String.Format("{0}当前时间:\t\t{1}", name, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))); builder.AppendLine(""); Console.WriteLine(builder.ToString()); } }}
/*主线程Main()的线程Id: 10主线程Main()的是否使用线程池: False主线程Main()的是否后台线程: False主线程Main()的线程状态: Running主线程Main()的当前时间: 2016-07-28 23:34:16Work1 run { Work1 ***---0 2016-07-28 23:34:16 线程Id:10 Work1 ***---1 2016-07-28 23:34:16 线程Id:10Work2 run { Work2 ###_100 2016-07-28 23:34:16 线程Id:6 Work2 ###_101 2016-07-28 23:34:16 线程Id:6 Work2 ###_102 2016-07-28 23:34:16 线程Id:6 Work2 ###_103 2016-07-28 23:34:16 线程Id:6 Work2 ###_104 2016-07-28 23:34:16 线程Id:6 Work2 ###_105 2016-07-28 23:34:16 线程Id:6 Work2 ###_106 2016-07-28 23:34:16 线程Id:6 Work2 ###_107 2016-07-28 23:34:16 线程Id:6 Work2 ###_108 2016-07-28 23:34:16 线程Id:6 Work1 ***---2 2016-07-28 23:34:16 线程Id:10 Work1 ***---3 2016-07-28 23:34:16 线程Id:10 Work1 ***---4 2016-07-28 23:34:16 线程Id:10 Work1 ***---5 2016-07-28 23:34:16 线程Id:10 Work1 ***---6 2016-07-28 23:34:16 线程Id:10 Work1 ***---7 2016-07-28 23:34:16 线程Id:10 Work1 ***---8 2016-07-28 23:34:16 线程Id:10 Work1 ***---9 2016-07-28 23:34:16 线程Id:10 Work2 ###_109 2016-07-28 23:34:16 线程Id:6end Work2 }Work2()的方法线程Id: 6Work2()的方法是否使用线程池: TrueWork2()的方法是否后台线程: TrueWork2()的方法线程状态: BackgroundWork2()的方法当前时间: 2016-07-28 23:34:16end Work1 }Work1()的方法线程Id: 10Work1()的方法是否使用线程池: FalseWork1()的方法是否后台线程: FalseWork1()的方法线程状态: RunningWork1()的方法当前时间: 2016-07-28 23:34:16 */
总结:
Parallel.Invoke 使用的是:前台主线程 + 线程池后台线程。
转自:http://www.cnblogs.com/rose0705/articles/5716766.html
阅读全文
0 0
- Parallel.Invoke 基本使用
- 使用Parallel.Invoke并行你的代码
- Parallel.For & Parallel.ForEach & Parallel.Invoke
- Task Parallel.For、Parallel.ForEach、Parallel.Invoke
- Parallel.Invoke中Cancel
- C# Parallel.Invoke 实现
- C#并行运算 Parallel.Invoke、Parallel.For、Parallel.Foreach性能测试及示例
- 基于任务的并发编程之Parallel.Invoke(一)
- [Azure]基于Invoke-Parallel对Azure ARM虚拟机批量开关机
- C# 使用Parallel并行开发Parallel.For、Parallel.Foreach实例
- Oracle Parallel使用
- Parallel多线程使用
- ConstroInfo 的Invoke使用
- Java中的invoke使用
- Java中的invoke使用
- Invoke的使用小结
- java 反射 invoke使用
- unity ~ Invoke的使用
- android系统6.0权限
- 关于APPCAN的UI基础框架的定义,说明,场景和例子
- java之静态导入
- Android网络连接的判断
- python Beautiful soup网页解析-星座网
- Parallel.Invoke 基本使用
- python-获取文件名,取整
- (OK) patent-1 (专利-1) 一种用于移动互联网协议的测试平台及测试方法
- React Native优秀资源
- [无视][mark]在Ubuntu下使用vscode编程、编译、调试、单步调试C++
- ArcGIS Engine调用自定义GP服务(二)
- 利用JFrame创建五子棋游戏(最简版)
- 点是否在一条直线判断-共线判断
- import.. from..分析