java - 使用 for 循环 Java 理解多线程

标签 java multithreading for-loop

我有一些我认为不能多线程的代码,也许我错了。我想在集群系统上执行此代码,但我不确定如何针对此类部署扩展它。

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

    public class Coord {
        public int a,b,c,d,e,f;


    public static void main(String[] args) throws IOException {
        FileOutputStream out = new FileOutputStream("/Users/evanlivingston/2b.txt");
        PrintStream pout = new PrintStream(out);
        Scanner sc = new Scanner(new File("/Users/evanlivingston/1.txt"));
        List<Coord> coords = new ArrayList<Coord>();{


            // for each line in the file
            while(sc.hasNextLine()) {
                String[] numstrs = sc.nextLine().split("\\s+"); 

                Coord c = new Coord();


                c.a = Integer.parseInt(numstrs[1]);
                c.b = Integer.parseInt(numstrs[2]);
                c.c = Integer.parseInt(numstrs[3]);
                c.d = Integer.parseInt(numstrs[4]);
                c.e = Integer.parseInt(numstrs[5]);
                c.f = Integer.parseInt(numstrs[6]);

                coords.add(c);

            }
// now you have all coords in memory
            {
for(int i=0; i<coords.size(); i++ ) 
    for( int j=0; j<coords.size(); j++) 
    {
        Coord c1 = coords.get(i);
        Coord c2 = coords.get(j);
        double foo = ((c1.a - c2.a) * (c1.a - c2.a)) *1 ;
        double goo = ((c1.b - c2.b) * (c1.b - c2.b)) *1 ;
        double hoo = ((c1.c - c2.c) * (c1.c - c2.c)) *2 ;
        double joo = ((c1.d - c2.d) * (c1.d - c2.d)) *2 ;
        double koo = ((c1.e - c2.e) * (c1.e - c2.e)) *4 ;
        double loo = ((c1.f - c2.f) * (c1.f - c2.f)) *4 ;
        double zoo = Math.sqrt(foo + goo + hoo + joo + koo + loo);

        DecimalFormat df = new DecimalFormat("#.###");
        pout.println(i + " " + j + " " + df.format(zoo));
        System.out.println(i);

    }
    pout.flush();
    pout.close();
            }
        }
    }   
}

我感谢任何人可以提供的帮助。

最佳答案

将内部 for 循环拆分为单独的任务看起来是使该进程成为多线程的理想选择。这是可以使用 ExecutorService 和 Futures 完成的一种方法

    final ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
    final List<Future<String>> results = new LinkedList<Future<String>>();
    // now you have all coords in memory
    for (int i = 0; i < coords.size(); i++) {
        final int index = i;
        final Coord c1 = coords.get(index);
        results.add(executor.submit(new Callable<String>() {
            public String call() {
                final StringBuilder stringBuilder = new StringBuilder();
                for (int j = 0; j < coords.size(); j++) {
                    final Coord c2 = coords.get(j);
                    final double foo = ((c1.a - c2.a) * (c1.a - c2.a)) * 1;
                    final double goo = ((c1.b - c2.b) * (c1.b - c2.b)) * 1;
                    final double hoo = ((c1.c - c2.c) * (c1.c - c2.c)) * 2;
                    final double joo = ((c1.d - c2.d) * (c1.d - c2.d)) * 2;
                    final double koo = ((c1.e - c2.e) * (c1.e - c2.e)) * 4;
                    final double loo = ((c1.f - c2.f) * (c1.f - c2.f)) * 4;
                    final double zoo = Math.sqrt(foo + goo + hoo + joo + koo + loo);

                    final DecimalFormat df = new DecimalFormat("#.###");
                    stringBuilder.append(index + " " + j + " " + df.format(zoo));
                    System.out.println(index);
                }
                return stringBuilder.toString();
            }
        }));
    }
    for (Future<String> result : results) {
        pout.print(result.get());
    }
    pout.flush();
    pout.close();
    executor.shutdown();

对于集群,我认为 Hazelcast 提供了一个很好的解决方案,允许您定义共享的 ExecutorService 和共享的集合。您将需要两种类型的节点,负责所有 I/O 和创建坐标列表以及提交任务的单个节点。以及一个简单地执行任务的处理节点。这就是我对如何做的全部看法。但是,如果您的数据集小到足以放入内存,那么将处理拆分这么多可能不值得。

关于java - 使用 for 循环 Java 理解多线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5723936/

相关文章:

java - Java和Matlab的图像处理结果不同

java - 与 mySQL 一起使用的自动 java 类

multithreading - 不需要时强制使用 Mutex

java - 对于每个循环不返回值?

c - 这些使我的程序停止运行的 for 循环出了什么问题?

Delphi for 循环和 StringList 错误

java - 结合 charAt 和 IgnoreCase?

java - 未能在java上制作我自己的绘图小部件

java - 当两个锁定的线程(通过变量)切换其中一个变量并尝试访问另一个变量时,会发生什么?

java - JTree 给出 ArrayIndexOutOfBoundsException?