Wie du schon gesagt hast funktioniert synchronized nur auf Objekte, nicht auf primitive Typen (int, float, boolean, ...).
Das hier:
Code:
public synchronized void run(){
heißt das die Methode run() nur von einem (1) anderen Thread aufgerufen werden kann. Eigentlich heißt es sogar noch ein bischen mehr. synchronized in einer Methodensignatur (also z.B. public synchronized void foo(){...}) bedeutet das ein Lock auf this (das aktuelle Objekt) gesetzt wird. D.h. wenn es mehrere synchronized methoden gibt kann im selben Objekt nur eine von beiden gleichzeitig laufen.
Du synchronisierst run(), eigentlich willst du aber der inkrement/dekrement synchronisieren.
Das würde in etwa so funktionieren:
Code:
public class Global{
private static int number=1;
public synchronized void increment(){
number++;
}
public synchronized void decrement(){
number--;
}
}
jak
____________________________________
Join the DNRC |
Godwin\'s Law (thx@stona)
Documentation is like sex: If it\'s good, it\'s very, very good. If it\'s bad, it\'s better than nothing.
\"In theory, theory and practice are the same. In practice, they are not\" (Lawrence Berra)