I have a java program with three threads. Each one of them executes a program that goes in an infinite loop, the first and second programs takes an input from the user (float) and they save it in a container and then the third program takes the two values to add them together then gives the result and the whole process will be repeated ..
The problem: when I give the first and second input, they will be successfully summed and outputted but after that, it will be a mess because only one of the two first threads will be running forever. I hope it's a bit clear
Here is my main function where I create the threads:
public static void main(String[] args) throws InterruptedException {
HashMap<String, Hal_int> allHalls = new HashMap<>();
try {
for (int i = 0; i < hals.keySet().size(); i++) {
allHalls.put(hals.keySet().toArray()[i].toString(),
new Hal_int(hals.keySet().toArray()[i].toString(),
hals.get(hals.keySet().toArray()[i])) );
}
/*allHalls is a container of type <String , Hal(thread type)>
that has the three threads */
allHalls.get("0").start(); // first thread
allHalls.get("1").start(); // second thread
allHalls.get("2").start(); // third thread
allHalls.get("2").join();
} catch (Exception e) {
System.out.println("Error");
}
}
Here is the Hal_int implementation :
public class Hal_int extends Thread {
private String name;
private Float acc;
private int pc;
private int[] register;
private String instruction[];
private Map<String, Buffer> ausgang = new LinkedHashMap<>();
private Map<String, Buffer> eingang = new LinkedHashMap<>();
Hal_int(String name, String datei) throws IOException {
acc = 0.0f;
pc = 0;
register = new int[10];
instruction = new String[100];
this.name = name;
readFile(datei);
}
private void interpret() throws IOException {
if (instruction[0].compareTo("START") == 0) {
String tmp = instruction[pc++];
while (tmp.compareTo("STOP") != 0) {
tmp = instruction[pc++];
if (!tmp.contains(" ")) {
if (tmp.compareTo("STOP") == 0) {
break;
} else {
System.out.println("Error!");
}
} else {
String[] arr = tmp.trim().split("\\s");
String command = arr[0];
int operand = (new Integer(arr[1]));
switch (command.toUpperCase()) {
case "OUT":
//Wenn Ausgang zu consol ist
if (ausgang.get(operand + "") == null) {
System.out.print(operand + ": ");
System.out.print(acc + "\n");
} else { // Ausgang zu Buffer
//System.out.println("Thread "+ Thread.currentThread().getName() + " passes through the interface :" + operand + " the value: " + acc);
ausgang.get(operand + "").put(acc);
}
break;
case "IN":
if (eingang.get(operand + "") == null) {
//Eingang von Consol
System.out.print(operand + ": ");
BufferedReader in = new BufferedReader(
new InputStreamReader(System.in));
acc = Float.parseFloat(in.readLine());
} else { //Eingang vom Buffer
acc = eingang.get(operand + "").get();
System.out.println("Thread " +Thread.currentThread().getName()
+ " gets from the interface :" +operand + " the value: " + acc);
}
break;
case "LOAD":
acc = (float) (register[operand - 10]);
break;
case "STORE":
(register[operand - 10]) = Math.round(acc);
break;//**
case "LOADNUM":
acc =(float) operand;
break;
case "ADD":
acc += register[operand - 10];
break;
case "ADDNUM":
acc += operand;
break;
case "SUB":
acc -= register[operand - 10];
break;
case "SUBNUM":
acc -= operand;
break;
case "MUL":
acc *= register[operand - 10];
break;
case "MULNUM":
acc *= operand;
break;
case "DIV":
acc = acc / register[operand - 10];
break;
case "DIVNUM":
acc = acc / operand;
break;
case "JUMP":
pc = operand;
break;
case "JUMPNULL":
if (acc == 0) {
pc = operand;
}
break;
case "JUMPNEG":
if (acc < 0) {
pc = operand;
}
break;
case "JUMPPOS":
if (acc > 0) {
pc = operand;
}
break;
default:
System.out.println("Unknown command: " + command);
return;
}
}
}
}
}
private void readFile(String datei) throws IOException {
File input = new File(System.getProperty("user.dir") + "\\" + datei);
FileReader fileR = new FileReader(input);
BufferedReader bufferedR = new BufferedReader(fileR);
String zeile = bufferedR.readLine();
int i = 0;
while (zeile != null) {
instruction[i++] = zeile.toUpperCase();
String[] arr = zeile.toUpperCase().trim().split("\\s");
if (arr.length >= 2 && arr[0].equals("IN") && !eingang.containsKey(arr[1])) {
eingang.put(arr[1], null);
}
if (arr.length >= 2 && arr[0].equals("OUT") && !ausgang.containsKey(arr[1])) {
ausgang.put(arr[1], null);
}
zeile = bufferedR.readLine();
}
}
@Override
public void run() {
try {
interpret();
} catch (Exception e) {
}
}
/**
* @return the ausgang
*/
public Map<String, Buffer> getAusgang() {
return ausgang;
}
/**
* @param ausgang the ausgang to set
*/
public void setAusgang(HashMap<String, Buffer> ausgang) {
this.ausgang = ausgang;
}
/**
* @return the eingang
*/
public Map<String, Buffer> getEingang() {
return eingang;
}
/**
* @param eingang the eingang to set
*/
public void setEingang(HashMap<String, Buffer> eingang) {
this.eingang = eingang;
}
}
Comments
Post a Comment