请问 MAPREDUCE中多执行绪问题

来源:互联网  时间:2016/6/24 12:32:01

关于网友提出的“ 请问 MAPREDUCE中多执行绪问题”问题疑问,本网通过在网上对“ 请问 MAPREDUCE中多执行绪问题”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:

问题: 请问 MAPREDUCE中多执行绪问题
描述:

您好:
一般若将 long counter = 0; 设在CLASS中,这应该是共用的,会给後续其他方法 更新值
那於以下 MAPREDUCE 程式码中,long counter = 0;
他不会被其他map运作时,重复更新吗?
public void map(){
for(int i=0;i<100;i++){ //1000000000
counter++;
}
synchronized (hdfs){
hdfs.put(Thread.currentThread().getId(), counter);//key,value
}
}
每一个 map()动作,都只自己从1加到100 吗?
程式码如下:


package org.windoop;
import java.util.*;
import java.util.Map.Entry;
public class MultiThreadDemo04 implements Runnable {
long counter = 0;
long total = 0;
static HashMap hdfs = new HashMap();
public static void main(String[] args) throws InterruptedException{
long startTime = System.currentTimeMillis();
ThreadGroup g1 = new ThreadGroup("g1");
for(int i = 0; i < 100; i++){
new Thread(g1, new MultiThreadDemo04()).start();
}
while(g1.activeCount() > 0){
Thread.sleep(100);
}
MultiThreadDemo04 reducer = new MultiThreadDemo04();
for(Entry entry : hdfs.entrySet())
{
reducer.reduce(entry.getKey(), entry.getValue());
}
System.out.println("Total: " + reducer.total);
System.out.println("Time Spent: " + (System.currentTimeMillis()-startTime));
}
public void run(){
map();
}
public void map(){
for(int i=0;i<100;i++){ //1000000000
counter++;
}
synchronized (hdfs){
hdfs.put(Thread.currentThread().getId(), counter);//key,value
}
}
public void reduce(long key, long value){
total += value;
}
}

谢谢!
解决方案1:

繁体字看着好吃力 ?
不是太明白你这个例子是想说什么问题?
模拟mapreduce过程?

上一篇Operation category READ is not supported in state standby启动jobtracker,报以下错误,
下一篇Sqoop1995从mysql导入到hdfs出错!Job Failed with status:3
明星图片
相关文章
《 请问 MAPREDUCE中多执行绪问题》由码蚁之家搜集整理于网络,
联系邮箱:mxgf168#qq.com(#改为@)