我发现了类似的问题但不是我想要的问题。
我需要将矩阵1024x1024分成相等的4个部分,但我必须花费每个部分的开头和结尾来处理另一个带线程的类。我想过使用两个循环,但我不知道如何正确更改变量的值,因为它超过了最大值。
代码:
private static final int MAX_MATRIX = 1024;
private static final int NUM_THREADS = 4;
int start_row = 0, end_row = 0, start_column = 0, end_column = 0;
for (int i = 0; i < NUM_THREADS/2; i++){
end_row = start_row + (MAX_MATRIX/2);
for (int j = 0; j < (NUM_THREADS/2); j++){
end_column = start_column + (MAX_MATRIX/2);
myTask[i] = new TaskAnalyze(start_row, end_row, start_column, end_column, int_radom);
myThreads[i] = new Thread(myTask[i],"TASK " + i);
}
start_column += MAX_MATRIX/2;
start_row += MAX_MATRIX/2;
}
int_random是矩阵。
应该是:
myTask [0] = new TaskAnalyze(0,512,0,512,int_radom);
myTask [1] = new TaskAnalyze(0,512,512,1024,int_radom);
myTask [2] = new TaskAnalyze(512,1024,0,512,int_radom);
myTask [3] = new TaskAnalyze(512,1024,512,1024,int_radom);
有什么建议吗?
答案 0 :(得分:0)
试试这个:
int MAX_MATRIX = 1024;
int NUM_THREADS = 4;
int start_row = 0, end_row = 0, start_column = 0, end_column = 0;
for (int i = 0; i < NUM_THREADS/2; i++){
start_row = i*MAX_MATRIX/2;
end_row = start_row + MAX_MATRIX/2;
for (int j = 0; j < NUM_THREADS/2; j++){
start_column = j*MAX_MATRIX/2;
end_column = start_column + MAX_MATRIX/2;
System.out.println(start_row + "," + end_row + "," + start_column + "," + end_column);
}
}
应该给你以下输出:
0,512,0,512
0,512,512,1024
512,1024,0,512
512,1024,512,1024
然后你可以用你的行替换System.out.println
行:
myTask[i] = new TaskAnalyze(start_row, end_row, start_column, end_column, int_radom);
myThreads[i] = new Thread(myTask[i],"TASK " + i);