MD5与SHA算法一样,利用他们可以计算某段数据的唯一hash值,常用做校验码。而MD5比SHA算法性能高。在我参加的一个项目中,主要用MD5码值来去重,因此对计算性能要求较高。网上有对MD5算法并行化方法,能保证计算结果与线性计算法一样。由于我只需要唯一标记,并不要求它与线性MD5计算结果一样,所以,我通过分片多线程计算的方法实现提速,也充分利用了多核CPU的并行计算优势。

依据:

B  = (b1,b2,b3,b4...bn)--->(m1,m2,m3...mn)----+--->M---->(md5) result = 16 bytes

C#代码:

class SafeQueue<T>
{
// A queue that is protected by Monitor.
private Queue<T> m_inputQueue = new Queue<T>(); // Lock the queue and add an element.
public void Enqueue(T qValue)
{
// Request the lock, and block until it is obtained.
Monitor.Enter(m_inputQueue);
try
{
// When the lock is obtained, add an element.
m_inputQueue.Enqueue(qValue);
}
finally
{
// Ensure that the lock is released.
Monitor.Exit(m_inputQueue);
}
} // Try to add an element to the queue: Add the element to the queue
// only if the lock is immediately available.
public bool TryEnqueue(T qValue)
{
// Request the lock.
if (Monitor.TryEnter(m_inputQueue))
{
try
{
m_inputQueue.Enqueue(qValue);
}
finally
{
// Ensure that the lock is released.
Monitor.Exit(m_inputQueue);
}
return true;
}
else
{
return false;
}
} // Try to add an element to the queue: Add the element to the queue
// only if the lock becomes available during the specified time
// interval.
public bool TryEnqueue(T qValue, int waitTime)
{
// Request the lock.
if (Monitor.TryEnter(m_inputQueue, waitTime))
{
try
{
m_inputQueue.Enqueue(qValue);
}
finally
{
// Ensure that the lock is released.
Monitor.Exit(m_inputQueue);
}
return true;
}
else
{
return false;
}
} // Lock the queue and dequeue an element.
public T Dequeue()
{
T retval; // Request the lock, and block until it is obtained.
Monitor.Enter(m_inputQueue);
try
{
// When the lock is obtained, dequeue an element.
retval = m_inputQueue.Dequeue();
}
finally
{
// Ensure that the lock is released.
Monitor.Exit(m_inputQueue);
} return retval;
} // Delete all elements that equal the given object.
public int Remove(T qValue)
{
int removedCt = ; // Wait until the lock is available and lock the queue.
Monitor.Enter(m_inputQueue);
try
{
int counter = m_inputQueue.Count;
while (counter > )
// Check each element.
{
T elem = m_inputQueue.Dequeue();
if (!elem.Equals(qValue))
{
m_inputQueue.Enqueue(elem);
}
else
{
// Keep a count of items removed.
removedCt += ;
}
counter = counter - ;
}
}
finally
{
// Ensure that the lock is released.
Monitor.Exit(m_inputQueue);
} return removedCt;
} // Print all queue elements.
public string PrintAllElements()
{
StringBuilder output = new StringBuilder(); // Lock the queue.
Monitor.Enter(m_inputQueue);
try
{
foreach( T elem in m_inputQueue )
{
// Print the next element.
output.AppendLine(elem.ToString());
}
}
finally
{
// Ensure that the lock is released.
Monitor.Exit(m_inputQueue);
} return output.ToString();
}
} //任务节点
public class TaskItem
{
public int idx_;
public byte[] data_;
public TaskItem( int idx, byte[] data )
{
this.idx_ = idx;
this.data_ = data;
}
}
//工作线程
public class WorkThread
{
SafeQueue<TaskItem> packet_;
AutoResetEvent notifyEvt_;
ManualResetEvent finishEvt_;
Thread thread_;
HashTable result_; public WorkThread()
{
....
thread_ = new Thread( new ThreadStart( this.DoWork ) );
}
public void Start()
{
thread_.Start();
}
//主线程调用:异步添加任务节点
public void AddDataPacket( TaskItem data )
{
packet_.Enqueue( data );
notifyEvt_.Set(); }
//主线程调用:等待收集计算结果
public Hashtable Finish()
{
Hashtable rs = new Hashtable( result_ );
finishEvt_.WaitOne();
finishEvt_.Reset();
result_.clear();
return rs;
}
//线程执行函数
public void DoWork()
{
TaskItem var;
while(true)
{
notifyEvt_.WaitOne();
while(true)
{
if( packet_.Count > )
{
//这里取出值错误
var = packet_.Dequeue();
}
if( var == null )
break;
//task finish
if( var.Idx == - )
{
....
this.finishEvt_.Set();
}
}
}
}
} public class Md5Hasher
{
public void main()
{
WorkThread[] test=new WorkThread[]; for( int i=; i<; ++i )
{
//这里输入数据
test[i%].AddDataPacket( ... );
}
}
}

