因为有个for 实际执行结果尽管是按照for里边的顺序执行,但处理器让哪个分线程先执行,谁先处理完就不一定了.

对于要求结果需要先后顺序的,比如对text内容的操作, 用并行 Parallel.For 操作,不做进一步优化处理,那结果就不是想要的了,还要l用它的并行的功能所以要多程序进行改进,

我使用的做法是初始化buffer list 把list数组的顺序定下来,在循环中,把对应的值顺序赋值给list.    这样做可能写的有点死,根据实际应用调整启动循环的数量实际运行起来效果不错.

做了个简单的测试,同时赋值多个字符串,打印list字符串的值,

 string a = "sssssssssssssssssssssssssssssssssssssssssss";
string b = "jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj";
string c = "nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnmmm";
string d = "ssssssssssssssssssssssssssssssssssssssssssssssssss";
string o = "hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh";
string f = "ttttttttttttttttttttttttttttttttttttttttttttttttttt";
List<string> la = new List<string>();
la.Add(a);
la.Add(b);
la.Add(c);
la.Add(d);
la.Add(o);
la.Add(f);
Parallel.For(, /*portion*/, (i, ParallelLoopState) =>
{
Console.WriteLine(la[i]) ;
});

结果:

顺序大变

再运行一次

因为设置断点,程序停了一会显示按照先后了

在运行一次,去掉断点

总结:对需要先后顺序的操作明显满足不了

本来已经加上一下代码就可以了

                  if (i == )
{
using (mmf_reader = mmf.CreateViewAccessor(, portion1, MemoryMappedFileAccess.Read))
{
buffer = new byte[portion1]; mmf_reader.ReadArray(, buffer, , (int)portion1);
mappedFiles.Add(new MappedFile
{
Offset =/* i **/ portion1, //fileOffset,
Buffer = buffer,
FileSize = portion1
});
}
}
else if (i == )
{
using (mmf_reader = mmf.CreateViewAccessor(portion1, portion2 - portion1, MemoryMappedFileAccess.Read))
{
buffer = new byte[portion2 - portion1]; mmf_reader.ReadArray(, buffer, , (int)(portion2 - portion1));
mappedFiles.Add(new MappedFile
{
Offset = portion1 + portion2,// i * portionSize, //fileOffset,
Buffer = buffer,
FileSize = portion2 - portion1
});
}
}
else if (i == )
{
using (mmf_reader = mmf.CreateViewAccessor(portion2, portion3 - portion2, MemoryMappedFileAccess.Read))
{
buffer = new byte[portion3 - portion2]; mmf_reader.ReadArray(, buffer, , (int)(portion3 - portion2));
mappedFiles.Add(new MappedFile
{
Offset = portion3 + portion2,// i * portionSize, //fileOffset,
Buffer = buffer,
FileSize = portion3 - portion2
});
}
}
else if (i == )
{
using (mmf_reader = mmf.CreateViewAccessor(portion2, portion3 - portion2, MemoryMappedFileAccess.Read))
{
buffer = new byte[portion3 - portion2]; mmf_reader.ReadArray(, buffer, , (int)(portion3 - portion2));
mappedFiles.Add(new MappedFile
{
Offset = portion3 + portion2,// i * portionSize, //fileOffset,
Buffer = buffer,
FileSize = portion3 - portion2
});
}
}
else if (i == )
{
using (mmf_reader = mmf.CreateViewAccessor(portion3, portion4 - portion3, MemoryMappedFileAccess.Read))
{
buffer = new byte[portion4 - portion3]; mmf_reader.ReadArray(, buffer, , (int)(portion4 - portion3));
mappedFiles.Add(new MappedFile
{
Offset = portion3 + portion2,// i * portionSize, //fileOffset,
Buffer = buffer,
FileSize = portion4 - portion3
});
}
}
else if (i == )
{
using (mmf_reader = mmf.CreateViewAccessor(portion4, portion5 - portion4, MemoryMappedFileAccess.Read))
{
buffer = new byte[portion5 - portion4]; mmf_reader.ReadArray(, buffer, , (int)(portion5 - portion4));
mappedFiles.Add(new MappedFile
{
Offset = portion3 + portion2,// i * portionSize, //fileOffset,
Buffer = buffer,
FileSize = portion5 - portion4
});
}
}
}

以上尽管值加进去了,里边顺序还是变了并行线程用起来还是有些复杂度的,

对以上

mappedFiles 先初始化 ,
 mappedFiles = new List<MappedFile>();
