User Tools

Site Tools


java:queuetester

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
java:queuetester [2012/05/30 18:27]
rlunaro [Application]
java:queuetester [2014/12/24 10:42] (current)
Line 1: Line 1:
 +====== QueueTester ======
 +
 +===== Getter class =====
 +
 +<code java>
 +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 
 +
 +}
 +
 +</​code>​
 +
 +===== Writer class =====
 +
 +<code java>
 +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
 +
 +}
 +
 +</​code>​
 +
 +===== Application =====
 +
 +
 +<code java>
 +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."​ );
 + }
 +
 +}
 +
 +</​code>​
  
java/queuetester.txt ยท Last modified: 2014/12/24 10:42 (external edit)