package asg.ass2; import java.io.Serializable; import java.util.Random; import java.util.List; import java.util.ArrayList; public class GodFather { public static void main(String[] args) { List jobQueue = new ArrayList(); List answerQueue = new ArrayList(); int n = Integer.parseInt(args[0]); int s = Integer.parseInt(args[1]); int w = Integer.parseInt(args[2]); int t = Integer.parseInt(args[3]); Runnable runObj = new AssignJobs(jobQueue, answerQueue); Thread threads[] = new Thread[t]; for (int i = 0; i < t; i++) { threads[i] = new Thread(runObj); threads[i].start(); } int temp = 0; synchronized (jobQueue) { for (int i = 0; i < w; i++) { int temp2= Math.abs((n-i)); int initialx = ((temp+temp2)% n) +1; int initialy = ((temp+temp2)% n) +1; int initialz = ((temp+temp2)% n) +1; Job task = new Job(new XYZcoordinate(initialx, initialy,initialz), s, n); jobQueue.add(task); jobQueue.notifyAll(); } } synchronized (answerQueue) { while (answerQueue.isEmpty() || answerQueue.size() < w) { try { answerQueue.wait(); } catch (InterruptedException e) { } } System.out.println("\nLength of Dimension of Space to Walk: " + n); System.out.println("\nNumber of Steps in each Random Walk: " + s); System.out.println("\nNumber of Random Walk to be performed: " + w); System.out.println("\nNumber of Worker Threads: " + t+"\n"); System.out.println("\nThe End Co-ordinates of each of the Random Walk is as shown below:\n"); int walknum = 0; for (Result result=answerQueue.get(walknum);walknum < answerQueue.size();walknum++) { result=answerQueue.get(walknum); System.out.println("\n Random Walk #" + walknum + "\n Start Point: "+ result.getStrpt().getX() + " "+ result.getStrpt().getY() + " "+ result.getStrpt().getZ()); System.out.println(" End Point: "+ result.getEndpt().getX() + " "+ result.getEndpt().getY() + " "+ result.getEndpt().getZ()); } } System.exit(0); } // main function ends } // GodFather class ends class XYZcoordinate implements Serializable { private static final long serialVersionUID = 1L; private int x; private int y; private int z; public XYZcoordinate(int xco, int yco, int zco) { x = xco; y = yco; z = zco; }////constructor of this class public int getX() { return x; } public int getY() { return y; } public int getZ() { return z; } public void setX(int xco) { x = xco; } public void setY(int yco) { y = yco; } public void setZ(int zco) { z = zco; } }// class threeD point ends here class Result implements Serializable { private static final long serialVersionUID = 1L; private XYZcoordinate strpt; private XYZcoordinate endPt; public XYZcoordinate getStrpt() { return strpt; } public XYZcoordinate getEndpt() { return endPt; } public void setStrpt(XYZcoordinate startpoint) { strpt = startpoint; } public void setEndpt(XYZcoordinate endpt) { endPt = endpt; } }// class result ends here class AssignJobs implements Runnable { List jobQueue = null; List resultQueue = null; int numberOfSteps = 0; int dimension = 0; public AssignJobs(List tQ, List rQ) { jobQueue = tQ; resultQueue = rQ; } // end of constructor class public void run() { while (true) { Job job = null; synchronized (jobQueue) { while (jobQueue.isEmpty()) { try { jobQueue.wait(); } catch (InterruptedException e) { } } job = jobQueue.get(0); jobQueue.remove(0); } Result result = beginRandomWalk(job); synchronized (resultQueue) { resultQueue.add(result); resultQueue.notifyAll(); } } // while loops ends } // end of run function private Result beginRandomWalk(Job assignedtask) { Result fincordRandWalk = new Result(); XYZcoordinate curpt = new XYZcoordinate(assignedtask.getStartPoint().getX(), assignedtask.getStartPoint().getY(), assignedtask.getStartPoint().getZ()); int rand = 0; int limit = 0; Random genrtRand = null; for (int i = 0; i < assignedtask.getNoSteps2Walk(); i++) { int n = assignedtask.getDimension(); int x = curpt.getX(); int y = curpt.getY(); int z = curpt.getZ(); int limstart = 0; genrtRand = new Random(System.currentTimeMillis()); rand = genrtRand.nextInt(); rand = Math.abs(rand); rand = rand % (int)(Math.pow(assignedtask.getDimension(),3)); limit = x * (n - y) * (n - z); if (limstart <= rand && rand < (limstart + limit)) { if (curpt.getX() < n) { curpt.setX(curpt.getX() + 1); } } else { limstart = limstart + limit; limit = (n - x) * y * z; if (limstart <= rand && rand < (limstart + limit)) { if (curpt.getX() > 1) { curpt.setX(curpt.getX() - 1); } } else { limstart = limstart + limit; limit = (n - x) * y * (n - z); if (limstart <= rand && rand < (limstart + limit)) { if (curpt.getY() < n) { curpt.setY(curpt.getY() + 1); } } else { limstart = limstart + limit; limit = x * (n - y) * z; if (limstart <= rand && rand < (limstart + limit)) { if (curpt.getY() > 1) { curpt.setY(curpt.getY() - 1); } } else { limstart = limstart + limit; limit = (n - x) * (n - y) * z; if (limstart <= rand&& rand < (limstart + limit)) { if (curpt.getZ() < n) { curpt.setZ(curpt.getZ() + 1); } } else { limstart = limstart + limit; limit = x * y * (n - z); if (limstart <= rand && rand < (limstart + limit)) { if (curpt.getZ() > 1) { curpt.setZ(curpt.getZ() - 1); } } } } } } } }// for ends here fincordRandWalk.setStrpt(assignedtask.getStartPoint()); fincordRandWalk.setEndpt(curpt); return fincordRandWalk; }// perform random walk function ends here }// MyRunnable class ends here class Job implements Serializable { private static final long serialVersionUID = 1L; private XYZcoordinate startPoint = null; private int numberOfSteps; private int Dimension; public Job(XYZcoordinate startPnt, int noOfSteps, int dim) { super(); startPoint = startPnt; numberOfSteps = noOfSteps; Dimension = dim; } ///// constructor of this class public int getNoSteps2Walk() { return numberOfSteps; } public XYZcoordinate getStartPoint() { return startPoint; } public int getDimension() { return Dimension; } public void setNoSteps2Walk(int noOfSteps) { numberOfSteps = noOfSteps; } public void setDimension(int dimension) { Dimension = dimension; } public void setStartPoint(XYZcoordinate startpoint) { startPoint = startpoint; } }// class task ends here. // //////////////////////////END of PROGRAME///////////////////////////////////////////