Tuesday 5 May 2009

5-A SIMPLE PROCESS SIMULATION

V. This example introduces randomnes to the weights of JobTasks directly. As in:
class JobTask4 extends Thread {
public int taskWeight = 1+(int)(400*Math.random());
With this, both the duration and the starting times of JobTasks become random.
package stochasticprocess;

public class JobQueue5 {

private final static int NUMRUNNERS = 10;

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

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

MonitorTask5 monitorTask5 = new MonitorTask5(999, runners);
monitorTask5.setPriority(1);
monitorTask5.start();

DispatcherTask5 dispatcherTask5 = new DispatcherTask5(111, runners);
dispatcherTask5.setPriority(1);
dispatcherTask5.start();

}
}

class JobTask5 extends Thread {
public int taskWeight = 1+(int)(400*Math.random());
public int taskWeightCounter = 1;
private int taskId;

public JobTask5(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 + ", remaining taskWeight = " + (taskWeight - taskWeightCounter));
}
}
}
class MonitorTask5 extends Thread {
private int taskWeight = 2000;
public int taskWeightCounter = 1;
private int taskId;
JobTask5[] runningTasks;
int sumRunningTasks;
int taskLoadLimit=300;

public MonitorTask5(int taskId, JobTask5[] 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 total Running Job taskWeight =" + sumRunningTasks);
System.out.println("Monitor Task Thread #" + taskId + ", remaining taskWeight = " + (taskWeight - taskWeightCounter));
if (sumRunningTasks > taskLoadLimit){
System.out.print("++++++++++++++++++++++++++++++++++++++++++++++>");
System.out.println("Task Load limit " + taskLoadLimit + "is exceeded by " + (sumRunningTasks - taskLoadLimit ));
}
}
}
}
}
class DispatcherTask5 extends Thread {
public int taskWeight = 600;
public int taskWeightCounter = 1;
private int taskId;
JobTask5[] runningTasks;

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

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

try {
sleep(10+(int)(100*Math.random()));
} 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 Thread #" + i);
runningTasks[i] = new JobTask5(i);
runningTasks[i].setPriority(2);
runningTasks[i].start();
break;
}
}
else {
runningTasks[i] = new JobTask5(i);
runningTasks[i].setPriority(2);
runningTasks[i].start();
}
}
System.out.println("Dispatcher Task Thread #" + taskId + ", remaining taskWeight = " + (taskWeight - taskWeightCounter));
}
}
}
}