gpt4 book ai didi

java - 使用线程在数组中搜索元素

转载 作者:行者123 更新时间:2023-11-30 03:52:08 25 4
gpt4 key购买 nike

我有以下作业要做:实现对数组中指定元素的并行搜索。使用线程数作为函数参数。每个线程检查自己的数组 block 大小(ArraySize/NumberOfThreads)。

class MyThread extends Thread {

final int[] SEARCH_TAB;
final int RANGE_TAB[][];
final int SEARCH_VALUE;

static int searchIndex = -1;
static boolean isWorking = true;

int whichThread;

MyThread(int[] searchTab, int[][] rangeTab, int searchValue, int whichThread) {
SEARCH_TAB = searchTab;
RANGE_TAB = rangeTab;
SEARCH_VALUE = searchValue;
this.whichThread = whichThread;
}

@Override
public void run() {
for (int i = RANGE_TAB[whichThread][0]; i < RANGE_TAB[whichThread][1] && isWorking; ++i) {
synchronized(this) {
if (SEARCH_TAB[i] == SEARCH_VALUE) {
isWorking = false;
searchIndex = i;
}
}
}
}
}

class Main {

private static int[][] range(int n, int p) {
int[] quantities = new int[p];
int remainder = n % p;
int quotient = n/p;
int i;
for (i = 0; i < p; ++i) quantities[i] = quotient;
i = 0;
while (remainder != 0) {
--remainder;
++quantities[i];
++i;
}

int[][] tab = new int[p][2];
tab[0][0] = 0;
tab[0][1] = quantities[0];
for (i = 1; i < p; ++i) {
tab[i][0] = tab[i-1][1];
tab[i][1] = tab[i][0] + quantities[i];
}
return tab;
}

private static int search(int[] searchTab, int numberOfThreads, int searchValue) {
int[][] rangeTab = range(searchTab.length, numberOfThreads);
Thread[] threads = new Thread[numberOfThreads];
for ( int i = 0; i < numberOfThreads; ++i) threads[i] = new MyThread(searchTab, rangeTab, searchValue, i);
for ( int i = 0; i < numberOfThreads; ++i) threads[i].start();
return MyThread.searchIndex;
}

public static void main(String[] args) {
int[] tab = {0, 1, 2, 3, 4, 5, 6, 7 , 8, 9, 10};
int value = 5;
int valueIndex = search(tab, 1, value);
if (valueIndex == -1) System.out.println("Not found.");
else System.out.println(valueIndex);
}
}

这段代码通常可以工作,但在实现一个线程时找不到索引。顺便说一句,我的老师说我的代码太长太复杂,有什么建议吗?

我将不胜感激任何形式的帮助。

最佳答案

下面的代码怎么样:

public class Searcher implements Runnable {
private int intToFind;
private int startIndex;
private int endIndex;
private int[] arrayToSearchIn;

public Searcher(int x, int s, int e, int[] a) {
intToFind = x;
startIndex = s;
endIndex = e;
arrayToSearchIn = a;
}

public void run() {
for (int i = startIndex; i <= endIndex; i++) {
if (arrayToSearchIn[i] == intToFind) System.out.println("Found x at index: " + i);
}
}
}

public class Starter {
public static void main(String[] args) {
int[] a = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
int numberOfThreads = 5;
int x = 20;
findElement(numberOfThreads, x, a);
}

private static void findElement(int numberOfThreads, int x, int[] a) {
int sizeOfa = a.length;
int range = sizeOfa/numberOfThreads;
for (int i = 0; i <= numberOfThreads-1; i++) {
Thread searcher;
if (i == numberOfThreads-1) {
searcher = new Thread(new Searcher(x, i*range, sizeOfa-1, a));
} else {
searcher = new Thread(new Searcher(x, i*range, i*range+range-1, a));
}
searcher.start();
}
}
}

您仍然可以优化代码,例如通过将数组的其余部分拆分到所有线程上,而不是仅仅将其插入最后一个线程(就像在我的代码中一样),但想法仍然是相同的。

编辑:我认为您的代码有问题。它只会显示数组中 x 的一次出现。如果您使用五个线程在 [5,5,5,5,5] 中查找 x = 5,您永远无法知道将返回哪个索引,因为这取决于线程的调度方式。结果将在 0 到 5 之间。

关于java - 使用线程在数组中搜索元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24178900/

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