在这个java方法中需要使用并发
因此,我有以下代码,它接受两个数组的输入,并应用一些查询将在这个java方法中需要使用并发,java,multithreading,optimization,concurrency,Java,Multithreading,Optimization,Concurrency,因此,我有以下代码,它接受两个数组的输入,并应用一些查询将Array1中的元素与Array2中的元素进行匹配,然后返回两个arrayList中相似的元素数 以下是我使用的代码: public static void get_ND_Matches() throws IOException{ @SuppressWarnings("rawtypes") List<String> array1 = new ArrayList<String&g
Array1
中的元素与Array2
中的元素进行匹配,然后返回两个arrayList中相似的元素数
以下是我使用的代码:
public static void get_ND_Matches() throws IOException{
@SuppressWarnings("rawtypes")
List<String> array1 = new ArrayList<String>();
List<String> array2 = new ArrayList<String>();
array1 = new ArrayList<String>( ClassesRetrieval.getDBpediaClasses());
array2 = new ArrayList<String>( ClassesRetrieval.fileToArrayListYago());
String maxLabel="";
HashMap<String,Integer> map = new HashMap<String,Integer>();
int number;
HashMap<String,ArrayList<String>> theMap = new HashMap<>();
for(String yagoClass:array2){
theMap.put(yagoClass, getListTwo(yagoClass));
System.out.println("Done for : "+yagoClass );
}
for(String dbClass:array1){
ArrayList<String> result = get_2D_Matches(dbClass);
for(Map.Entry<String, ArrayList<String>> entry : theMap.entrySet()){
String yagoClass=entry.getKey();
Set<String> IntersectionSet =Sets.intersection(Sets.newHashSet(result), Sets.newHashSet(entry.getValue()));
System.out.println(dbClass + " and "+ yagoClass+ " = "+ IntersectionSet.size());
number = IntersectionSet.size();
map.put(yagoClass, number);
}
int maxValue=(Collections.max(map.values()));
for(Entry<String, Integer> entry:map.entrySet()){
if(entry.getValue()==maxValue && maxValue != 0){
maxLabel = entry.getKey();
}
if(maxValue==0){
maxLabel = "Nothing in yago";
}
}
System.out.println("-------------------------------");
System.out.println(dbClass+" from DBPEDIA Corresponds to "+ maxLabel);
System.out.println("-------------------------------");
}
}
public static void get\u ND\u Matches()引发IOException{
@抑制警告(“原始类型”)
List array1=新的ArrayList();
List array2=新的ArrayList();
array1=新的ArrayList(ClassesRetrieval.getDBpediaClasses());
array2=新的ArrayList(ClassesRetrieval.FileToArrayListAgo());
字符串maxLabel=“”;
HashMap=newHashMap();
整数;
HashMap theMap=新的HashMap();
用于(字符串yagoClass:array2){
map.put(yagoClass,getListTwo(yagoClass));
System.out.println(“为:+yagoClass完成”);
}
for(字符串dbClass:array1){
ArrayList结果=获取2D匹配(dbClass);
对于(Map.Entry:theMap.entrySet()){
字符串yagoClass=entry.getKey();
Set IntersectionSet=Sets.intersection(Sets.newHashSet(result),Sets.newHashSet(entry.getValue());
System.out.println(dbClass+”和“+yagoClass+”=”+IntersectionSet.size());
数字=相交集大小();
地图放置(yagoClass,编号);
}
int maxValue=(Collections.max(map.values());
for(条目:map.entrySet()){
if(entry.getValue()==maxValue&&maxValue!=0){
maxLabel=entry.getKey();
}
如果(maxValue==0){
maxLabel=“雅高中无任何内容”;
}
}
System.out.println(“------------------------------------”;
System.out.println(DBPEDIA中的dbClass+”对应于“+maxLabel);
System.out.println(“------------------------------------”;
}
}
此代码返回例如:
DBPEDIA中的Actor对应于Yago_Actor
来自DBPEDIA的相册对应于Yago_相册
DBPEDIA中的SomeClass在Yago中不对应任何内容
等等
在幕后,此代码使用getDBpediaClasses
,然后应用Get_2D_Matches()方法获取每个类的结果的arrayList。然后将得到的每个ArrayList与由getListTwo()
为FileToArrayListAgo()的每个类生成的另一个ArrayList进行比较
现在,由于所有的计算都是在后台进行的(每个数组中有数百万个元素),这个过程需要几个小时才能执行
我很想使用并发/多线程来解决这个问题。有人能告诉我怎么做吗?将不完全干净和优化的代码并行化没有什么意义。在典型的4核CPU上,您可能会得到因子4,或者根本没有,这取决于您是否正确选择要并行化的部件。使用更好的算法可能会给你带来更多
瓶颈可能是您尚未发布的get\u 2D\u Matches
直接计算最大值而不是创建一次性的HashMap映射
可以节省大量时间,将set.newHashSet(result)
移出循环也可以节省大量时间
您应该重新考虑变量命名。对于像map
、theMap
和result
这样的名称(对于不是该方法的结果的内容),很难找出发生了什么
如果您真的想将其并行化,则需要首先拆分过长的方法。这样就相当简单了,因为每个dbClass
的处理都可以独立完成。只需将其封装为一个可调用的
,并将其提交给执行服务
但是,我建议先清理代码,然后提交给它,然后考虑并行化。< / P> Oracle可以告诉你:)java 8和lambdas可以帮助你。