Ekstragruppen 3. mai

På ekstragruppen i morgen skal vi se litt på sortering og mer og tråder.

/* Example usage of wait() and notify() */
 
class Storage {
    int number;
    boolean put = false;
 
    synchronized void get() {
        if (!put) {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("Fikk tak i " + number);
        put = false;
        notify();
    }
 
    synchronized void put(int k) {
        if (put) {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        number = k;
        System.out.println("Satte number til " + k);
        put = true;
        notify();
    }
}
 
class Putter implements Runnable {
    Storage s;
 
    public Putter(Storage s) {
        this.s = s;
        new Thread(this).start();
    }
 
    public void run() {
        for (int i = 0; i < 10; i++) {
            s.put(i);
        }
    }
}
 
class Getter implements Runnable {
    Storage s;
 
    public Getter(Storage s) {
        this.s = s;
        new Thread(this).start();
    }
 
    public void run() {
        for (int i = 0; i < 10; i++) {
            s.get();
        }
    }
}
 
class Example2 {
    public static void main(String[] args) {
 
        Storage s = new Storage();
 
        new Putter(s);
        new Getter(s);
 
    }
}

Løsningsforslag på oppgave:

import java.util.LinkedList;
 
class Buffer {
 
    private LinkedList bag = new LinkedList();
    private int totalNoArrays;
    private int sorted;
 
    public int size() {
        return totalNoArrays;
    }
 
    public synchronized int [] getArray() {
        return bag.poll();
    }
 
    /* Method called when initially adding arrays */
    public void add(int[] a) {
        bag.add(a);
    }
 
    /* Get ready for sorting */
    public void makeReady() {
        totalNoArrays = bag.size();
        sorted = 0;
    }
 
    /* Method called from the threads */
    public synchronized void addArray(int[] a) {
        try {
        Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
 
        bag.addLast(a);
        sorted++;
        notifyAll();
    }
 
    public synchronized boolean finished() {
        System.out.println("finished");
        if (totalNoArrays != sorted) {
 
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
 
        return totalNoArrays == sorted;
    }
 
    public void printArrays() {
        for (int[] a : bag) {
            for (int i = 0; i < a.length; i++ ) {
                if (i != a.length -1) System.out.print(a[i]+ " - ");
                else System.out.println(a[i] + "\n");
            }
        }
    }
}
 
class InsertionSort implements Runnable {
    private Buffer b;
    private int[] array;
 
    public InsertionSort(Buffer b) {
        this.b = b;
        new Thread(this).start();
    }
 
    public void run() {
        array =  b.getArray();
        array = sort(array);
        b.addArray(array);
    }
 
    private int[] sort(int[] a) {
        for (int j = 1; j < a.length; j++) {
            int key = a[j];
 
            int i = j - 1;
            while (i >= 0 && a[i] > key) {
                a[i+1] = a[i];
                i--;
            }
            a[i+1] = key;
        }
    }
 
}
class BubbleSort implements Runnable {
    private Buffer b;
    private int [] array;
 
    public BubbleSort(Buffer b) {
        this.b = b;
        new Thread(this).start();
    }
 
    public void run() {
        array = b.getArray();
        array = sort(array);
        b.addArray(array);
 
    }
 
    private boolean ok(int[] a) {
        boolean ok = true;
        for (int i = 0; i < a.length-1; i++) {
            if (a[i] > a[i+1]) ok = false;
        }
        return ok;
    }
 
    private int[] sort(int[] a) {
        boolean swapped;
 
        do {
            swapped = false;
            for (int i = 0; i < a.length-1; i++) {
                if (a[i] > a[i+1]) {
                    int tmp = a[i+1];
                    a[i+1] = a[i];
                    a[i] = tmp;
                    swapped = true;
                }
            }
        } while (swapped);
        return a;
    }
}
 
class PrintSolution implements Runnable {
    Buffer b;
 
    public PrintSolution(Buffer b) {
        this.b = b;
        new Thread(this).start();
    }
 
    public void run() {
        while (!b.finished()) continue;
 
        b.printArrays();
    }
}
 
class Exercise1 {
    public static void main(String[] args) {
        Buffer b =  fillBuffer(4, 10);
        b.makeReady();
 
        for (int i = 0; i < b.size(); i++) {
            //new BubbleSort(b);
            new InsertionSort(b);
        }
        new PrintSolution(b);
    }
 
    static Buffer fillBuffer(int noArrays, int size) {
        Buffer tmp = new Buffer();
        for (int i = 0; i < noArrays; i++) {
            int[] a = new int[size];
            for (int j = 0; j < size; j++) {
                a[j] = (int) (Math.random() * 100);
            }
 
            tmp.add(a);
        }
 
        return tmp;
    }
 
}
Print

Leave a Reply