原帖地址在这里:http://www.iteye.com/topic/711162
引用
前几天在网上看到一个淘宝的面试题:有一个很大的整数list,需要求这个list中所有整数的和,写一个可以充分利用多核CPU的代码,来计算结果。
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
public class BigListSum {
/**
* @param args
*/
private static List<Integer> list=new LinkedList<Integer>();
private static int max=100000;
public static List<Integer> setList(int max){
for(int i=0;i<max;i++)
list.add(i+1);
//System.out.println(list.size());
return list;
}
public static void main(String[] args)throws Exception {
BigListSum bls=new BigListSum();
BigListSum.setList(max);
int threadCount=10;
int len=list.size()/threadCount+1;
AtomicCounter counter=bls.new AtomicCounter();
for(int i=0;i<threadCount;i++){
if(len*i<list.size())
bls.new subThread("thread"+i,list,len*i,len,counter).start();
}
}
class subThread extends Thread{
private AtomicCounter counter;
private List<Integer> childList;
private int offset;
private int len;
protected subThread(String name,List<Integer> list,int offset,int len,AtomicCounter counter){
super(name);
this.counter=counter;
this.offset=offset;
if((offset+len)>list.size()){
this.len=list.size()-offset;
}else{
this.len=len;
}
childList=list.subList(this.offset, this.offset+this.len);
}
public void run(){
long sum=0;
if(childList.size()>0){
for(int i=0;i<childList.size();i++){
sum+=childList.get((int)i);
}
counter.increment(sum);
}
System.out.println(Thread.currentThread().getName()+":["+
this.childList.get(0)+"<-->"+this.childList.get(this.childList.size()-1)+
"].count:"+counter.getValue());
}
}
class AtomicCounter{
private AtomicLong sum=new AtomicLong();
public AtomicCounter(){
}
public AtomicCounter(long value){
sum.set(value);
}
public long getValue(){
return sum.get();
}
public long increment() {
return sum.incrementAndGet();
}
public long increment(long i) {
return sum.addAndGet(i);
}
public long decrement() {
return sum.decrementAndGet();
}
public long decrement(long i) {
return sum.addAndGet(-i);
}
}
}
output:
thread9:[90010<-->100000].count:949194955
thread0:[1<-->10001].count:999209956
thread8:[80009<-->90009].count:1849384965
thread1:[10002<-->20002].count:1999419967
thread7:[70008<-->80008].count:2749574975
thread2:[20003<-->30003].count:2999629978
thread6:[60007<-->70007].count:3649764985
thread3:[30004<-->40004].count:3999839989
thread5:[50006<-->60006].count:4549954995
thread4:[40005<-->50005].count:5000050000
利用的是原子操作,无同步
分享到:
相关推荐
Anecdoteofthejar坛子的轶事PPT学习教案.pptx
三峡大坝坛子岭导游词.docx
对电脑多的混合性皮肤MM超全国货体验给坛子里的M.doc
腌菜的坛子
泡菜坛子的基本底料
Anecdoteofthejar坛子的轶事学习教案.pptx
坛子、酒瓶与苹果作文.doc
这是英文原版。国内现在已经引进:“Aliprantis;...坛子里已经有了,需要者自己搜索下。 标题:实分析习题及解答 作者:B. Gelbaum;常心怡、李元中、鲁石、冯汉桥等编译 版本:中文版 页数:335,正文331页
问了坛子里的一些朋友,好像也没有这种简单的现成的工具,于是一咬牙一跺脚,自己写吧。顺便把多个图片合并也写进去了,目前只是测试了自己需要的部分(仅BMP格式),和源码一起放出来,希望对你有用,不够用自己去...
设一个罐子中装有b个黑球和r个红球,从中随机地抽取一个球,然后放回并同时加进c个与取出球颜色相同的球和d个与取出球颜色相反的球,其中c、d为任意给定的整数,如此反复进行下去。当c>0,d=0时称为Polya罐子模型;...
行业资料-建筑装置-带平台的酸菜坛子.zip
中班下学期语言教案《狐狸和坛子》润新教育.txt
我有一个新的社会网络应用的构想。我并不期望这个应用可以获得巨大成功,但我想还是有些潜力的。我找过一些好友和同事交流过这个想法,他们都十分喜欢这个构想。还有些朋友甚至提出想作为合作伙伴加入一起进行开发,...
3.改完文件以后,打开本软件,开始编译签名,先选中反编译后产生的目录,也就是刚才你改文件的那个目录,然后再选一个生成APK存放的目录,点开始编译,一会就会生成一个.apk的文件,然后就自己装进手机里玩把 如果你要制作...
这是整理发布的一款泡菜坛子的基本底料,泡菜坛子的基本底料能给你需要了解的知识与资料,欢迎...该文档为泡菜坛子的基本底料,是一份很不错的参考资料,具有较高参考价值,感兴趣的可以下载看看
此电路是极为常见的一个线性三端稳压器扩流电路,我们在实际使用的时候,遇到一些由于没有考虑周全或者说是低级错误的故障,故而开贴让坛子里面的朋友讨论,让以后用到此电路的朋友不至于重蹈覆辙.
主要讲述fpga器件多种配置模式,并行模式及主从模式,串行模式及外设模式
微机原理汇编语言及接口技术 部分习题答案,坛子里下,希望对其他人有用!
看到坛子里有很多人在问51转到arm该如何如何。我说说自己的经验吧。
常常看到坛子里的朋友们专研和讨论SAP某一个功能的配置和用法,精神可嘉。某项具体功能地实现,的确是顾问的基本功,但如果要从SAP业务顾问提升到另一个层次,无论是方案架构师、项目经理,还是管理咨询、业务流程...