Moin,
kennt jemand ein Beispiel, welches das Racing Condition- Problem gut beschreibt mit einer Konsolenausgabe, ich hab zwar versucht eines zu machen, aber das klappt noch nicht so,
kennt jemand ein Beispiel, welches das Racing Condition- Problem gut beschreibt mit einer Konsolenausgabe, ich hab zwar versucht eines zu machen, aber das klappt noch nicht so,
Code:
public class StartThread extends Thread {
private Table table;
private Thread th1;
private Thread th2;
public StartThread() {
table = new Table();
th1 = new Thread(this);
th1.setName("1");
th2 = new Thread(this);
th2.setName("2");
th1.start();
th2.start();
}
public static void main(String[] args) {
new StartThread();
}
@Override
public void run() {
while (true) {
try {
System.out.println("Das ist der Name: " + StartThread.currentThread().getName());
table.fillTable(5);
}
catch (Exception ex) {
th1.interrupt();
th2.interrupt();
System.exit(1);
System.out.println("Ausnahme in Klasse StartThread");
}
}
}
}
Code:
public class Table {
private Integer[] tableArray = new Integer[10];
private int index = 0 ;
public Table() {
}
public void fillTable(Integer s) throws Exception {
tableArray[index] = s;
index++;
showTable();
}
public void showTable() {
for(int i = 0; i < tableArray.length; i++)
System.out.print(tableArray[i] + ",");
System.out.println("");
}
}