java:queuetester
                Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| java:queuetester [2012/05/30 16:26] – creado rlunaro | java:queuetester [2022/12/02 21:02] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== QueueTester ====== | ||
| + | |||
| + | ===== Getter class ===== | ||
| + | |||
| + | <code java> | ||
| + | package com.circularQueue; | ||
| + | |||
| + | public class Getter extends Thread | ||
| + | { | ||
| + |     private CircularConcurrentQueue< | ||
| + |     private String[] destArray;  | ||
| + | private int from; | ||
| + | private int to; | ||
| + |      | ||
| + |     public Getter( CircularConcurrentQueue< | ||
| + | { | ||
| + |         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 ===== | ||
| + | |||
| + | <code java> | ||
| + | package com.circularQueue; | ||
| + | |||
| + | |||
| + | |||
| + | public class Putter extends Thread | ||
| + | { | ||
| + |     private CircularConcurrentQueue< | ||
| + | private int from; | ||
| + | private int to; | ||
| + |     private String[] idArray;  | ||
| + |      | ||
| + |     public Putter( CircularConcurrentQueue< | ||
| + | { | ||
| + |         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 " | ||
| + | for( int nCounter = from; nCounter < to; nCounter ++ ) | ||
| + | { | ||
| + | queue.add( idArray[nCounter] ); | ||
| + | } | ||
| + |              | ||
| + |              | ||
| + | } | ||
| + | catch( InterruptedException e ) | ||
| + | { | ||
| + |             e.printStackTrace(); | ||
| + | } // try part | ||
| + |          | ||
| + | } // run | ||
| + | |||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | ===== 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< | ||
| + |      | ||
| + | public void run() | ||
| + | { | ||
| + |         System.out.print( " | ||
| + | // 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( " | ||
| + |          | ||
| + |          | ||
| + |         Putter[] allPutters = new Putter[NUM_THREADS]; | ||
| + |         Getter[] allGetters = new Getter[NUM_THREADS]; | ||
| + |          | ||
| + |         System.out.print( " | ||
| + |         for( int idLauncher = 0; idLauncher < NUM_THREADS; | ||
| + | { | ||
| + |             allPutters[idLauncher] = new Putter( queue, idListOrigin, | ||
| + |              | ||
| + |             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; | ||
| + | { | ||
| + | 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( " | ||
| + | } | ||
| + | if( nContOrig % 1000 == 0 ) | ||
| + | { | ||
| + |                 System.out.println( " | ||
| + | } | ||
| + | } // nContOrig | ||
| + |          | ||
| + | } | ||
| + |      | ||
| + | /** | ||
| + | * @param args | ||
| + | */ | ||
| + | public static void main(String[] args) | ||
| + | { | ||
| + | App app = new App(); | ||
| + | |||
| + | 		app.run(); | ||
| + | |||
| + | 		System.out.println( " | ||
| + | } | ||
| + | |||
| + | } | ||
| + | |||
| + | </ | ||
