java - 我应该传递哪些参数来平均分配函数调用之间的工作?
问题描述
我有一个称为doWork(int a, int b, int c, int amount)
计算总计算集数量的函数。在我的主要内容中,我确定每个函数调用应该执行多少计算。我需要知道的是如何传递a,b,c
,以便对函数的每次调用都会执行它自己的计算部分。
int nprocs = 2;
int total = 10
public static void main() {
//total number of calculations to compute
int totalLoad = ((total*total*total)/6)-(total/6);
//number of calculations per function call
int workLoad = totalLoad/nproc;
for(int i = 0; i < nproc; i++) {
doWork(a, b, c, workLoad);
}
}
public void doWork(int i,int j,int k, int amount) {
boolean start = true;
for(; i < total && amount != 0; i++) {
if(!start) {
j = i+1;
}
for(; j < total && amount != 0; j++) {
if(!start) {
k = j;
}
for(; k < total && amount != 0; k++) {
if(start) {
start = false;
}
//some calculation here
System.out.println(format(i,j,k));
amount--;
}
}
}
每个函数调用都需要处理要进行的总计算的不同子部分。例如:
nproc = 2
totalLoad = 6
Total work: Function call 1:
calc 1 calc 1
calc 2 calc 2
calc 3 calc 3
calc 4
calc 5 Function call 2:
calc 6 calc 4
calc 5
calc 6
此外,是否有更简洁的方法来编写 doWork() 函数。这对我来说似乎很乱。
解决方案
推荐阅读
- javascript - 多个动态 javascript 上传按钮
- c# - :System.IO.FileLoadException:无法加载文件或程序集“EntityFramework”
- javascript - 动态文本框仅使用一次 jquery 循环
- spring-webflux - 在 Mono 上重试似乎会生成一个无限循环
- android - 具有动态大小 Flutter 的 ModalBottomSheet 中的 tabBar
- python - 使用键重新组合 pandas 中的数据框。比遍历行更快的方法?
- reactjs - 无法编辑输入(material-ui & react-draggable)
- dart - 如何使用具有参数化返回类型的函数参数调用函数
- python - 在 QTextEdit 中自动添加括号或引号
- mysql - PRIMARY KEY [MySql MariaDB 10.4.11] 忽略名称“”