并发MD5计算方法的更多相关文章

  1. 阿里P8面试官:如何设计一个扛住千万级并发的架构?

    大家先思考一个问题,这也是在面试过程中经常遇到的问题. 如果你们公司现在的产品能够支持10W用户访问,你们老板突然和你说,融到钱了,会大量投放广告,预计在1个月后用户量会达到1000W,如果这个任务交 ...

  2. 演示get、post请求如何算sn,算得sn如何使用

    import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.UnsupportedEncoding ...

  3. python查找并删除相同文件-UNIQ File-wxPython-v6

    相比第一版,新增:菜单,对话框,文件过滤器,操作结果保存,配置功能(自己写了一个读写配置文件的功能),提示语优化,模块分化更合理. 截图: 源代码: UniqFile-wxPython-v6.py: ...

  4. python查找并删除相同文件-UNIQ File-script版本

    今天用wxPython做了一个GUI程序,实现查找指定目录内的相同文件,主要原理是计算文件的md5值(计算前先找出文件大小相同的文件,然后计算这些文件的md5值,而不是所有文件都计算,大大减少了md5 ...

  5. python查找并删除相同文件-UNIQ File-wxPython版本

    今天用wxPython做了一个GUI程序,我称之为UNIQ File,实现查找指定目录内的相同文件,主要原理是计算文件的md5值(计算前先找出文件大小相同的文件,然后计算这些文件的md5值,而不是所有 ...

  6. 百度GPSutil

    ================================================= package com.qcar.benz.biz.common; import com.aliba ...

  7. HTTP服务简介

    第1章 HTTP服务介绍 1.1 简述用户访网站流程 a 进行域名信息的DNS解析   dig +trace 获得www.oldboyedu.com  ip地址信息 b 进行与网站服务器建立连接,tc ...

  8. 基于vue-simple-uploader封装文件分片上传、秒传及断点续传的全局上传插件

    目录 1. 前言 2. 关于vue-simple-uploader 3. 基于vue-simple-uploader封装全局上传组件 4. 文件上传流程概览 5. 文件分片 6. MD5的计算过程 7 ...

  9. vue-simple-uploader上传插件

    基于vue-simple-uploader封装文件分片上传.秒传及断点续传的全局上传插件 https://www.cnblogs.com/xiahj/p/vue-simple-uploader.htm ...

随机推荐

  1. JSP学习笔记(一)

    注释: 1.单行注释<!-- -->或者// <%@ page language="java" import="java.util.*" co ...

  2. 开源软交换系统 FreeSwitch 与 Asterisk 比较

    Asterisk 与freeswitch都是流行的开源软交换服务器,Asterisk出现的比较早,大概在1999年开始此项目,应该是最流行的开源软交换服务器,整个社区上下游都已经很成熟. freesw ...

  3. Hibernate之管理session与批处理

    1. Hibernate 自身提供了三种管理Session对象的方法 –Session对象的生命周期与本地线程绑定 –Session 对象的生命周期与JTA事务绑定 –Hibernate 委托程序管理 ...

  4. 启动程序的同时传参给接收程序(XE8+WIN764)

    相关资料: http://blog.csdn.net/yanjiaye520/article/details/7590252 注意事项: 1.ParamStr(0)是实例自己. 2.传的参数是以空格分 ...

  5. HDU 5792 World is Exploding (树状数组)

    World is Exploding 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5792 Description Given a sequence ...

  6. POJ 2763 Housewife Wind (树链剖分 有修改单边权)

    题目链接:http://poj.org/problem?id=2763 n个节点的树上知道了每条边权,然后有两种操作:0操作是输出 当前节点到 x节点的最短距离,并移动到 x 节点位置:1操作是第i条 ...

  7. IIS6的SSL配置,如何配置SSL到登陆页,如何将SSL证书设置成受信任的证书

    一. 申请证书1. 到受信任的机构申请 略 2. 到自建的证书服务器申请 a. 安装证书服务 通过控制面板中的“添加/删除程序”,选择“添加/删除Windows组件”.在Windows组件向导中找到“ ...

  8. 11道php面试题

    贡献11道php面试题及解决方法,跟大家总结一下曾经遇到的部分面试题.希望可以给大家得到帮助. 1. 什么事面向对象?主要特征是什么? 面象对象是把自然界的物体和概念直接映射到程序界的一种比较优雅的手 ...

  9. struts2属性Struts2中属性接收参数中文问题和简单数据验证

    PS:今天上午,非常郁闷,有很多简单基础的问题搞得我有些迷茫,哎,代码几天不写就忘.目前又不当COO,还是得用心记代码哦! 一:如果表单提交数据中有中文时,尽量应用post方式. 需要在Struts. ...

  10. JQuery UI Widget Factory官方Demo

    <!doctype html> <html lang="en"> <head> <meta charset="utf-8&quo ...