Tuesday, 5 May 2009

4-A SIMPLE PROCESS SIMULATION

This example introduces randomness to the JobTasks by changing the sleep time of the dispatcher:

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

try {
sleep(10+(int)(100*Math.random()));
} catch (InterruptedException e) {
throw new RuntimeException(e);
}

It also arranges MonitorTask’s weight to 2000.

package stochasticprocess;

public class JobQueue4 {

private final static int NUMRUNNERS = 10;

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

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

MonitorTask4 monitorTask4 = new MonitorTask4(999, runners);
monitorTask4.setPriority(1);
monitorTask4.start();

DispatcherTask4 dispatcherTask4 = new DispatcherTask4(111, runners);
dispatcherTask4.setPriority(1);
dispatcherTask4.start();

}
}

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

public JobTask4(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 MonitorTask4 extends Thread {
private int taskWeight = 2000;
public int taskWeightCounter = 1;
private int taskId;
JobTask4[] runningTasks;
int sumRunningTasks;
int taskLoadLimit=300;

public MonitorTask4(int taskId, JobTask4[] 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 DispatcherTask4 extends Thread {
public int taskWeight = 600;
public int taskWeightCounter = 1;
private int taskId;
JobTask4[] runningTasks;

public DispatcherTask4(int taskId, JobTask4[] 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 JobTask4(i);
runningTasks[i].setPriority(2);
runningTasks[i].start();
break;
}
}
else {
runningTasks[i] = new JobTask4(i);
runningTasks[i].setPriority(2);
runningTasks[i].start();
}
}
System.out.println("Dispatcher Task Thread #" + taskId + ", remaining taskWeight = " + (taskWeight - taskWeightCounter));
}
}
}
}