java线程间数据传输_数据统计之数据多线程数据传输

说实话这段代码对于数据库的提升要看读写比例,数据库插入的数据的方式以及引起的数据库锁表示行锁还是页锁还是表锁有关系,线程数据量依据服务器的不同设定也不一样。提升效率比较难以计算。但是毕竟也是一种尝试

就贴出来共享了

SqlHelper.CommandTimeout = 1800;

long minAutoID = GetSourceMaxOrMinID(curstatdatevalue, DownLogSourceDBConnString, true);

long maxAutoID = GetSourceMaxOrMinID(curstatdatevalue, DownLogSourceDBConnString, false);

//数据分片,确定每个分片的数据量

long everyCount = (maxAutoID - minAutoID) / PartsCount + 1;

List list = new List();

for (int i = 0; i 

{

list.Add(new AutoIdArea(minAutoID + i * everyCount, minAutoID + (i + 1) * everyCount - 1, "D_Copy_ThreadAutoId_" + curstatdatevalue / 100 + "_" + i));

}

Utility.WriteLog("开始 将明细数据转换成原始统计数据保存到ResDownloadLog_Temp表中", LogDir);

//设置信号灯

Semaphore semaphores = new Semaphore(ThreadCount, ThreadCount);

//定义方法

Action fun = (areaInfo, s, curstat) =>

{

try

{

long minId =

Convert.ToInt64(Utility.GetStatAutoID(areaInfo.ThreadKey, areaInfo.BeginInt,

ComputingDB_ConnString));

if (minId <= areaInfo.BeginInt)

minId = areaInfo.BeginInt;

//在别的线程没有出现异常的情况下继续跑

while (Import(areaInfo, ref minId, curstat, DownLogSourceDBConnString) > 0) ;

}

catch (Exception ex)

{

ThreadException = ex;

Utility.WriteLog(

string.Format("异步导入数据异常:beginId={0},endId={1},ex:{2}", areaInfo.BeginInt, areaInfo.EndInt,

ex.ToString()), LogDir);

Utility.SendMail("异步导入数据异常",

string.Format("beginId={0},endId={1},ex:{2}", areaInfo.BeginInt,

areaInfo.EndInt, ex.ToString()), false);

}

finally

{

s.Release();

}

};

List listResult = new List();

for (int i = 0; i 

{

semaphores.WaitOne();

if (ThreadException != null)

{

//等待所有异步线程完成

for (int j = 0; j 

{

listResult[j].AsyncWaitHandle.WaitOne();

}

throw ThreadException;

}

listResult.Add(fun.BeginInvoke(list[i], semaphores, curstatdatevalue, null, null));

}

//等待所有异步线程完成

for (int i = 0; i 

{

listResult[i].AsyncWaitHandle.WaitOne();

}

if (ThreadException != null)

{

throw ThreadException;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值