java 数组算法技巧,Java:一种快速对数组进行多次计算的方法

Sorry if this isn't the right place for these questions, but I'm in need of some base help.

I have a class called Differential and that has a member variable mValues (List>). What I want to do is iterate through all the values and compare them with each other. This means for five values I am doing 10 comparisons. However this is going to be used for at least 20,000 lists. There are three calculations that I want to do and I was wondering how I should approach this.

I was thinking that I would use a completionService to iterate through the values in multi-threading. Then each thread would deal with the multiple calculations and return them. Does this seem appropriate? Is there a way of doing this somewhat quickly that I'm not realizing?

解决方案

Before you make the multi-thread version, I would suggest making the single thread version work and show us the code. Then you can split the work of a for loop (sequence) easily. Lots of details omitted below.

mysequence = ...//array or list

//2 threads

c1=new Callable(){

Object call(){

for(i=0...mysequence.size()/2)

//do work

}

return result;

}

c2=new Callable(){

Object call(){

for(i=mysequence.size()/2+1 ... mysequence.size()-1)

//do work

}

return result;

}

ExecutorService exec=Executors.xxxThreadPool(2);

fut1= exec.submit(c1)

fut2 =exec.submit(c2)

fut1.get()

fut2.get()

Let me find a link to another post of mine. See my answer here for a better example. Thread won't naturally exit at end of run()

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值