Advertisement
Jerkiller

scheduler

Apr 1st, 2015
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.13 KB | None | 0 0
  1. public class Scheduler{
  2.  
  3.     private int n_sched;
  4.     //un array di interi dove per ogni indice mi salvo quanto il thread con quell'id ha usato la cpu
  5.     private int[] time;
  6.  
  7.     public Scheduler(int threads){
  8.         n_sched = threads;
  9.         time = new int[n_sched];
  10.         for(int i=0;i<n_sched;i++)time[i]=0;
  11.     }
  12.    
  13.    
  14.     public synchronized void getCPU(int id_t, int time_t){
  15.         System.out.println("id "+id_t+" richiede cpu per un tempo "+time_t);
  16.        
  17.         //se non esiste, inserisco il valore time_t
  18.         //if(time[id_t]==0)time[id_t]+=time_t;
  19.         //questa riga non mi serve penso
  20.        
  21.         stampa(); //debug
  22.        
  23.         while(time[id_t]!=time_min())
  24.             try{ this.wait(); }
  25.             catch (InterruptedException e) { System.out.println("boh"); }
  26.            
  27.         //aggiorno valore
  28.         time[id_t]+=time_t;
  29.        
  30.     }
  31.    
  32.     public synchronized void releaseCPU(){
  33.         notifyAll();
  34.     }
  35.    
  36.     ///return id del thread che ha usato meno cpu time.
  37.     private int time_min(){
  38.         int min=time[0];
  39.         for(int i=0;i<n_sched;i++)
  40.             if(time[i]<min)
  41.                 min=time[i];
  42.        
  43.         return min;
  44.     }
  45.    
  46.     private void stampa(){
  47.         for(int i=0;i<n_sched;i++)System.out.print(time[i]+" ");
  48.         System.out.print("\n");
  49.     }
  50.    
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement