Java 在main中创建数组并用来自不同线程的数据填充它
好吧,我在这方面遇到了麻烦,也许我只是想得太久了,或者我很笨,但以下是我所拥有的和我正在尝试做的: 更新-代码全部修复,不再出现运行问题。Java 在main中创建数组并用来自不同线程的数据填充它,java,arrays,multithreading,Java,Arrays,Multithreading,好吧,我在这方面遇到了麻烦,也许我只是想得太久了,或者我很笨,但以下是我所拥有的和我正在尝试做的: 更新-代码全部修复,不再出现运行问题。 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]; //
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){}
}
}
当我从myClass打印w的输出时:
w = new int[5];
我得到w=100
但是我想要w=11
编辑-我现在得到正确的输出-检查代码是否有更改在这部分
myThreadClass(w[i])
您没有传递索引,而是传递一个值,该值为零,因为w
是一个由5个元素组成的数组,所有元素都用默认值0初始化
您应该改为
myThreadClass(i)
。在这部分myThreadClass(w[i])
您没有传递索引,而是传递一个值,该值为零,因为w
是一个由5个元素组成的数组,所有元素都用默认值0初始化
您应该改为执行myThreadClass(i)。
w[]
最初是全部0
。您正在将其中一个值传递给线程构造函数w[]
最初都是ZERO
。您正在将这些值中的一个传递给线程构造函数,这一行来自myClass:
w = new int[5];
将w的所有元素初始化为0
那么,当你打电话的时候
T = new Thread( new myThreadClass(w[i]));
您的团队正在有效地做到这一点:
T = new Thread( new myThreadClass(0));
因此,w[]唯一会改变的元素是第一个元素。myClass中的这一行:
w = new int[5];
将w的所有元素初始化为0
那么,当你打电话的时候
T = new Thread( new myThreadClass(w[i]));
您的团队正在有效地做到这一点:
T = new Thread( new myThreadClass(0));
因此,w[]唯一会改变的元素是第一个元素。这里有一个针对您的问题的过度设计的解决方案。没有封装也可以做得很好,但我决定使用它,因为它使示例更具可读性
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);
}
}
}
公共类测试{
公共静态void main(字符串[]args){
//创建包含结果的结果集
结果集结果集=新结果集(5);
线程[]线程=新线程[resultSet.getSize()];
//创建线程
对于(int i=0;i
这里有一个针对您的问题的过度设计的解决方案。没有封装也可以做得很好,但我决定使用它,因为它使示例更具可读性
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);
}
}
}
公共类测试{
公共静态void main(字符串[]args){
//创建包含结果的结果集
结果集结果集=新结果集(5);
线程[]线程=新线程[resultSet.getSize()];
//创建线程
对于(int i=0;i