
import java.io.*;

// vier Threads, diesmal mit Aufruf start
// 10 * 1,2,3,4  addieren= Summe 100
public class Thread05a  {

   private static void pause() {
    try {
      System.in.read();
    }
    catch (IOException e) {
    }
  }  // pause

  private void test1() {
    MyThread t1,t2;
    MyThread t3,t4;
    CalcSumme cs= new CalcSumme();  // Ampel+Summe
    t1 = new MyThread(1,1000,cs);
    t2 = new MyThread(2, 1000,cs);
    t3 = new MyThread(3,1000,cs);
    t4 = new MyThread(4,1000,cs);
    
    t1.start();
    t2.start();
    t3.start();
    t4.start();
    try {
      t2.join();
      t3.join();
      t1.join();
      t4.join();
    }
    catch (InterruptedException e) {
    }    
    System.out.println("***************  Summe: "
     +Long.toString(cs.getSumme() ) ); 
   } // start



  public static void main(String[] args) {
    Thread05a frame = new Thread05a();
    frame.test1();
    pause();
  }

 

class CalcSumme {
  private long summe;

  public CalcSumme() {
    summe=0;
  }
    // synchronized
   public synchronized void add(long nr) {
      summe = summe + nr;
  }
   public long getSumme() {
    return summe;
   }
} // CalcSumme

class MyThread extends Thread {
  private int nummer=0;
  private int n;
  private CalcSumme cs;

  public MyThread int nr, int n, CalcSumme cs) {
     nummer = nr;
     this.n=n;
     this.cs=cs;
  }


 public void run() {
    long summe=0;
    for (int i=0; i<n; i++) {
      summe+=nummer;
    }
    cs.add(summe);
  }  // run

}





}


