//Titel:            Dialog-Fenster
//Version:
//Copyright:		Copyright (c) 2010
//Autor:
//Organisation:
//Beschreibung:


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;


public class summen_thread_test2 extends JFrame {

  private final int MAX=10;

  private JButton bnTest1 = new JButton("Test1 "+MAX+"*Summe");
  private JButton bnTest2 = new JButton("Test2 "+MAX+"*Summe");
  private JButton bnTest3 = new JButton("Test3 "+MAX);
  private JButton bnTest4 = new JButton("Test4 fastest");
  private JButton bnEsc = new JButton("Abbruch");


  private JTextArea memo;

  private long summe;


  public summen_thread_test2() {
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    setGUI();
  }

  private void setGUI() {
    setSize(800, 430);
    setTitle("JFrame als Dialog");

    bnTest1.setFont( new Font("Arial", Font.BOLD,18)  );
    bnTest2.setFont( new Font("Arial", Font.BOLD,18)  );
    bnTest3.setFont( new Font("Arial", Font.BOLD,18)  );
    bnTest4.setFont( new Font("Arial", Font.BOLD,18)  );
    bnEsc.setFont( new Font("Arial", Font.BOLD,18)  );
   

    this.getContentPane().setLayout( new BorderLayout() );
    memo = new JTextArea();
    memo.setFont( new Font("Arial", Font.BOLD,24)  );
    this.getContentPane().add( new JScrollPane(memo), BorderLayout.CENTER);

	JPanel pnButton = new JPanel();
	//pnButton.setBackground(Color.red);
	pnButton.setLayout( new FlowLayout() );
	pnButton.add(bnTest1);
	pnButton.add(bnTest2);
         pnButton.add(bnTest3);
         pnButton.add(bnTest4);
	pnButton.add(bnEsc);

    this.getContentPane().add( pnButton, BorderLayout.SOUTH );

          bnTest1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
              BnTest1_Click();
            }
          });
          bnTest2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
              BnTest2_Click();
            }
          });
          bnTest3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
              BnTest3_Click();
            }
          });
          bnTest4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
              BnTest4_Click();
            }
          });


          bnEsc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
              BnEsc_Click();
            }
          });


  } // setGUI

    private void BnTest1_Click() {
      long t1, t2;
      summe=0;
      System.out.println("Test1");
      myThread1[] ths = new myThread1[MAX];
      for (int i=0; i<ths.length; i++) {
        ths[i] = new myThread1(i+1,  1000000);
      }
      t1 =  System.currentTimeMillis();
      for (int i=0; i<ths.length; i++) {
        ths[i].start();
      }
      try {
          for (int i=0; i<ths.length; i++) {
             ths[i].join();
          }
      }
      catch (InterruptedException e) {
      }
      t2 =  System.currentTimeMillis();
      memo.setText("Summe: "+Long.toString(summe)+"     Soll: "+(1000000*ths.length)+"\n\r"+  "Zeit: "+Long.toString(t2-t1) );
    }  // Test1


  private void BnTest2_Click() {
      long t1, t2;
      summe=0;
      System.out.println("Test2");
      myThread2[] ths = new myThread2[MAX];
      for (int i=0; i<ths.length; i++) {
        ths[i] = new myThread2(i+1,  1000000);
      }
      t1 =  System.currentTimeMillis();
      for (int i=0; i<ths.length; i++) {
        ths[i].start();
      }
      try {
          for (int i=0; i<ths.length; i++) {
             ths[i].join();
          }
      }
      catch (InterruptedException e) {
      }
      t2 =  System.currentTimeMillis();
      memo.setText("Summe: "+Long.toString(summe)+"     Soll: "+(1000000*ths.length)+"\n\r"+ "Zeit: "+Long.toString(t2-t1) );
    }  // Test2


    private void BnTest3_Click() {
      long t1, t2;
      summe=0;
      System.out.println("Test3");
      myThread3[] ths = new myThread3[MAX];
      for (int i=0; i<ths.length; i++) {
        ths[i] = new myThread3(i+1,1000000);
      }
      t1 =  System.currentTimeMillis();
      for (int i=0; i<ths.length; i++) {
        ths[i].start();
      }
      try {
         for (int i=0; i<ths.length; i++) {
            ths[i].join();
         }
      }
      catch (InterruptedException e) {
      }
      t2 =  System.currentTimeMillis();
      memo.setText("Summe: "+Long.toString(summe)+"     Soll: "+(1000000*ths.length)+"\n\r"+  "Zeit: "+Long.toString(t2-t1) );
    }  // Test3


     // fastest
    private void BnTest4_Click() {
      long t1, t2;
      summe=0;
      long n = 1000000*MAX; 
      t1 =  System.currentTimeMillis();
      for (int i=1; i<=n; i++) {
         addNr4(1); // _nr;
       }        //setVisible(false);
      t2 =  System.currentTimeMillis();
      memo.setText("Summe: "+Long.toString(summe)+"     Soll: "+(1000000*10)+"\n\r"+  "Zeit: "+Long.toString(t2-t1) );
     }
    
    private void BnEsc_Click() {
      setVisible(false);
      System.exit(0);
    }

    private  void addNr1(int nr) {
       summe+=nr;
    }

    private synchronized void addNr2(int nr) {
       summe+=nr;
    }

    private synchronized void addNr3(int nr) {
       summe+=nr;
    }


    private  void addNr4(int nr) {
       summe+=nr;
    }

  public static void main(String[] args) {
    summen_thread_test2 frame = new summen_thread_test2();
    frame.setVisible(true);
 }

   class myThread1 extends Thread {
     int _n;
     int _nr;
   
     public myThread1 (int nr, int n) {
        _n = n;
        _nr = nr;
     }
   
   
      // Thread Methode
     public void run() {
       for (int i=1; i<=_n; i++) {
         addNr1(1); // _nr;
       }
     }
   
   
   }  // myThread1


 class myThread2 extends Thread {
     int _n;
     int _nr;
   
     public myThread2 (int nr, int n) {
        _n = n;
        _nr = nr;
     }
   
      // Thread Methode
     public void run() {
       for (int i=1; i<=_n; i++) {
         addNr2(1); // _nr;
       }
     }
   
   }  // myThread2


   class myThread3 extends Thread {
     int _n;
     int _nr;
   
     public myThread3 (int nr, int n) {
        _n = n;
        _nr = nr;
     }
   
   
      // Thread Methode
     public void run() {
       int summe=0;
       for (int i=1; i<=_n; i++) {
         summe+=1; // _nr;
       }
       addNr3(summe);
     }
   
   
   }  // myThread3

   
   

}  // summen_thread_test1






// synchronized
   /*
         try {
           Thread.sleep( (int) (Math.random()*150) );
         }
         catch (InterruptedException e) {
         }
   */
