gpt4 book ai didi

java - 在原子整数数组上使用线程

转载 作者:行者123 更新时间:2023-12-02 03:43:00 24 4
gpt4 key购买 nike

我正在编写代码让 4 个线程构建直方图。

我在 main 中有一个数组:

int N = 10000;
Random r = new Random();
int[] a = new int[N];

for (int i = 0; i < a.length; i++)
{
a[i] = Math.abs(r.nextInt() % 100);
}

所以基本上我想做的是循环遍历这个数组并计算每个数字出现的次数。

所以我编写了线程类,并使用了 AtomicInteger 我认为这将有助于解决多个线程尝试同时访问同一索引的问题。

import java.util.concurrent.atomic.AtomicInteger;

public class UseThread implements Runnable
{
private static int[] array;
private static AtomicInteger[] count;
private static boolean[] check;

public UseThread(int[] array, AtomicInteger[] count)
{
this.array = array;
this.count = count;
this.check = new boolean[array.length];
}

public void run()
{
for (int i = 0; i < array.length; i++)
{
if (!getIndex(this.check[i]))
{
this.check[i] = true;
int number = array[i];
count[number].incrementAndGet();
}
}
}

public synchronized static boolean getIndex(boolean check2)
{
return check2;
}

但是,这并没有完全解决我的问题。一些线程同时访问数组,使得 count 数组保存的值大于 array 数组的长度。

我认为问题出在检查的 boolean 数组上。我有一种感觉,多个线程同时访问同一个 boolean 数组索引。

我认为这可能是一个简单的修复,但我只是没有看到它。

有什么建议吗?

我尝试过AtomicBoolean数组,但没有帮助。下面是同一个类,但实现了 AtomicBoolean 数组。

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

public class Assign7Q3 implements Runnable
{
private static int[] array;
private static AtomicInteger[] count;
private static AtomicBoolean[] check;

public Assign7Q3(int[] array, AtomicInteger[] count)
{
this.array = array;
this.count = count;
this.check = new AtomicBoolean[array.length];
for(int i = 0; i < check.length; i ++)
check[i] = new AtomicBoolean(false);
}

public void run()
{
for (int i = 0; i < array.length; i++)
{
//System.out.println(this.check[i].get());
if (!getIndex(this.check[i]))
{
this.check[i].set(true);
int number = array[i];
count[number].incrementAndGet();
}
}
}

public synchronized static boolean getIndex(AtomicBoolean check2)
{
return check2.get();
}

最佳答案

您需要使用compareAndSet让您的 if 语句互斥:

if (this.check[i].compareAndSet(false, true))
{
int number = array[i];
count[number].incrementAndGet();
}

这会自动检查并设置值。

如果没有 compareAndSet,两个线程有​​可能在一个有机会调用 之前同时检查值并进入 if block 设置(真)

关于java - 在原子整数数组上使用线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36635187/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com