我正在尝试使用多线程将大块处理数据分解成更小的块 我遇到的问题是我的线程没有针对其数据空间的指定部分运行。
例如:
thread-2应该处理0 - 1000
的范围thread-3应该处理1001 - 2000的范围
当我将我的新主题背靠背调用时,我得到:
thread-2 = 0 - 1000
thread-3 = 0 - 1000
当我在两个线程调用之间添加一个线程sleep(3000)
时,我得到:
thread-2 = 0 - 1000
thread-3 = 0 - 2000
我不确定我做错了什么,真的很感激一些指导。
请注意下面的Snippits,我在上面的示例中缩写了以上数字的实际通话范围是
1,000,000 - 1,001,000和1,001,001 - 1,002,000
主要方法详细说明线程调用的片段:
try {
int start = 1000000;
int end = 1001000;
new Thread(new MyThread(start, end, PUBLIC_INVALID_LIST)).start();
Thread.sleep(3000);
start = 1001001;
end = 1002000;
new Thread(new MyThread(start, end, PUBLIC_INVALID_LIST)).start();
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
来自MyThread的 Snippet扩展了Thread
这详细说明了我如何将params从main方法传递到run()
方法:
//variables to pass from constructor to run()
private int startIndex;
private int endIndex;
private ArrayList PUBLIC_INVALID_LIST;
MyThread(int startIndex, int endIndex, ArrayList PUBLIC_INVALID_LIST) {
this.startIndex = startIndex;
this.endIndex = endIndex;
this.PUBLIC_INVALID_LIST = PUBLIC_INVALID_LIST;
}//end of initializer
public void run() {
答案 0 :(得分:0)
仍然不确定变量传入的问题是什么,但我在方法中使用int []
进行了替代传递int sizeSet = (max /100000) + 1;//max is highest path_id value
int[] start_range = new int[sizeSet];
int[] end_range = new int[sizeSet];
start_range[0] = 1;//first possible path_id
end_range[0] = 100000;//end point of first possible range
int rangeIndex = 1;//create counter for while loop
while(rangeIndex < sizeSet){
start_range[rangeIndex] = start_range[rangeIndex - 1] + 100000;
end_range[rangeIndex] = end_range[rangeIndex - 1] + 100000;
++ rangeIndex;
}//end range setting while block
rangeIndex = 0;//reset index counter
while(rangeIndex < sizeSet){
new Thread(new MyThread(start_range[rangeIndex], end_range[rangeIndex], PUBLIC_INVALID_LIST)).start();
++ rangeIndex;
}