
//Titel:        Layout
//Version:
//Copyright:    Copyright (c) 1999
//Autor:       Ihr Name
//Organisation:      Ihre Organisation
//Beschreibung:  Ihre Beschreibung


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

import java.awt.event.*;

public class Test_Thread_cpu extends JFrame {

	final long MAX1 = 2000000000;
	final long MAX  = 10000;

  JLabel jLabel1 = new JLabel("Anz Threads");
  JSpinner spAnzThreads;


  JLabel jLabel2 = new JLabel("Ergebnisse");
  JTextArea TErg = new JTextArea("");

  JButton BnStart1 = new JButton("Start 1");
  JButton BnStart2 = new JButton("Start 2");
  JButton BnStart3 = new JButton("Start 3");
  JButton BnEsc = new JButton("Abbruch");


  public Test_Thread_cpu() {
    setSize(800, 450);
    setTitle("Test der CPU mit Threads");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setGUI();
	 TErg.setText("PI: "+Math.PI+"\r\n" );
  }

  private void setGUI() {
    this.getContentPane().setLayout( new GridBagLayout() );

		// Aktueller Wert, Min, Max, Increment
    spAnzThreads = new JSpinner( new SpinnerNumberModel(4,1, 100, 1)   );


    this.getContentPane().add(jLabel1,
        new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
            ,GridBagConstraints.NORTH,
            GridBagConstraints.NONE,
            new Insets(10,20,30,20), 0, 0));

    this.getContentPane().add(spAnzThreads,
        new GridBagConstraints(1, 0, 1, 1, 100.0, 0.0
            ,GridBagConstraints.NORTH,
            GridBagConstraints.HORIZONTAL,
            new Insets(10,20,30,20), 0, 0));


