Advertisement
Guest User

Untitled

a guest
May 30th, 2015
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.07 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.FileReader;
  3. import java.io.IOException;
  4. import java.util.Scanner;
  5. import java.util.Set;
  6. import java.util.concurrent.ConcurrentHashMap;
  7.  
  8. public class Strategia2 {
  9.     int max;
  10.     int size = 0;
  11.     int p;
  12.     int ilozapA = 0;
  13.     int migracjaA = 0;
  14.     int sA;
  15.     int r;
  16.     int ilozap = 0;
  17.     int migracja = 0;
  18.     int calka = 0;
  19.     int s;
  20.     ConcurrentHashMap<Integer, Integer> zlecenia = new ConcurrentHashMap<Integer, Integer>();
  21.     ConcurrentHashMap<Integer, Integer> procobc = new ConcurrentHashMap<Integer, Integer>();
  22.     ConcurrentHashMap<Integer, Integer> przypisane = new ConcurrentHashMap<Integer, Integer>();
  23.     Scanner sc = new Scanner(System.in);
  24.  
  25.     public void Ska() throws IOException {
  26.         WczytajDane();
  27.         Procesy();
  28.         System.out.println("Lista zleceń" + zlecenia);
  29.         System.out.println("Procesory" + procobc);
  30.         System.out.println("Przypisania " + przypisane);
  31.         Set keys = zlecenia.keySet();
  32.         Object x = 1;
  33.         s = OdchŚred();
  34.         System.out.println(max);
  35.         do {
  36.             for (Object o : keys) {
  37.                 if ((int) (Math.random() * 10) > 5) {
  38.                     boolean przydzielony = false;
  39.                     if (!przypisane.containsKey(o)) {
  40.                         do {
  41.                             ilozap++;
  42.                             int z = (int) (Math.random() * procobc.size());
  43.                             if (procobc.get(x) <= 100) {
  44.                                 System.out
  45.                                         .println("Procesowi "
  46.                                                 + o
  47.                                                 + " przydzielono miejsce na "
  48.                                                 + x
  49.                                                 + " Albowiem jest miejsce");
  50.                                 procobc.put((int) z, procobc.get(z)
  51.                                         + (int) zlecenia.get(o));
  52.                                 przypisane.put((Integer) o, z);
  53.                                 przydzielony = true;
  54.                                 calka++;
  55.                             }
  56.                             if (procobc.get(x) >= p) {
  57.                                 migracja++;
  58.                                 System.out.println("Procesowi " + o
  59.                                         + " przydzielono miejsce na " + z);
  60.                                 procobc.put((int) z, procobc.get(z)
  61.                                         + (int) zlecenia.get(o));
  62.                                 przypisane.put((Integer) o, z);
  63.                                 przydzielony = true;
  64.                                 calka++;
  65.                             }
  66.                             if (Przeciążone()){
  67.                                 break;
  68.                             }
  69.                            
  70.                         } while (przydzielony == false);
  71.                     }
  72.  
  73.                 }
  74.  
  75.                 if ((int) o <= calka && przypisane.get(o) != null) {
  76.                     System.out.println("Proces " + o + " Dotarł do końca ");
  77.                     int a = procobc.get(przypisane.get(o)) - zlecenia.get(o);
  78.                     procobc.put(przypisane.get(o), a);
  79.                     if (!przypisane.contains(o)) {
  80.                         procobc.put(przypisane.get(o), 0);
  81.                     }
  82.                     zlecenia.remove(o);
  83.                     przypisane.remove(o);
  84.                 }
  85.  
  86.                 System.out.println("Lista zleceń" + zlecenia);
  87.                 System.out.println("Procesory" + procobc);
  88.                 System.out.println("Przypisania " + przypisane);
  89.             }
  90.             calka++;
  91.             if (zlecenia.isEmpty()) {
  92.                 break;
  93.             }
  94.         } while (ToJużJestKoniec() == false);
  95.         WczytajDD();
  96.         System.out.println("Podsumowanie : zapytania o obciążenie " + ilozap
  97.                 + " (zmiana " + (ilozapA - ilozap) + " względem A)");
  98.         System.out.println("Ilość migracji " + migracja
  99.                 + (" zmiana " + (migracjaA - migracja) + " względem A")
  100.                 + " a średnie obciążenie procesora to  " + s
  101.                 + (" zmiana " + (sA - s + " względem A")));
  102.     }
  103.    
  104.     public int ZnajdźKlucz(int z){
  105.         Set keys = przypisane.keySet();
  106.         for (Object o:keys){
  107.             if (przypisane.get(o) == z){
  108.                 return (int) o;
  109.             }
  110.         }
  111.         return 1;
  112.     }
  113.     public boolean ToJużJestKoniec() {
  114.         Set keys = zlecenia.keySet();
  115.         for (Object o : keys) {
  116.             if (zlecenia.get(o) > 0) {
  117.                 return false;
  118.             }
  119.         }
  120.         return true;
  121.     }
  122.  
  123.     public void Procesy() {
  124.         for (int z = 0; z <= size; z++) {
  125.             procobc.put(z, 0);
  126.         }
  127.     }
  128.     public boolean Przeciążone(){
  129.         Set keys = procobc.keySet();
  130.         for (Object o : keys) {
  131.             if (procobc.get(o) < max) {
  132.                 return false;
  133.             }
  134.         }
  135.         calka++;
  136.         return true;
  137.     }
  138.     public int DajZapytania(){
  139.         return ilozap;
  140.     }
  141.     public int DajMigracje(){
  142.         return migracja;
  143.     }
  144.     public int DajSrednia(){
  145.         return s;
  146.     }
  147.  
  148.     public void WczytajDane() throws IOException {
  149.         FileReader a = new FileReader("C:/Users/Jarema/Desktop/PWR/angie.txt");
  150.         BufferedReader abba = new BufferedReader(a);
  151.         String part = abba.readLine();
  152.         while (!part.equals("Koniec")) {
  153.             String[] splitter = part.split(";");
  154.             int c = Integer.valueOf(splitter[0]);
  155.             int b = Integer.valueOf(splitter[1]);
  156.             int size = Integer.valueOf(splitter[2]);
  157.             p = Integer.valueOf(splitter[3]);
  158.             max = Integer.valueOf(splitter[4]);
  159.             r = Integer.valueOf(splitter[5]);
  160.             this.size = size;
  161.             zlecenia.put(c, b);
  162.             part = abba.readLine();
  163.         }
  164.         abba.close();
  165.         System.out.println(zlecenia);
  166.     }
  167.  
  168.     public void WczytajDD() throws IOException {
  169.         FileReader a = new FileReader("C:/Users/Jarema/Desktop/PWR/Dane.txt");
  170.         BufferedReader abba = new BufferedReader(a);
  171.         String part = abba.readLine();
  172.         String[] splitter = part.split(";");
  173.         ilozapA = Integer.valueOf(splitter[0]);
  174.         migracjaA = Integer.valueOf(splitter[1]);
  175.         sA = Integer.valueOf(splitter[2]);
  176.         abba.close();
  177.     }
  178.  
  179.     public int OdchŚred() {
  180.         return (Obciazenie() / procobc.size());
  181.     }
  182.  
  183.     public int Obciazenie() {
  184.         Set keys = zlecenia.keySet();
  185.         int suma = 0;
  186.         for (Object o : keys) {
  187.             suma += zlecenia.get(o);
  188.         }
  189.         return suma;
  190.     }
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement