因为有个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. django使用restframework实现安全的api

    参考地址:https://github.com/tomchristie/django-rest-framework/ 一般如果在批量修改多的时候,不建议使用,一般在get请求,或者修改单条数据的时候使 ...

  2. hadoop-2.7.3完全分布式部署

    一.环境介绍      IP       host JDK linux版本 hadop版本 192.168.0.1 master 1.8.0_111 centos7.2.1511 hadoop-2.7 ...

  3. 开始jQuery学习之旅

    jQuery写法 //实例写法$('div').css({width:200,backgroundColor:'red'}); 参数规则 // css selector$('.wrapper ul l ...

  4. js高级-递归调用

    函数调用自身 求1-100的和 var sum = 0; for(var i = 1; i<=100; i++){ sum += i } console.log(sum) //自己写的递归 va ...

  5. java 基础-思维导图

    思维导图的好处 最近看了一些文章的思维导图,发现思维导图真是个强大的工具.了解了思维导图的作用之后,觉得把它运用到java上应该是个不错的想法,这样回顾知识点的时候一目了然,快速知道自己的短板. 思维 ...

  6. day31 粘包问题

    TCP粘包问题 cmd客户端代码 import socket import struct import socket import json c = socket.socket() c.connect ...

  7. The hyperlink for cell A2 references relation rId1, but that didn't exist!

    excel单元格中存在超链接,去掉 可以参考 https://www.cnblogs.com/songyunxinQQ529616136/p/6599523.html

  8. input type='number'时,maxlength属性无效

    <input type="number" oninput="if(value.length>11)value=value.slice(0,11)"  ...

  9. poj2970 The lazy programmer 【优先队列】

    A new web-design studio, called SMART (Simply Masters of ART), employs two people. The first one is ...

  10. leetcode题库解答源码(python3)

    下面和大家分享本人在leetcode上已经ace的题目源码(python3): 本人会持续更新!- class Leetcode_Solution(object): def twoSum_1(self ...