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

// 100*101/2  =  5050
// 5050 * 3 =  15150
// mit Threads, aber ohne Join

public class Thread01b extends JFrame {


  //Frame konstruieren
  public Thread01b() {
     JLabel label1;
     int n=100;


    setSize(400, 320);
    this.setLocation(500, 100);
    setTitle("Thread Testliste1");

    label1 = new JLabel("Summe");
    label1.setFont( new Font("Arial", Font.BOLD,32));


    this.getContentPane().add(label1, BorderLayout.NORTH);

    ThreadSumme f1,f2, f3;
    // Anzeige des Fenster, Zwang
    setVisible(true);

    f1 = new ThreadSumme(n);
    f2 = new ThreadSumme(n);
    f3 = new ThreadSumme(n);
    f1.start();
    f2.start();
    f3.start();

	try {
         f3.join();
         f2.join();
         f1.join();
       }
    catch (InterruptedException e) {
    }


    int Summe = 0;
    Summe = f1.getSumme()+f2.getSumme()+f3.getSumme();
    label1.setText( "Summe: "+Integer.toString(Summe) );
  } // create



/*
    try {
           Thread.sleep(10000);
        } catch (InterruptedException e) {
    }
*/

  //Überschreiben, damit das Programm bei Herunterfahren des Systems beendet werden kann
  protected void processWindowEvent(WindowEvent e) {
    super.processWindowEvent(e);
    if(e.getID() == WindowEvent.WINDOW_CLOSING) {
      System.exit(0);
    }
  }


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


}



class ThreadSumme extends Thread {

   private int _n;
   private int _Summe;

  // Übergabe der Grenze
  public ThreadSumme (int n) {
     _n = n;
  }

  public int getSumme() {
    return _Summe;
  }

   public void run() {
    System.out.println("Start Thread");
     _Summe = 0;
    for (int i=1; i<=_n; i++) {
      _Summe+=i;
      delay(5);
    }
    System.out.println("Ende Thread");
  }

  public static void delay(long time) {
    try {
           Thread.sleep(50);
        } catch (InterruptedException e) {
    }
  }  // delay


   // Thread Methode


}




/*

    try {
      Thread.sleep(50);
    } catch (InterruptedException e) {
    }


*/
