From ef42d9791f2b51aaa7c27d8a7cf75be4f32edd93 Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Sat, 10 Nov 2012 10:44:10 -0500 Subject: [PATCH] Add in the reflection code for Jobs --- FirstFit.java | 5 +++-- Job.java | 7 +++++-- dummyAlgorithm.java | 18 ++++++++++++++++-- jobThread.java | 9 +++++++-- memoryManagement.java | 3 --- 5 files changed, 31 insertions(+), 11 deletions(-) diff --git a/FirstFit.java b/FirstFit.java index ab89131..b36c1e7 100644 --- a/FirstFit.java +++ b/FirstFit.java @@ -15,7 +15,7 @@ class FirstFit implements baseAlgorithm startLoc, endLoc, blkSize, - memSize = memoryManagement.memory, + memSize, active, noJobs=0, s1=0, @@ -26,8 +26,9 @@ class FirstFit implements baseAlgorithm private int[] memory = new int[memSize]; //this is a no argument constructor - public FirstFit() + public FirstFit(int memSize) { + this.memSize = memSize; memTable[0][0]=0; //job number memTable[0][1]=0; //job size memTable[0][2]=0; //start location in memory diff --git a/Job.java b/Job.java index 2eabae4..23cddf9 100644 --- a/Job.java +++ b/Job.java @@ -14,9 +14,12 @@ public class Job { private jobThread myThread; //Reference to the thread we control private boolean myThreadPaused; //Used to keep track of the execution state of our thread - public Job(int jobTime, int jobID, Method parentAlgorithmDeallocate, int jobSize, int beginningLocation ){ + public Job(int jobTime, int jobID, int jobSize, int beginningLocation, Method parentAlgorithmDeallocate, Object parentAlgorithm ){ //Create a new job, and start it running - myThread = new jobThread(jobTime, jobID, parentAlgorithmDeallocate, jobSize, beginningLocation); + myThread = new jobThread(jobTime, jobID,jobSize, beginningLocation, parentAlgorithmDeallocate, parentAlgorithm); + } + + public void start(){ myThread.start(); myThreadPaused = false; } diff --git a/dummyAlgorithm.java b/dummyAlgorithm.java index cf4ed64..28b50d7 100644 --- a/dummyAlgorithm.java +++ b/dummyAlgorithm.java @@ -1,3 +1,5 @@ +import java.lang.reflect.*; + public class dummyAlgorithm implements baseAlgorithm{ int[] memoryBlock; @@ -9,9 +11,21 @@ public class dummyAlgorithm implements baseAlgorithm{ public void allocate(int jobID, int jobSize, int jobTime){ /* This method to be overloaded by each algorithm */ - System.out.println("Allocating job " + jobID + " with size: " + jobSize + " for: " + jobTime + " milliseconds."); + + //Generic code to get this classes deallocate() method + Method deallocateMethod; + try { + deallocateMethod = this.getClass().getMethod("deallocate", new Class[]{int.class, int.class}); + Job newJob = new Job(jobTime, jobID, jobSize, 999, deallocateMethod, this); + newJob.start(); + System.out.println("Allocating job " + jobID + " with size: " + jobSize + " for: " + jobTime + " milliseconds."); + } catch (NoSuchMethodException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } } + public void deallocate(int jobSize, int beginningLocation){ - System.out.println("Removing job with size: " + jobSize + " beginning at: " + beginningLocation); + System.err.println("Removing job with size: " + jobSize + " beginning at: " + beginningLocation); } } diff --git a/jobThread.java b/jobThread.java index edfecbf..72cf764 100644 --- a/jobThread.java +++ b/jobThread.java @@ -1,3 +1,4 @@ +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class jobThread extends Thread { @@ -9,14 +10,16 @@ public class jobThread extends Thread { private int jobID; private boolean jobDone; private Method parentAlgorithmDeallocate; //Our parent to notify when we're done. + private Object parentAlgorithm; //The actual instance of our parent class /* Fields that we need to know when calling the deallocate */ private int jobSize; private int beginningLocation; - public jobThread(long jobTime, int jobID, Method parentAlgorithmDeallocate, int jobSize, int beginningLocation){ + public jobThread(long jobTime, int jobID, int jobSize, int beginningLocation, Method parentAlgorithmDeallocate, Object parentClass){ this.jobTime = jobTime; this.parentAlgorithmDeallocate = parentAlgorithmDeallocate; + this.parentAlgorithm = parentClass; this.elapsedTime = 0; this.isPaused = false; this.startTime = 0; @@ -76,8 +79,10 @@ public class jobThread extends Thread { } } + Object[] deallocateArgs = {this.jobSize, this.beginningLocation}; //We're done, go ahead and notify our algorithm to clean us up - parentAlgorithmDeallocate.invoke(this.jobSize, this.beginningLocation); + parentAlgorithmDeallocate.invoke(parentAlgorithm, deallocateArgs); + } catch (Exception e) { return; } diff --git a/memoryManagement.java b/memoryManagement.java index 212e976..9cbbb9d 100644 --- a/memoryManagement.java +++ b/memoryManagement.java @@ -9,9 +9,6 @@ import java.util.StringTokenizer; public class memoryManagement{ - //added by David Turnbull variable to set memory for algorithms - public static int memory = 1024; - public static void main(String args[])throws Exception{ final int JOBAMOUNT = 1000; final int MEMORYSIZE = 10000;