好的,所以我遇到了麻烦,也许我一直在思考太久或者是愚蠢但是这就是我拥有的和我想做的事情:
更新代码全部修复了运行问题。
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
EDITED-我现在正在获得正确的输出 - 检查代码是否有变化
答案 0 :(得分:2)
在这部分myThreadClass(w[i])
中你没有传递一个索引,你传递的值为零,因为w
是一个包含5个元素的数组,所有这些都是用默认值0初始化的
您应该改为myThreadClass(i)
。
答案 1 :(得分:1)
w[]
最初都是ZERO
。您正在将其中一个值传递给线程构造函数
答案 2 :(得分:1)
来自myClass的这一行:
w = new int[5];
将w的所有元素初始化为0。
所以,当你打电话
T = new Thread( new myThreadClass(w[i]));
你实际上是这样做的:
T = new Thread( new myThreadClass(0));
所以w []的唯一元素就是第一个。
答案 3 :(得分:0)
这是针对您的问题的过度设计的解决方案。你可以在没有封装的情况下做得很好,但我决定使用它,因为它使示例更具可读性。
public class Test {
public static void main(String[] args) {
// Create the resultset containing the result
ResultSet resultSet = new ResultSet(5);
Thread[] threads = new Thread[resultSet.getSize()];
// Create threads
for (int i = 0; i < resultSet.getSize(); i++) {
threads[i] = new Thread(new TestTask(
resultSet.createResultSetter(i)));
}
// Start threads
for (int i = 0; i < resultSet.getSize(); i++) {
threads[i].start();
}
// Wait until threads complete
for (int i = 0; i < resultSet.getSize(); i++) {
try {
threads[i].join();
} catch (InterruptedException exception) {
// ??!
}
}
// Print the result
for (int i = 0; i < resultSet.getSize(); i++) {
System.out.println(resultSet.getResult(i));
}
}
/**
* Interface used to set the result
*/
public static interface ResultSetter {
public void setResult(int result);
}
/**
* Container class for results
*/
public static class ResultSet {
private final int[] results;
public ResultSet(int size) {
results = new int[size];
}
public int getSize() {
return results.length;
}
public ResultSetter createResultSetter(final int position) {
return new ResultSetter() {
public void setResult(int result) {
ResultSet.this.setResult(position, result);
}
};
}
public synchronized int getResult(int position) {
return results[position];
}
public synchronized void setResult(int position, int result) {
results[position] = result;
}
}
/**
* A task executed by a thread
*/
public static class TestTask implements Runnable {
private ResultSetter resultSetter;
public TestTask(ResultSetter resultSetter) {
this.resultSetter = resultSetter;
}
@Override
public void run() {
resultSetter.setResult(1);
}
}
}