好的,所以我遇到了麻烦,也许我只是想得太久或者很愚蠢,但这是我所拥有的以及我正在尝试做的事情:
更新代码全部修复,不再出现运行问题。
public class myClass program {
int [] w = null;
int [] x = null;
Thread T = null;
public static void main(String [] args){
x = new int[5];
w = new int[5];
// here i am trying to invoke a new thread passing the index
// of my array, then incrementing the index each time i create a new thread
// the purpose is to fill each index each time the new thread runs.
for(int i = 0; i < w.length; i ++){
// T = new Thread(new myThreadClass(w[i])); // only passes 0 take this out and
T = new Thread( new myThreadClass(i)); // pass i so the position changes
T.start();
try{
Thread.sleep(100);
}catch(Exception e){}
}
}
在我的单独类 myThreadClass.java 中,我有以下内容:
public class myThreadClass extends Thread{
int [] w = null;
int position = 0;
int value = 1;
public myThreadClass(int p){
this.position = p
w = myClass.w;
}
@Override
public void run(){
// synchronize the thread so there is no memory cache problems
//
synchronized(w){
w[position] = value;
}
}
}
当我从 myClass 打印出 w 的输出时:
我明白了w = 1 0 0 0 0
但我想要w = 1 1 1 1 1
已编辑-我现在得到正确的输出-检查代码是否有更改