Ich habe folgendes Beispiel erstellt..komme aber nicht ganz klar was die Synchronisation mit wait und notify angeht. Könntet ihr mir helfen?
Java:
public class Wert {
private int x;
public boolean ok= false;
public synchronized int get(){
return x;
}
public synchronized void put(int x){
while(ok == false){
try{
ok = true;
wait();
}
catch(InterruptedException e){
}
}
notify();
this.x = x;
System.out.println(x);
}
}
public class Erzeuger extends Thread{
Wert w;
public Erzeuger(Wert w){
this.w = w;
}
public void run(){
for(int i = 0; i < 7;++i){
w.put(i);
try{
sleep((int) Math.random()*100);
}
catch(Exception e){
}
}
}
}
public class Test {
public static void main(String[] args) {
Wert w1 = new Wert();
Wert w2 = new Wert();
Erzeuger t1 = new Erzeuger(w2);
Erzeuger t2 = new Erzeuger(w2);
t1.start();
t2.start();
}
}