Thread Simulation Version 1 (part)
// Processor1.java
// It is a thread class to be run from SimV1
// It receives parameters from the parent, a thread name and ID number
import java.lang.Math;
import java.util.Random;
public class Processor1 extends Thread {
private int myid; // private to this thread
private int cacheindex[] = new int [5];
private char cachedata[] = new char [5];
private int i; //for loop counter
private int CPUaddress; // random address requested by CPU
Random randGen; // a pointer to a Random generator
Processor1(String name, int me) { //constructor - executed when thread is created with new
super(name); //super is part of the Thread class
myid=me;
randGen = new Random(5); // seed the random number sequence
start(); // by default calls method "run"
}
public void run() {
SimV1.addrbus = 1000;
SimV1.databus = 'X';
for (i = 0; i < 5; i++){
cacheindex[i] = -1;
}
System.out.println("Howdy from " + Thread.currentThread().getName() +
", id=" + myid + " addrbus = " + SimV1.addrbus);
//for (i = 0; i < 5; i++){ System.out.println(cacheindex[i]); }
for (i = 1; i < 6; i++) { // controls cycles of CPU>address on bus.
CPUaddress = Math.abs(randGen.nextInt()) % 10; //address between 0 and 9
SimV1.addrbus = CPUaddress; //address on the bus
SimV1.databus = '0'; //reset the databus to '0'
System.out.println("Update from " + Thread.currentThread().getName() +
", id=" + myid + " addrbus = " + SimV1.addrbus);
//Now we wait for memory to respond
try { Thread.sleep(500); //milliseconds
} catch (InterruptedException e) { return; }//end of try
while (SimV1.databus == '0') {
try {Thread.sleep(100);}// wait 1/2 second
catch (InterruptedException e) {return;} // end of try
} //spin loop waiting for memory to send data
System.out.println(" Processor found data: " + SimV1.databus);
}//end of address generating for loop
SimV1.addrbus = -2; // signal memory thread we are done
}//end run
} // end class