//初始化 list 六个
for (int i = ; i < ; i++)
{
MappedFile map = new MappedFile();
mappedFiles.Add(map);
mappedFiles[]=new MappedFile
{
// Offset =/* i **/ portion1, //fileOffset,
Buffer = buffer,
// FileSize = portion1
};

不用list.add的方式,直接赋值方式.问题解决

 

c# Parallel.For 并行编程 执行顺序测试的更多相关文章

  1. ORACLE中查询语句的执行顺及where部分条件执行顺序测试

    Oracle中的一些查询语句及其执行顺序 原文地址:https://www.cnblogs.com/likeju/p/5039115.html 查询条件: 1)LIKE:模糊查询,需要借助两个通配符, ...

  2. Unity脚本在层级面板中的执行顺序测试2

    上一篇测试了生成顺序对执行顺序的影响,链接:LINK 执行顺序测试3: LINK 这篇主要测试一下Awake,OnEnable,Start三个常用消息的循环顺序 1.测试消息循环顺序 先上一个最简单的 ...

  3. Unity脚本在层级面板中的执行顺序测试3

    断断续续的写了3篇,以后有时间可以做成一个系列了 前面2篇测试了GameObject的顺序,以及Awake和OnEnable的时机: Unity脚本在层级面板中的执行顺序测试1 http://www. ...

  4. Unity脚本在层级面板中的执行顺序测试4-附加整理

    测试4为一些附加内容,后续的各种tips都加在此. 前几篇测试的链接: Unity脚本在层级面板中的执行顺序测试1 http://www.cnblogs.com/hont/p/4298110.html ...

  5. Unity脚本在层级面板中的执行顺序测试1

    第二篇测试循环时和动态创建时的调用顺序:LINK 测试版本Unity4.6.因为新版本对Transform的排序做了改变,所以不排除旧版本的测试结果不一样.测试时,使用Awake中添加Debug.lo ...

  6. Spring的Bean的生命周期方法执行顺序测试

    通过一个简单的Maven工程来演示Spring的Bean生命周期函数的执行顺序. 下面是工程的目录结构: 直接贴代码: pom.xml文件内容: <?xml version="1.0& ...

  7. Animation在每一帧中的执行顺序测试

    测试代码: void Update() { transform.position = Vector3.zero; } void LateUpdate() { Debug.Log(transform.p ...

  8. C#并行编程-Parallel

    菜鸟学习并行编程,参考<C#并行编程高级教程.PDF>,如有错误,欢迎指正. 目录 C#并行编程-相关概念 C#并行编程-Parallel C#并行编程-Task C#并行编程-并发集合 ...

  9. Task C# 多线程和异步模型 TPL模型 【C#】43. TPL基础——Task初步 22 C# 第十八章 TPL 并行编程 TPL 和传统 .NET 异步编程一 Task.Delay() 和 Thread.Sleep() 区别

    Task C# 多线程和异步模型 TPL模型   Task,异步,多线程简单总结 1,如何把一个异步封装为Task异步 Task.Factory.FromAsync 对老的一些异步模型封装为Task ...

随机推荐

  1. Android内存优化相关

    Android的内存管理方式 Android系统内存分配与回收方式 一个APP通常就是一个进程对应一个虚拟机 GC只在Heap剩余空间不够时才去垃圾回收 GC触发时,所有线程都会被暂停!!! APP内 ...

  2. 获取jdk支持的编码类型

    //获取jdk支持的编码类型 Map<String,Charset> maps = Charset.availableCharsets(); for(Map.Entry<String ...

  3. ArcGIS 栅格数据教程

    ArcGIS 栅格数据教程 全部8个教程,带详细操作步骤和原始数据. 技术咨询:谢老师,135_4855_4328,xiexiaokui#139.com ArcGIS 10.5 此教程中的练习将使用样 ...

  4. C# windows服务:创建Windows服务(Windows Services)的一般步骤

    C#创建Windows服务(Windows Services) Windows服务在Visual Studio 以前的版本中叫NT服务,在VS.net启用了新的名称.用Visual C# 创建Wind ...

  5. 吴裕雄 python神经网络 花朵图片识别(9)

    import osimport numpy as npimport matplotlib.pyplot as pltfrom PIL import Image, ImageChopsfrom skim ...

  6. python基础学习 Day19 面向对象的三大特性之多态、封装 property的用法(1)

    一.课前内容回顾 继承作用:提高代码的重用性(要继承父类的子类都实现相同的方法:抽象类.接口) 继承解释:当你开始编写两个类的时候,出现了重复的代码,通过继承来简化代码,把重复的代码放在父类中. 单继 ...

  7. python错题整理

    1.列表list去重 l1 = [1,1,2,3,5,5,4,4,4,5,6] set1 = set(l1) # print(set1) # set是集合 l2 = list(set1) # 将集合转 ...

  8. rectangle,boundingRect和Rect

    rectangle( rook_image, Point( , *w/8.0 ), Point( w, w), Scalar( , , ), , ); 矩形将被画到图像 rook_image 上 矩形 ...

  9. Trustin Lee

    Trustin Lee,MINA.Netty2通讯框架的作者韩国人,80年出生,8岁起在MSX迷你计算机上编写BASIC程序,爱好游戏编程以及使用汇编.C和C++解决编程问题,1998年获得韩国信息奥 ...

  10. Executors与ThreadPoolExecutor

    最近阿里发布的 Java开发手册中强制线程池不允许使用 Executors 去创建,而是通过 ThreadPoolExecutor 的方式,这样的处理方式让写的同学更加明确线程池的运行规则,规避资源耗 ...