例1,没有同步的时候运行同一个对象的同一个方法的结果:ide
public class TestSyn { public 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(); } }
结果:函数
能够看到,是同时在执行一个方法里面的内容,没有进行同步spa
例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(); } } }
结果:3d
能够看到是一个方法执行完后再执行下一次,已经进行了同步code
例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(); } }
发不一样的两个方法依然进行了同步。blog
咱们从新新建一个对象,执行相同的同步函数观察结果:继承
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(); } }
发现不一样对象的同一个方法没有进行同步get
例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(); } }
发现又没有进行同步了。
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方法