import java.io.*;

import java.util.concurrent.Semaphore;

public class TestThread2{

   int summe;

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


  public void test1() {
    Semaphore CritSem = new Semaphore(1,true);
    Thread1 t1,t2;
    summe=0;
    t1 = new Thread1(100,CritSem);
    t2 = new Thread1(100,CritSem);
    t1.start();
    t2.start();
    try {
         t1.join();
         t2.join();
       }
    catch (InterruptedException e) {
    }
    System.out.println("summe: "+summe);
  }

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

  class Thread1 extends Thread {
     int n;
     Semaphore CritSem;
    public Thread1(int n, Semaphore CritSem) {
       this.n=n;
       this.CritSem = CritSem;
    }
    public void run() {
         for (int i=1; i<=n; i++) {
          summe++;
         }
    }

    public void run1() {
       try {
         for (int i=1; i<=n; i++) {
          CritSem.acquire();
          summe++;
          CritSem.release();
         }
       }
       catch (InterruptedException ie)
       { 
            System.out.println("InterruptedException"); 
       }
    } // run
  }

}
