schafft Mein Programm eine Klasse "MyArray"durch einen 2D-Array zu einer Klasse Passing Multithreading
class MyArray{
int[][] arr={{14,15,16},{11,12,13}};
}
Und eine andere Klasse "Add_Thread"
class Add_Thread implements Runnable {
private final Matrix RES;
int row;
Thread t;
MyArray arr=new MyArray();
public Add_Thread(Matrix RES,int row) {
this.RES = RES;
t = new Thread(this);
t.start();
this.row = row;
}
public void run() {
synchronized(arr){
for (int i = 0; i < arr.arr[0].length; i++) {
RES.push(arr.arr[row][i]);
}
}
}
}
den Wert von "MyArray" nimmt und schiebt ihn zu einer anderen Klasse „Matrix“ speichert
class Matrix {
final int[][] res_matrix;
int i = 0;
int j = 0;
private int k = 0;
private int l = 0;
public Matrix(int i, int j) {
this.i = i;
this.j = j;
res_matrix = new int[i][j];
}
public synchronized void push(int element) {
int pos1 = k;
int pos2 = l;
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(ResultantMatrix.class.getName()).log(Level.SEVERE, null, ex);
}
res_matrix[pos1][pos2] = element;
k++;
l++;
}
}
sie den Wert und an der Funktion main() sendet
public class Array_demo {
public static void main(String[] args) {
Matrix res = new Matrix(2, 3);
Add_Thread add=new Add_Thread(res, 0);
Add_Thread add2=new Add_Thread(res, 1);
for(int i=0;i<res.res_matrix.length;i++)
{
for(int j=0;j<res.res_matrix[0].length;j++)
{
System.out.print(res.res_matrix[i][j]+" ");
}
System.out.println("");
}
}
}
Aber ich bin immer Fehler in Leere push() Verfahren und res_matrix [Pos1] [pos2] = Element. Ich bin neu zu thread, so wäre es toll, wenn meine Fehler breit erklärt werden.
Output
0 0 0
0 0 0
Exception in thread "Thread-0" java.lang.ArrayIndexOutOfBoundsException: 2
at threading.Matrix.push(Push.java:34)
at threading.Add_Thread.run(Push.java:58)
at java.lang.Thread.run(Thread.java:745)
Exception in thread "Thread-1" java.lang.ArrayIndexOutOfBoundsException: 2
at threading.Matrix.push(Push.java:34)
at threading.Add_Thread.run(Push.java:58)
at java.lang.Thread.run(Thread.java:745)
Werden Sie die Fehlermeldung mit uns teilen? Wenn es sich um eine Laufzeitausnahmebedingung handelt, müssen Sie den vollständigen Stack-Trace einschließen. [edit] dein Beitrag, antworte nicht in einem Kommentar. –