Thread Simulation Version 1 (part)
// Memory1.java
// It is a thread class to be run from SimV1
// It receives parameters from the parent, a thread name and ID number
public class Memory1 extends Thread {
private int myid; // private to this thread
private char memory[] = new char [10]; //memory array
private int i; // counter
Memory1(String name, int me) { //constructor - executed when thread is created with new
super(name); //super is part of the Thread class
myid=me;
start(); // by default calls method "run"
}
public void run() {
System.out.println("Howdy from " + Thread.currentThread().getName() +
", id=" + myid + " addrbus = " + SimV1.addrbus + " databus = " + SimV1.databus);
for (i = 0; i < 10; i++){
memory[i] = (char) ('A' + i); // note the use of casting to force the conversion
}
//for (i = 0; i < 10; i++){ System.out.println(memory[i]); }
System.out.println(" Memory waiting for an address on the bus.");
while (SimV1.addrbus != -2) { //use -2 as a terminate flag from processor
try { Thread.sleep(1000); //milliseconds - slows things down for viewing
} catch (InterruptedException e) { return; }
if (SimV1.addrbus > -1) {
System.out.println(" Memory found an address: " + SimV1.addrbus);
//System.out.println( " Memory at " + SimV1.addrbus + " = " + memory[SimV1.addrbus]);
SimV1.databus = memory[SimV1.addrbus];
}
} // end of while loop
}// end of run
} // end of class