Concurrency und Unit-Testing

blindmind

Mitglied
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:
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:
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:
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:
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....:rolleyes:

Bin für jede Anregung dankbar...und Danke im Voraus!
 
Kleine Frage am Rande: warum nimmst du nicht ne ArrayBlockingQueue, ConcurrentLinkedQueue oder so was ähnliches aus java.util.concurrent?
 
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..:\
 

Neue Beiträge

Zurück