001    //package edu.unima.ky.parallel;
002    
003    /** 
004     * Buchung.
005     * Multi threaded finance testing application for the "Bounded Buffer"
006     * generates random transfer amount within desired range.
007     * @author Heinz Kredel. 
008     */ 
009    public class Buchung {
010    
011    /**
012     * Main method.
013     * @param args
014     * @throws InterruptedException
015     */
016        public static void main(String[] args) throws InterruptedException {
017    
018            int anzahl = 100; // number of iterations
019            int kanzahl = 10; // number of accounts
020            BoundedBuffer buffer = new BoundedBuffer(10); // Buffers are generated here.
021            KontoA[] konten = new KontoA[kanzahl];  
022            double balance = 0.0;
023            double stand;
024            for (int i = 0; i < konten.length; i++ ) {
025                konten[i] = new KontoA( 0.0 );
026                stand = konten[i].zeigeGeld();
027                balance += stand;
028                System.out.println("Konto " + i 
029                                + " Stand " + stand);
030            }
031            System.out.println("Balance " + balance); 
032            
033    
034            Thread t1 = new BuchungInput(anzahl,buffer,konten);
035    /*
036     * Thread t2 gets forth size of iterations.
037     * Thread t3 and thread t4 gets each a half.
038     */     
039            Thread t2 = new BuchungDo(anzahl/2,buffer,"1");
040            Thread t3 = new BuchungDo(anzahl/4,buffer,"2");
041            Thread t4 = new BuchungDo(anzahl/4,buffer,"3");
042         
043            t1.start(); t2.start(); t3.start(); t4.start();
044            t1.join(); t2.join(); t3.join(); t4.join();
045    
046            balance = 0.0;
047            for (int i = 0; i < konten.length; i++ ) {
048                stand = konten[i].zeigeGeld();
049                balance += stand;
050                System.out.println("Konto " + i 
051                                + " Stand " + stand);
052            }
053            System.out.println("Balance " + balance); 
054    
055        }
056    
057    }
058    
059    /** 
060     * Generates input data for Buchung.
061     * Uses an array KontoA[] "konten", int "anzahl" as 
062     * number of accounts, "kmax" as konten.length and bmax as range 
063     * of amount to transfer.
064     */
065    class BuchungInput extends Thread {
066        private KontoA[] konten; 
067        private int anzahl, kmax;
068        private double bmax;
069        private BoundedBuffer buffer;
070    
071    /**
072     * Generates accouting data.
073     * @param a size of the buffer.
074     * @param b BoundedBuffer.
075     * @param k KontoA. 
076     */
077        BuchungInput(int a, BoundedBuffer b, KontoA[] k) {
078            anzahl = a;
079            buffer = b;
080            konten = k;
081            kmax = konten.length;
082            bmax = 100000.0;
083        }
084    
085    /**
086     * Run in parallel.
087     */
088        public void run() {
089            for (int i = 0; i < anzahl; i++) {
090                int a = (int) ( Math.random() * kmax ); 
091                int b = (int) ( Math.random() * kmax ); 
092                double betrag = (double) ( Math.random() * bmax ); 
093                Ueberweisung u = new Ueberweisung( konten[a], konten[b], betrag );
094                System.out.print("+");
095                //try {
096                     buffer.put( (Object)u );
097                //} catch (InterruptedException e) { }
098            }
099            System.out.println();
100        }
101    
102    }
103    
104    /**
105     * Process the Buchung data.
106     * Uses a buffer ("BoundedBuffer"), Int anzahl and String me.
107     */
108    class BuchungDo extends Thread {
109        private int anzahl;
110        private BoundedBuffer buffer;
111        private String me = "-";
112    
113    /**
114     * This constructor generates int "anzahl" (number of accounts),
115     * a BoundedBuffer and String "m".
116     * @param a number of transactions.
117     * @param b BoundedBuffer.
118     * @param m String.
119     */
120        BuchungDo(int a, BoundedBuffer b, String m) {
121            me = m;
122            anzahl = a;
123            buffer = b;
124        }
125    
126        public void run() {
127            for (int i = 0; i < anzahl; i++) {
128                //try {
129                    Ueberweisung u = (Ueberweisung) buffer.get();
130                    System.out.print(me);
131                    u.run();
132                //} catch (InterruptedException e) { }
133            }
134            System.out.println();
135        }
136    
137    }