synchronized 例子

例1,没有同步的时候运行同一个对象的同一个方法的结果:

public class TestSyn {
    public synchronized void showMsg() {
        try {
            for (int i = 0; i < 3; i++) {
                System.out.println(Thread.currentThread().getName() + ":" + i);
                Thread.sleep(200);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        TestSyn testSyn = new TestSyn();
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                testSyn.showMsg();
            }
        };
        Runnable runnable2 = new Runnable() {
            @Override
            public void run() {
                testSyn.showMsg();
            }
        };
        executorService.execute(runnable);
        executorService.execute(runnable2);
        executorService.shutdown();
    }
}

结果:

synchronized基础-LMLPHP

可以看到,是同时在执行一个方法里面的内容,没有进行同步

例2,当我们其它不变,只是在方法上加synchronized后:

public class TestSyn {
    public synchronized void showMsg() {
        try {
            for (int i = 0; i < 3; i++) {
                System.out.println(Thread.currentThread().getName() + ":" + i);
                Thread.sleep(200);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

结果:

synchronized基础-LMLPHP

可以看到是一个方法执行完后再执行下一次,已经进行了同步

例3,我们在添加另外一个synchronized后观察运行结果:

public class TestSyn {
    public synchronized void showMsg() {
        try {
            for (int i = 0; i < 3; i++) {
                System.out.println(Thread.currentThread().getName() + ":" + i);
                Thread.sleep(200);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public synchronized void showMsg2() {
        try {
            for (int i = 0; i < 3; i++) {
                System.out.println(Thread.currentThread().getName() + ":" + i);
                Thread.sleep(200);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        TestSyn testSyn = new TestSyn();
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                testSyn.showMsg();
            }
        };
        Runnable runnable2 = new Runnable() {
            @Override
            public void run() {
                testSyn.showMsg2();
            }
        };
        executorService.execute(runnable);
        executorService.execute(runnable2);
        executorService.shutdown();
    }
}

synchronized基础-LMLPHP

发不同的两个方法依然进行了同步。

我们重新新建一个对象,执行相同的同步函数观察结果:

public class TestSyn {
    public synchronized void showMsg() {
        try {
            for (int i = 0; i < 3; i++) {
                System.out.println(Thread.currentThread().getName() + ":" + i);
                Thread.sleep(200);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        TestSyn testSyn = new TestSyn();
        TestSyn testSyn2 = new TestSyn();
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                testSyn.showMsg();
            }
        };
        Runnable runnable2 = new Runnable() {
            @Override
            public void run() {
                testSyn2.showMsg();
            }
        };
        executorService.execute(runnable);
        executorService.execute(runnable2);
        executorService.shutdown();
    }
}

synchronized基础-LMLPHP

发现不同对象的同一个方法没有进行同步

 例4,我们换成一个static方法添加synchronized后:

public class TestSyn {
    public synchronized void showMsg() {
        try {
            for (int i = 0; i < 3; i++) {
                System.out.println(Thread.currentThread().getName() + ":" + i);
                Thread.sleep(200);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public synchronized static void showMsg2() {
        try {
            for (int i = 0; i < 3; i++) {
                System.out.println(Thread.currentThread().getName() + ":" + i);
                Thread.sleep(200);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        TestSyn testSyn = new TestSyn();
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                testSyn.showMsg();
            }
        };
        Runnable runnable2 = new Runnable() {
            @Override
            public void run() {
                TestSyn.showMsg2();
            }
        };
        executorService.execute(runnable);
        executorService.execute(runnable2);
        executorService.shutdown();
    }
}

synchronized基础-LMLPHP

发现又没有进行同步了。

结论:

1.某个对象实例内,synchronized aMethod(){}关键字可以防止多个线程访问对象的synchronized方法(如果一个对象有多个synchronized方法,只要一个线程访问了其中的一个synchronized方法,其它线程不能同时访问这个对象中任何一个synchronized方法)。这时,不同的对象实例的synchronized方法是不相干扰的。也就是说,其它线程照样可以同时访问相同类的另一个对象实例中的synchronized方法

2.是某个类的范围,synchronized static aStaticMethod{}防止多个线程同时访问这个类中的synchronized static 方法。它可以对类的所有对象实例起作用

3.如果同一对象两个synchronized方法一个是非static方法和static方法,是不相干扰的

4.synchronized关键字是不能继承的,也就是说,基类的方法synchronized f(){} 在继承类中并不自动是synchronized f(){},而是变成了f(){}。继承类需要你显式的指定它的某个方法为synchronized方法

11-21 05:18