如何在Java中使用Multithreading与ArrayList [英] how to use Multithreading With ArrayList in Java

查看:86
本文介绍了如何在Java中使用Multithreading与ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述









你好,



<我有一个完美的程序,不幸的是我有一些需要大量时间的计算,几分钟......



我的目标是使用多线程来加速花费这么多时间的零件。



在这个例子中,我给出了应该并行化的零件的原型

  public static ArrayList< Object2> createListOfObject2(ArrayList< Object1> mylist){
ArrayList< Object2> listToReturn = new ArrayList<>();
Object2 object2;
for(int i = 0; i< mylist.size(); i ++){
for(int j = 0; j< mylist.size(); j ++){
object2 = heavyCalcul(mylist,i,j);
listToReturn.add(object2);
}
}
return listToReturn;
}
private static Object2 heavyCalcul(ArrayList< Object1> mylist,int i,int j){
int weight = MyCalculator.getInstance()。calcul(mylist.get(i),mylist获得(j)的);
Object2 Object2 = new Object2(weight);
返回Object2;
}

正如您所见,方法

  public static ArrayList< Object2> createListOfObject2(ArrayList< Object1> mylist)

获取Object1的列表,并应创建另一个object2列表。



我做了一个双向循环,每次我创建一个由两个对象组成的对象2时,它应该花费O(n²)次。



更大的列表需要很长时间。



那么我应该在哪里放置我应该使用的多线程和类型的列表。



第二个问题是类MyCalculator是一个单例类,我只创建它的一个对象,在我看来,即使使用多线程,真正的程序也不会受益multhitreading。



使用多线程时我应该遵循哪些规则?



非常感谢。

解决方案

你的对象是单身的事实是无关紧要的。重要的是共享可变状态。因此,如果您的计算不改变共享状态,并且每个计算因此独立于其他计算,则可以使用并行流:

  myList.parallelStream()
.flatMap(first - > myList.stream()。map(second - > MyCalculator.getInstance()。calcul(first,second)))
.collect(Collectors.toList());


Hello ,

I have a program that works perfectly , unfortunantly i have some calculs that takes a lot of time , some minutes ..

My objectif is to use multithreading to accelerate the parts that take so much time ,.

In this Example I give the prototype of the part that i should parallelize

public static ArrayList<Object2> createListOfObject2(ArrayList<Object1> mylist) {
    ArrayList<Object2> listToReturn = new ArrayList<>();
    Object2 object2;
    for (int i = 0; i < mylist.size(); i++) {
        for (int j = 0; j < mylist.size(); j++) {
            object2 = heavyCalcul(mylist, i, j);
            listToReturn.add(object2);
        }
    }
    return listToReturn;
}
private static Object2 heavyCalcul(ArrayList<Object1> mylist, int i, int j) {
    int weight = MyCalculator.getInstance().calcul(mylist.get(i),mylist.get(j));
    Object2 Object2 = new Object2(weight);
    return Object2;
}

As you ca see , the method

public static ArrayList<Object2> createListOfObject2(ArrayList<Object1> mylist)

get a list of Object1 , and should create another list of object2 .

I made a twoo foor Loop , and each time i create an object2 forming by two object , it should take O(n²) times.

for bigger list it takes a long time.

So where should I put the multithreding and wich type of list i should use.

The second Problem is that the class MyCalculator is a singleton Class , and i create only one object of it , and in my opinion even using multhitreading the real program will not benefit of multhitreading .

What are the rules i should follow to use multhitreading ?

Many Thanks.

解决方案

The fact that your object is a singleton is irrelevant. What matters is shared mutable state. So, if your computation don't mutate shared state, and every computation is thus independant from the others, you can just use a parallel stream:

myList.parallelStream()
      .flatMap(first -> myList.stream().map(second -> MyCalculator.getInstance().calcul(first, second)))
      .collect(Collectors.toList());

这篇关于如何在Java中使用Multithreading与ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