MichiLee: Heap/Steak und Static in Java

Beitrag lesen

Nabend Forum,
hätte eine kleine Frage zur folgende Seite mit Heap und Stack:
http://blogs.escde.net/jochen/archive/2007/06/13/79.aspx

Was ist denn nur ganz grob der Vorteil, wenn man in der Programmiersprache noch zwischen Speicherstellen wie Heap und Stack unterscheiden?
Ein Werteobjekt ist ja kein Referenz-Typ.
An sich wäre aber praktisch das "ms" ja eine Referenz?

"
MyStruct ms;
 ms.SetI(1);
"

Wir haben noch folgenden Code zum Anschauen bekommen, was an Semaphoren angelehnt sei:

  
class Semaphore {  
  
  protected int n;  
  
  public Semaphore (int n) {  
  
    this.n = n;  
  
  }  
  
  public synchronized void warten () {  
  
    while (n <= 0)  
  
      try {  
  
        wait();  
  
      } catch(Exception e) {  
  
        e.printStackTrace();  
  
      }  
  
    -- n;  
  
  }  
  
  public synchronized void signalisieren () {  
  
    if (++ n > 0)  
  
      notify();  
  
  }  
  
}  
  
  
  
//  
  
// Skript "Synchronisation", p6  
  
//  
  
class A extends Thread {  
  
  private float konto;  
  
  
  
  public void run() {  
  
  
  
    System.out.println("A:wait(s)");  
  
    example.s.warten();  
  
  
  
    System.out.println("A: Lesen Konto");  
  
    konto = example.account;  
  
  
  
    // wait for a random time  
  
    try {  
  
      sleep((long)(Math.random() * 1000),0);  
  
    } catch (InterruptedException e) {}  
  
  
  
    System.out.println("A: 5.1 Euro addieren");  
  
    konto += 5.1F;  
  
  
  
    System.out.println("A: Schreiben Konto");  
  
    example.account = konto;  
  
  
  
    System.out.println("A:signal(s)");  
  
    example.s.signalisieren();  
  
  }  
  
}  
  
  
  
class B extends Thread {  
  
  private float konto;  
  
  
  
  public void run() {  
  
  
  
    System.out.println("B:wait(s)");  
  
    example.s.warten();  
  
  
  
    System.out.println("B: Lesen Konto");  
  
    konto = example.account;  
  
  
  
    // wait for a random time  
  
    try {  
  
      sleep((long)(Math.random() * 1000),0);  
  
    } catch (InterruptedException e) {}  
  
  
  
    System.out.println("B: 4.5 Euro abziehen");  
  
    konto -= 4.5F;  
  
  
  
    System.out.println("B: Schreiben Konto");  
  
    example.account = konto;  
  
  
  
    System.out.println("B:signal(s)");  
  
    example.s.signalisieren();  
  
  }  
  
}  
  
  
  
  
  
public class example {  
  
  public static float account = 25.5F;  
  
  public static Semaphore s = new Semaphore(1);  
  
  
  
  public static void main(String[] args) {  
  
    A a = new A();  
  
    B b = new B();  
  
  
  
    a.start();  
  
    b.start();  
  
  
  
    try {  
  
      a.join();  
  
      b.join();  
  
    } catch (InterruptedException e) {}  
  
  
  
    System.out.println("Neuer Kontostand: " + example.account);  
  
  }  
  
}  
  
  

Da ja in Example die Variable account statisch ist, würden ja Klasse A und B diese Klassenvariable jedesmal überschreiben ne? (Hab jetzt leider kein Netbeans und Eclipse vor Ort zum testen. Ich frage mich nur, obwohl A und B von Thread abgeleitet ist, warum das gleich beim Objektbilden zu nem Thread wird, da ja A a = new A() aufgerufen wird, anstatt Thread a = new A();
Ich schau mir nochmals die Objektbildung und die Hierarchie an, echt übel, was man innerhalb von 1-2 Monaten vergessen und wieder durcheinanderbringen kann :-)
Vielleicht liegts auch an der Uhrzeit

Grüße