    this.getContentPane().add(jLabel2,
        new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
            ,GridBagConstraints.NORTH,
            GridBagConstraints.NONE,
            new Insets(0,20,0,0), 0, 0));

    this.getContentPane().add(new JScrollPane(TErg),
        new GridBagConstraints(0, 2, 2, 1, 100.0, 100.0
            ,GridBagConstraints.EAST,
            GridBagConstraints.BOTH,
            new Insets(0,20,0,20), 0, 0));


   // --------------------------
            // Schalter
     JPanel panelBn = new JPanel();
     panelBn.setLayout( new FlowLayout() );
     //panelBn.setBackground(Color.green);

    this.getContentPane().add(panelBn,
        new GridBagConstraints(0, 3, 2, 1, 100.0, 0.0
            ,GridBagConstraints.CENTER,
            GridBagConstraints.HORIZONTAL,
            new Insets(40,20,20,20), 0, 0));


    panelBn.add(BnStart1);
    panelBn.add(BnStart2);
    panelBn.add(BnStart3);
    panelBn.add(BnEsc);

	spAnzThreads.setFont( new Font("Arial", Font.BOLD,18)  );
	jLabel1.setFont( new Font("Arial", Font.BOLD,18)  );
	jLabel2.setFont( new Font("Arial", Font.BOLD,18)  );
	TErg.setFont( new Font("Arial", Font.BOLD,18)  );
	BnStart1.setFont( new Font("Arial", Font.BOLD,18)  );
	BnStart2.setFont( new Font("Arial", Font.BOLD,18)  );
	BnStart3.setFont( new Font("Arial", Font.BOLD,18)  );
	BnEsc.setFont( new Font("Arial", Font.BOLD,18)  );

   BnStart1.addActionListener(new java.awt.event.ActionListener() {

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

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

      public void actionPerformed(ActionEvent e) {
        BnStart3_Clicked();
      }
    });

    BnEsc.addActionListener(new java.awt.event.ActionListener() {

      public void actionPerformed(ActionEvent e) {
        BnEsc_Clicked();
      }
    });


  } // setGUI


	void BnStart2_Clicked() {
		long t1,t2;
		float pi, summe;
		double delta;
		float h, x, y;
		t1 =  System.currentTimeMillis();
		summe=0.0f;
		h = 1.0f/MAX;
		x=0.0f;  // Mitte
      for (int i=0; i<MAX; i++) {
			y = (float) (1.0 / (1+x*x));
			summe += y;
			x+=h;
			if (x > 1.0) System.out.println("x: "+x);
      }
		pi= (float) (summe*4.0*h);
		t2 =  System.currentTimeMillis();
		delta = (pi-Math.PI)*100.0;
		TErg.append("Zeit2: "+(t2-t1)+"   "+pi+"  delta: "+delta+"\r\n" );
  }
	void BnStart2a_Clicked() {
		long t1,t2;
		double summe;
		double h, x, y, pi, delta;
		t1 =  System.currentTimeMillis();
		summe=0.0;
		h = 1.0/MAX;
		x=1.0; 
      for (int i=0; i<MAX; i++) {
			y = 1.0 / (1+x*x);
			summe += y;
			x-=h;
			if (x < 0.0) 
				System.out.println("x: "+x);
      }
		pi=summe*4.0*h;
		t2 =  System.currentTimeMillis();
		delta = (pi-Math.PI)*100.0;
		TErg.append("Zeit Single: "+(t2-t1)+"\t"+pi+"  delta: "+delta+"\r\n" );

  }

	void BnStart1_Clicked() {
		long t1,t2;
		double summe, pi, delta;
		double h, x, y;
		t1 =  System.currentTimeMillis();
		summe=0.0;
		h = 1.0/MAX;
		x=0.0;  // Mitte
      for (int i=0; i<MAX; i++) {
			y = 1.0 / (1+x*x);
			summe += y;
			// summe=summe+i+1;
			x+=h;
			// if (x > 1.0) System.out.println("x: "+x);
      }
		pi=summe*4.0*h;
		t2 =  System.currentTimeMillis();
		delta = (pi-Math.PI)*100.0;
		TErg.append("Zeit1: "+(t2-t1)+"   "+pi+"  delta: "+delta+"\r\n" );
  }  // Start1


	void BnStart3_Clicked() {
		long t1,t2;
		double pi, summe;
		double delta;
		double h, x, y;
		int anz = ((Integer) spAnzThreads.getValue()).intValue();

		t1 =  System.currentTimeMillis();
      myThread f[] = new myThread[anz ];
      for (int i=0; i<anz; i++) {
        f[i] = new myThread(i, anz, MAX);
      }
      for (int i=0; i<anz; i++) {
        f[i].start();
      }
		try {
	      for (int i=0; i<anz; i++) {
        		f[i].join();
   	   }
	   }
	    catch (InterruptedException e) {
   	 }
		summe=0.0;
      for (int i=0; i<anz; i++) {
        summe+=f[i].getSumme();
      }
		h = 1.0/MAX;
		pi= (float) (summe*4.0*h);
		t2 =  System.currentTimeMillis();
		delta = (pi-Math.PI)*100.0;
		TErg.append("Zeit2: "+(t2-t1)+"   "+pi+"  delta: "+delta+"\r\n" );
  }  // start Threads

 

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

  void BnEsc_Clicked() {
    System.exit(0);
  }


}


class myThread extends Thread {
	private long id;
	private long anzThread;
	private long MAX;
	private double summe;

  public myThread (long id, long anzThread, long MAX) {
		this.id = id;
		this.anzThread=anzThread;
		this.MAX = MAX;
		summe=0.0;
  }

	public double getSumme() {
		return summe;
	}

  public void run() {
		double h, x, y, diff;
		long start, ende, step;
		summe=0.0;
		diff = 1.0 / anzThread;  // (b-a) / AnzThreads
		h = 1.0/MAX;
		step = MAX / anzThread;
		start = step*id;   // 1. mit 0
		ende = start + step;
		if (id == (anzThread-1) )
			ende = MAX;
		x=start*h;
		
      for (long i=start; i<ende; i++) {
			y = (1.0 / (1.0+x*x));
			summe += y;
			//summe+=i+1;
			x+=h;
			// if (x > 1.0) System.out.println("x: "+x);
      }
		/*
		System.out.println("id: "+id
                +"    h: "   +h
                +"    start: "      +start
                +"    ende: "  +ende
                +"    step: "+step
                +"   summe: "+summe+"\n");
		*/
  }

}


