posted by 구름너머 2013. 3. 13. 16:00

소스1:

class PingPong2 {
    synchronized void hit(long n) {
        for(int i = 1; i < 3; i++)
            System.out.print(n + "-" + i + " ");
    }
}

 

소스2:

public class Tester implements Runnable {
    static PingPong2 pp2 = new PingPong2();
    public static void main(String[] args) {
        new Thread(new Tester()).start();
        new Thread(new Tester()).start();
    }
    public void run() { pp2.hit(Thread.currentThread().getId()); }
}

 

예상 결과: n-1 n-2 n-1 n-2

실제 실행 결과 :  D:\studyjava>C:\jdk1.6.0_34\bin\java Tester
8-1 8-2 9-1 9-2