tutorials.de Buch-Aktion 05/2012
ERLEDIGT
NEIN
ANTWORTEN
2
ZUGRIFFE
1049
EMPFEHLEN
  • An Twitter übertragen
  • An Facebook übertragen
AUF DIESES THEMA
ANTWORTEN
  1. #1
    blindmind blindmind ist offline Mitglied
    Registriert seit
    Apr 2006
    Beiträge
    17
    Hallo zusammen!

    Kurze Frage: Ich habe eine selbst implementierte Datenstruktur FIFO-Queue, welche Objekte jeglicher Art aufnehmen und nach dem FIFO-Prinzip ausgeben kann. Diese enthält zur Sicherstellung der Thread-Sicherheit synchronisierte Methoden dafür.

    Hier erstmal der Code:

    FFIFOQueue.java

    Code :
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    
    public class FIFOQueue
    {
        private Object[] container;             //Storage for the elements in the queue
        private int numElements = 0;            //Number of elements in the queue
        private int readIndex = 0;              //First element in the queue
        private int writeIndex = 0;             //Next available index in the storage
        
        
        public FIFOQueue(final int capacity)
        {
            container = new Object[capacity];
        }
        
        
        public synchronized void put(Object element) {
            while(numElements == container.length) {
                try {
                    System.out.println("----QUEUE is full! " + Thread.currentThread().getName() + " has to wait.");
                    wait();
                }
                catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
            container[writeIndex] = element;
            System.out.println("----Put: " + element.toString());
            writeIndex = (writeIndex + 1) % container.length;
            numElements++;
            notify();
        }
     
     
        public synchronized Object get() {
            while(numElements == 0) {
                try {
                    System.out.println("----QUEUE is empty! " + Thread.currentThread().getName() + " has to wait!");
                    wait();
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
            final Object element = container[readIndex];
            container[readIndex] = null;
            readIndex = (readIndex + 1) % container.length;
            numElements--;
            notify();
            System.out.println("----Get: " + element.toString());
            return element;
        }
    }

    Folgende Threads sollen nebenläufig lesen bzw. schreiben.

    ReaderThread.java:

    Code :
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    
    public class ReaderThread extends Thread
    {
        private FIFOQueue queue;
        
        public ReaderThread(final FIFOQueue queue, final String name)
        {
            super(name);
            this.queue = queue;
        }
        
        public void run() {
            for(int i=0; i<=10; i++) {
                queue.get();
                try {
                    Thread.sleep(4000); //Just for demonstration purposes
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("Reader-Thread finished!");
        }
    }

    WriterThread.java

    Code :
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    
    public class WriterThread extends Thread
    {
        private FIFOQueue queue;
        private String[] data = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"};
        
        public WriterThread(final FIFOQueue queue, final String name)
        {
            super(name);
            this.queue = queue;
        }
        
        public void run() {
            for(int i=0; i<data.length; i++) {
                queue.put(data[i]);
            }
            System.out.println("Writer-Thread finished!");
        }
    }

    QueueApplication.java

    Code :
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    
    public class QueueApplication {
     
        public static void main(String[] args) {
            FIFOQueue queue = new FIFOQueue(5);
            Thread reader = new ReaderThread(queue, "Reader-Thread");
            Thread writer = new WriterThread(queue, "Writer-Thread");
            reader.start();
            writer.start();
            try {
                reader.join();
                writer.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            
        }
    }

    Meine Frage ist nun, wie ich nun durch Unit-Tests sicherstellen kann, das die Syncronisation wirklich korrekt ausgeführt wird...Die Ausgaben via System.out sind schon korrekt, allerdings ist das kein akzeptabler Test....

    Bin für jede Anregung dankbar...und Danke im Voraus!
     

  2. #2
    genodeftest genodeftest ist offline Mitglied Brillant
    Registriert seit
    Jun 2009
    Beiträge
    868
    Kleine Frage am Rande: warum nimmst du nicht ne ArrayBlockingQueue, ConcurrentLinkedQueue oder so was ähnliches aus java.util.concurrent?
     
    Code bitte so einfügen: [java]System.out.println("Hallo");[/java] (Analog für andere Programmiersprachen)
    Code java:
    1
    
    System.out.println("Hallo");
    hilfreich zu Java: Really Big Index, Java ist auch eine Insel Band 1 und Band 2.
    ___________
    Ubuntu Bug #1: Microsoft has a majority market share
    Casecon: Projekt leiser Käse

  3. #3
    blindmind blindmind ist offline Mitglied
    Registriert seit
    Apr 2006
    Beiträge
    17
    Danke für den Tip!

    Ja, die Klassen aus java.util.concurrent wären natürlich sinnvoller, aber leider nicht nicht die Aufgabenstellung...Daher hab ich es "von Hand" geschrieben.

    Jetzt weiß ich halt nur nicht, wie ich die Unit-Tests machen soll, um die Synchronisation zu testen..:\
     

Ähnliche Themen

  1. Ein paar Fragen zu Unit-Testing
    Von sepan im Forum Coders Talk
    Antworten: 2
    Letzter Beitrag: 18.01.08, 19:08
  2. PHPUnit 3 - Unit Testing
    Von kela_root im Forum PHP
    Antworten: 0
    Letzter Beitrag: 04.12.07, 17:45
  3. Beispiel zu Unit Testing mit NUnit, Spring.Net und ADO .Net
    Von Konstantin Denerz im Forum .NET Café
    Antworten: 1
    Letzter Beitrag: 12.07.07, 16:31
  4. GUI Unit Testing in C#
    Von dreadread im Forum .NET Archiv
    Antworten: 0
    Letzter Beitrag: 25.02.04, 09:15