|  |  | |
|  |  | 
| 
 | |||||||
| Programmierung Rat & Tat für Programmierer | 
|  | 
|  | Themen-Optionen | Ansicht | 
|  | 
|  08.11.2008, 23:33 | #1 | 
| Veteran  Registriert seit: 31.03.2006 
					Beiträge: 341
				 |  Java: Synchronisierung von Threads Hallo zusammen! Ich möchte zwei Threads synchronisieren. Der eine Thread erhöht eine Variable, der zweite Thread dekrementiert sie. Ich mache das ganze ja mit "synchronized" Threadsicher. Aber irgendwie klappts nicht so ganz. Der zweite Thread wird gestartet obwohl der erste noch rennt. Hier mal der Code: Code: class increase extends Thread{
    public synchronized void run(){
        System.out.println("Beginn increased 1");
        Global.number++;
        System.out.println(Global.number);
        try {
            increase.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("End increased");
    }
}
class decrease extends Thread{
    public synchronized void run(){
        System.out.println("Beginn decreased 1");
        Global.number--;
        System.out.println(Global.number);
        try {
            decrease.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("End decreased");
    }
}
public class test {
    /**
     * @param args
     */
    
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        increase thread_one = new increase();
        decrease thread_two = new decrease();
        
        System.out.println("Startwert: " + Global.number);
        thread_one.start();
        thread_two.start();
        System.out.println("Endwert: " + Global.number);
    }
}Code: public class Global{
    public static int number=1;
}Code: Startwert: 1 Endwert: 1 Beginn increased 1 2 Beginn decreased 1 1 End increased End decreased LG anve | 
|   |   | 
|  23.12.2008, 12:36 | #2 | 
| Inventar  Registriert seit: 13.06.2001 
					Beiträge: 1.830
				 |   Wie du schon gesagt hast funktioniert synchronized nur auf Objekte, nicht auf primitive Typen (int, float, boolean, ...). Das hier: Code:  public synchronized void run(){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--;
    }
}
				____________________________________ 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) | 
|   |   | 
|  | 
| Aktive Benutzer in diesem Thema: 1 (Registrierte Benutzer: 0, Gäste: 1) | |
| 
 | 
 |