Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.Scanner;
- import java.util.Set;
- import java.util.concurrent.ConcurrentHashMap;
- public class Strategia2 {
- int max;
- int size = 0;
- int p;
- int ilozapA = 0;
- int migracjaA = 0;
- int sA;
- int r;
- int ilozap = 0;
- int migracja = 0;
- int calka = 0;
- int s;
- ConcurrentHashMap<Integer, Integer> zlecenia = new ConcurrentHashMap<Integer, Integer>();
- ConcurrentHashMap<Integer, Integer> procobc = new ConcurrentHashMap<Integer, Integer>();
- ConcurrentHashMap<Integer, Integer> przypisane = new ConcurrentHashMap<Integer, Integer>();
- Scanner sc = new Scanner(System.in);
- public void Ska() throws IOException {
- WczytajDane();
- Procesy();
- System.out.println("Lista zleceń" + zlecenia);
- System.out.println("Procesory" + procobc);
- System.out.println("Przypisania " + przypisane);
- Set keys = zlecenia.keySet();
- Object x = 1;
- s = OdchŚred();
- System.out.println(max);
- do {
- for (Object o : keys) {
- if ((int) (Math.random() * 10) > 5) {
- boolean przydzielony = false;
- if (!przypisane.containsKey(o)) {
- do {
- ilozap++;
- int z = (int) (Math.random() * procobc.size());
- if (procobc.get(x) <= 100) {
- System.out
- .println("Procesowi "
- + o
- + " przydzielono miejsce na "
- + x
- + " Albowiem jest miejsce");
- procobc.put((int) z, procobc.get(z)
- + (int) zlecenia.get(o));
- przypisane.put((Integer) o, z);
- przydzielony = true;
- calka++;
- }
- if (procobc.get(x) >= p) {
- migracja++;
- System.out.println("Procesowi " + o
- + " przydzielono miejsce na " + z);
- procobc.put((int) z, procobc.get(z)
- + (int) zlecenia.get(o));
- przypisane.put((Integer) o, z);
- przydzielony = true;
- calka++;
- }
- if (Przeciążone()){
- break;
- }
- } while (przydzielony == false);
- }
- }
- if ((int) o <= calka && przypisane.get(o) != null) {
- System.out.println("Proces " + o + " Dotarł do końca ");
- int a = procobc.get(przypisane.get(o)) - zlecenia.get(o);
- procobc.put(przypisane.get(o), a);
- if (!przypisane.contains(o)) {
- procobc.put(przypisane.get(o), 0);
- }
- zlecenia.remove(o);
- przypisane.remove(o);
- }
- System.out.println("Lista zleceń" + zlecenia);
- System.out.println("Procesory" + procobc);
- System.out.println("Przypisania " + przypisane);
- }
- calka++;
- if (zlecenia.isEmpty()) {
- break;
- }
- } while (ToJużJestKoniec() == false);
- WczytajDD();
- System.out.println("Podsumowanie : zapytania o obciążenie " + ilozap
- + " (zmiana " + (ilozapA - ilozap) + " względem A)");
- System.out.println("Ilość migracji " + migracja
- + (" zmiana " + (migracjaA - migracja) + " względem A")
- + " a średnie obciążenie procesora to " + s
- + (" zmiana " + (sA - s + " względem A")));
- }
- public int ZnajdźKlucz(int z){
- Set keys = przypisane.keySet();
- for (Object o:keys){
- if (przypisane.get(o) == z){
- return (int) o;
- }
- }
- return 1;
- }
- public boolean ToJużJestKoniec() {
- Set keys = zlecenia.keySet();
- for (Object o : keys) {
- if (zlecenia.get(o) > 0) {
- return false;
- }
- }
- return true;
- }
- public void Procesy() {
- for (int z = 0; z <= size; z++) {
- procobc.put(z, 0);
- }
- }
- public boolean Przeciążone(){
- Set keys = procobc.keySet();
- for (Object o : keys) {
- if (procobc.get(o) < max) {
- return false;
- }
- }
- calka++;
- return true;
- }
- public int DajZapytania(){
- return ilozap;
- }
- public int DajMigracje(){
- return migracja;
- }
- public int DajSrednia(){
- return s;
- }
- public void WczytajDane() throws IOException {
- FileReader a = new FileReader("C:/Users/Jarema/Desktop/PWR/angie.txt");
- BufferedReader abba = new BufferedReader(a);
- String part = abba.readLine();
- while (!part.equals("Koniec")) {
- String[] splitter = part.split(";");
- int c = Integer.valueOf(splitter[0]);
- int b = Integer.valueOf(splitter[1]);
- int size = Integer.valueOf(splitter[2]);
- p = Integer.valueOf(splitter[3]);
- max = Integer.valueOf(splitter[4]);
- r = Integer.valueOf(splitter[5]);
- this.size = size;
- zlecenia.put(c, b);
- part = abba.readLine();
- }
- abba.close();
- System.out.println(zlecenia);
- }
- public void WczytajDD() throws IOException {
- FileReader a = new FileReader("C:/Users/Jarema/Desktop/PWR/Dane.txt");
- BufferedReader abba = new BufferedReader(a);
- String part = abba.readLine();
- String[] splitter = part.split(";");
- ilozapA = Integer.valueOf(splitter[0]);
- migracjaA = Integer.valueOf(splitter[1]);
- sA = Integer.valueOf(splitter[2]);
- abba.close();
- }
- public int OdchŚred() {
- return (Obciazenie() / procobc.size());
- }
- public int Obciazenie() {
- Set keys = zlecenia.keySet();
- int suma = 0;
- for (Object o : keys) {
- suma += zlecenia.get(o);
- }
- return suma;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement