Tuesday 5 May 2009

3-A SIMPLE PROCESS SIMULATION

II. Upto here, when a JobTask is created by the JobQueue-Main routine at the beginning and when it finishes its weight it stops. This example provides a dispatcher which checks the runners queue which is passed to it via a parameter. After the check if the runners item is null it creates a JobTask3 thread object which has a fixed weight of 400. From now on, the JobQueue-Main routine does not create JobTask objects. It only creates the MonitorTask first and then creates the DispatcherTask. These tasks create JobTasks and monitor them as lonh as their own TaskWeights allow them. In fact these values are 400 for JobTask and MonitorTask and 600 for DispatcherTask in our example:


package stochasticprocess;

public class JobQueue3 {

private final static int NUMRUNNERS = 5;

public JobQueue3() {
}
public static void main(String[] args) {
JobTask3[] runners = new JobTask3[NUMRUNNERS];

// for (int i = 0; i < NUMRUNNERS; i++) {
// runners[i] = new JobTask3(i);
// runners[i].setPriority(2);
// }
//
// for (int i = 0; i < NUMRUNNERS; i++)
// runners[i].start();

MonitorTask3 monitorTask3 = new MonitorTask3(999, runners);
monitorTask3.setPriority(1);
monitorTask3.start();

DispatcherTask3 dispatcherTask3 = new DispatcherTask3(111, runners);
dispatcherTask3.setPriority(1);
dispatcherTask3.start();

}
}

class JobTask3 extends Thread {
public int taskWeight = 400;
public int taskWeightCounter = 1;
private int taskId;

public JobTask3(int taskId) {
this.taskId = taskId;
}

public void run() {
while (taskWeightCounter < taskWeight) {
taskWeightCounter++;

try {
sleep(10);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}

if ((taskWeightCounter % 50) == 0)
System.out.println("Job Task Thread #" + taskId + ", taskWeightCounter = " + taskWeightCounter);
}
}
}
class MonitorTask3 extends Thread {
private int taskWeight = 400;
public int taskWeightCounter = 1;
private int taskId;
JobTask3[] runningTasks;
int sumRunningTasks;

public MonitorTask3(int taskId, JobTask3[] runnerTasks) {
this.taskId = taskId;
this.runningTasks = runnerTasks;
}

public void run() {
while (taskWeightCounter < taskWeight) {
taskWeightCounter++;
try {
sleep(20);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}

if ((taskWeightCounter % 50) == 0)
{
sumRunningTasks = 0;
for(int i=0; i < runningTasks.length; i++){
if (runningTasks[i] != null)
sumRunningTasks += runningTasks[i].taskWeight - runningTasks[i].taskWeightCounter;
}
System.out.print("==========================================>");
System.out.println("Monitor Task Thread # Running Job Tasks=" + sumRunningTasks);
System.out.println("Monitor Task Thread #" + taskId + ", taskWeightCounter = " + taskWeightCounter);
}
}
}
}
class DispatcherTask3 extends Thread {
public int taskWeight = 600;
public int taskWeightCounter = 1;
private int taskId;
JobTask3[] runningTasks;

public DispatcherTask3(int taskId, JobTask3[] runnerTasks) {
this.taskId = taskId;
this.runningTasks = runnerTasks;
}

public void run() {
while (taskWeightCounter < taskWeight) {
taskWeightCounter++;

try {
sleep(10);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}

if ((taskWeightCounter % 50) == 0)
{
for (int i = 0; i < runningTasks.length; i++) {
if (runningTasks[i] != null) {
if ((runningTasks[i].taskWeight - runningTasks[i].taskWeightCounter) == 0) {
System.out.println("Dispatcher Task Thread dispatches new JobTaskkkkkkkkkkkkkkkkkkkkkkkkkkkk");
runningTasks[i] = new JobTask3(i);
runningTasks[i].setPriority(2);
runningTasks[i].start();
}
}
else {
runningTasks[i] = new JobTask3(i);
runningTasks[i].setPriority(2);
runningTasks[i].start();
}
}
System.out.println("Dispatcher Task Thread #" + taskId + ", taskWeightCounter = " + taskWeightCounter);
}
}
}
}