Przykład Ping wątku Java

Próbuję zrozumieć podstawy wątku i jako pierwszy przykład tworzę dwa wątki, które zapisują String na standardowe wyjście. Jak wiem, program planujący umożliwia wykonywanie wątków za pomocą harmonogramu rundy robin. Dlatego mam:

PING PING ponga ponga ponga PING PING PING ponga

Teraz chcę użyć wspólnej zmiennej, więc każdy wątek będzie wiedział, czy twoja kolej:

public class PingPongThread extends Thread {
private String msg;
private static String turn;

public PingPongThread(String msg){
    this.msg = msg;
}
@Override
public void run() {
    while(true) {
        playTurn();
    }

}
public synchronized void playTurn(){
    if (!msg.equals(turn)){
        turn=msg;
        System.out.println(msg);
    }
}
}

Klasa główna:

public class ThreadTest {
    public static void main(String[] args) {
        PingPongThread thread1 = new PingPongThread("PING");
        PingPongThread thread2 = new PingPongThread("pong");
        thread1.start();
        thread2.start();
    }
}

Zsynchronizowałem „menedżera zwrotów”, ale nadal otrzymuję coś takiego:

PING PING ponga ponga ponga PING PING PING ponga

Czy ktoś może wyjaśnić, czego mi brakuje, i dlaczego nie dostaję Ping ponga ... ping ponga. Dzięki!

questionAnswers(6)

yourAnswerToTheQuestion