Tuesday, 5 May 2009


This is an example for process simulation with simple and complete source codes for each development phase.
The first phase is taken from
http://www.java2s.com/ . My final example produces JobTasks with random weights at random moments of time and checks whether the total workload limit of a system of 5 workers is exceeded. The system can be made dynamic but queue length is constant in my examples.

I. There is a JobTask queue named runners which holds 5 Tasks. JobTask class is a thread which holds the task difficulty (or amount or duration) initially in the taskWeight variable and processes this value through the taskWeightCounter variable. Every time the task waits up it increases the taskWeightCounter and then waits for a constantt duration via sleep(10). A status message is printed when taskWeightCounter is a multiple of 50. The tasks are terminated when the taskWeightCounter reaches the value in the taskWeight. JobQueue – main routine instantiates the JobTasks in the runners queue and starts them.

package stochasticprocess;

public class JobQueue {

private final static int NUMRUNNERS = 5;

public JobQueue() {
public static void main(String[] args) {
JobTask[] runners = new JobTask[NUMRUNNERS];

for (int i = 0; i < NUMRUNNERS; i++) {
runners[i] = new JobTask(i);
for (int i = 0; i < NUMRUNNERS; i++)

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

public JobTask(int taskId) {
this.taskId = taskId;

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

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

if ((taskWeightCounter % 50) == 0)
System.out.println("Job Task Thread #" + taskId + ", taskWeightCounter = " + taskWeightCounter);