Java 在没有安全性的情况下并发访问阵列

Java 在没有安全性的情况下并发访问阵列,java,concurrency,Java,Concurrency,字段值是float[],我有点困惑,因为当我调试代码时,我运行的问题是,当一个线程正在工作并使用我的float[]值数组时,其他线程正在等待,这是正常的吗?我不关心数组上的错误数据,因为线程正在计算数组的不同部分 ExecutorService executor = Executors.newFixedThreadPool(threads); List<PartOfImage> callables; for(int j=0;j<200;++j)

字段值是float[],我有点困惑,因为当我调试代码时,我运行的问题是,当一个线程正在工作并使用我的float[]值数组时,其他线程正在等待,这是正常的吗?我不关心数组上的错误数据,因为线程正在计算数组的不同部分

ExecutorService executor = Executors.newFixedThreadPool(threads);

List<PartOfImage> callables;
        for(int j=0;j<200;++j)
        for(int i=0;i<threads;++i){
            callables=new LinkedList<>();
            callables.add(new PartOfImage(values,width,(height/threads)*i, (height/threads)*(i+1),
                    oldImage,((i+1)%threads)==0,(i%threads)==0));

        List<Future<Object>> answers =   executor.invokeAll(callables);


         oldImage=getValues();
        }
ExecutorService executor=Executors.newFixedThreadPool(线程);
列出可调用项;

对于(int j=0;j假设您的
线程数为10。您可能认为您的代码在
callables
列表中生成10个callables,然后运行
invokeAll
。但实际上,您的代码为每个
i
执行整个块:

callables=new LinkedList<>();
callables.add(new PartOfImage(values,width,(height/threads)*i, (height/threads)*(i+1),
              oldImage,((i+1)%threads)==0,(i%threads)==0));

List<Future<Object>> answers =   executor.invokeAll(callables);
oldImage=getValues();
callables=newlinkedlist();
添加(图像的新部分(值、宽度、(高度/线程)*i、(高度/线程)*(i+1),
oldImage,((i+1)%threads)==0,(i%threads)==0));
列表答案=executor.invokeAll(可调用项);
oldImage=getValues();
这意味着它创建一个具有一个可调用的列表,并在该列表上运行
invokeAll
,然后转到下一个
i
,创建一个具有一个可调用的列表,在该(单个任务)列表上运行
invokeAll
,依此类推


因此,实际上它是连续运行的。您应该将列表的创建移到
i
循环之外,只有
可调用项。add
应该在循环之内,而
invokeAll
getValues
应该在循环之外。

您还没有显示执行器的定义。如果它是单线程执行器,它将l连续运行任务,而不是并行运行。好的,我添加了它。
getValues();
的定义是什么?您必须使用
Future.get()
我假设,在这种情况下,调用被阻塞。每个逻辑内核一次只能运行一个线程,其他所有线程都必须等待。我建议使用并行流来更有效地调度此任务。我不明白一件事,我的处理器有4个内核和8个线程。那么我可以使用executor或4运行8个线程吗?因为你说的是cores。。。