Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Scheduler{
- private int n_sched;
- //un array di interi dove per ogni indice mi salvo quanto il thread con quell'id ha usato la cpu
- private int[] time;
- public Scheduler(int threads){
- n_sched = threads;
- time = new int[n_sched];
- for(int i=0;i<n_sched;i++)time[i]=0;
- }
- public synchronized void getCPU(int id_t, int time_t){
- System.out.println("id "+id_t+" richiede cpu per un tempo "+time_t);
- //se non esiste, inserisco il valore time_t
- //if(time[id_t]==0)time[id_t]+=time_t;
- //questa riga non mi serve penso
- stampa(); //debug
- while(time[id_t]!=time_min())
- try{ this.wait(); }
- catch (InterruptedException e) { System.out.println("boh"); }
- //aggiorno valore
- time[id_t]+=time_t;
- }
- public synchronized void releaseCPU(){
- notifyAll();
- }
- ///return id del thread che ha usato meno cpu time.
- private int time_min(){
- int min=time[0];
- for(int i=0;i<n_sched;i++)
- if(time[i]<min)
- min=time[i];
- return min;
- }
- private void stampa(){
- for(int i=0;i<n_sched;i++)System.out.print(time[i]+" ");
- System.out.print("\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement