Table of Contents

QueueTester

Getter class

package com.circularQueue;
 
public class Getter extends Thread
{
    private CircularConcurrentQueue<String> queue; 
    private String[] destArray; 
    private int from; 
    private int to; 
 
    public Getter( CircularConcurrentQueue<String> queue, String[] destArray, int from, int to )
    {
        this.queue = queue; 
        this.destArray = destArray; 
        this.from = from; 
        this.to = to;
    }
 
    @Override
    public void run()
    {
        try
        {
            Thread.sleep( (int)(Math.random() * 1000) );
 
            for( int nCounter = from; nCounter < to; nCounter++ )
            {
                String out = queue.next();
                destArray[nCounter] = out;
            } // for nCounter
 
        }
        catch( InterruptedException e )
        {
            e.printStackTrace();
        } // try part
 
    } // run 
 
}

Writer class

package com.circularQueue;
 
 
 
public class Putter extends Thread
{
    private CircularConcurrentQueue<String> queue; 
    private int from; 
    private int to; 
    private String[] idArray; 
 
    public Putter( CircularConcurrentQueue<String> queue, String[] idArray, int from, int to )
    {
        this.queue = queue; 
        this.idArray = idArray; 
        this.from = from; 
        this.to = to;
    }
 
    @Override
    public void run()
    {
        try
        {
            Thread.sleep( (int)(Math.random() * 1000) );
 
            // try to put in the queue all the elements from "from" to "to"
            for( int nCounter = from; nCounter < to; nCounter ++ )
            {
                queue.add( idArray[nCounter] );
            }
 
 
        }
        catch( InterruptedException e )
        {
            e.printStackTrace();
        } // try part
 
    } // run
 
}

Application

package com.Queue;
 
import java.util.UUID; 
 
public class App 
{
    private final int MAX_ID = 100000;
    private final int NUM_THREADS = MAX_ID / 1000;
    private String[] idListOrigin;
    private String[] idListDest;
    private CircularConcurrentQueue<String> queue = new ConcurrentQueue<String>();
 
    public void run()
    {
        System.out.print( "Fulfilling list of ID's..." );
        // feed the idList
        idListOrigin = new String[MAX_ID];
        idListDest = new String[MAX_ID];
 
        for( int idListCounter = 0; idListCounter < MAX_ID; idListCounter++ )
        {
            idListOrigin[idListCounter] = UUID.randomUUID().toString();
        } // for( idListCounter = 0
 
        System.out.println( "OK" );
 
 
        Putter[] allPutters = new Putter[NUM_THREADS];
        Getter[] allGetters = new Getter[NUM_THREADS];
 
        System.out.print( "Creating threads....");
        for( int idLauncher = 0; idLauncher < NUM_THREADS; idLauncher++ )
        {
            allPutters[idLauncher] = new Putter( queue, idListOrigin, idLauncher * 1000, ( idLauncher * 1000 ) + 1000 ); 
 
            allPutters[idLauncher].start();
 
            allGetters[idLauncher] = new Getter( queue, idListDest, idLauncher * 1000, ( idLauncher * 1000 ) + 1000 );
 
            allGetters[idLauncher].start();
        } // idLauncher = 0
 
 
        // wait until everything has finished
        for( int idLauncher = 0; idLauncher < NUM_THREADS; idLauncher++ )
        {
            try
            {
                allPutters[idLauncher].join();
                allGetters[idLauncher].join();                
            }catch( InterruptedException e )
            {}
        }
 
        System.out.print( "Check that the origin and destination data are correct....");
        for( int nContOrig = 0; nContOrig < MAX_ID; nContOrig++ )
        {
            // check that the element idListOrigin[nContOrig]
            // exists once and only once in the list idListDest
            int appears = 0;
            for( int nContDest = 0; nContDest < MAX_ID; nContDest++ )
            {
                if( idListOrigin[nContOrig].equals( idListDest[nContDest] ) )
                {
                    appears++;
                }
            } // nContDest
            if( appears != 1 )
            {
                System.out.println( "Element " + idListOrigin[nContOrig] + " appears " +  appears + "times");
            }
            if( nContOrig % 1000 == 0 )
            {
                System.out.println( "Element number: " + nContOrig );
            }
        } // nContOrig
 
    }
 
	/**
	 * @param args
	 */
	public static void main(String[] args) 
	{
		App app = new App();
 
		app.run();
 
		System.out.println( "Finished." );
	}
 
}