2.2.9静态同步synchronized方法与synchronized(class)代码块
Posted 成功的路上总是离不开贵人的帮助,名师的指点和小人的刺激。
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2.2.9静态同步synchronized方法与synchronized(class)代码块相关的知识,希望对你有一定的参考价值。
关键字synchronized还可以应用在static静态方法上,这样写那是对当前的*.java文件对应的class类进行持锁,
测试如下
package com.cky.bean; /** * Created by edison on 2017/12/8. */ public class Service { synchronized public static void printA() { try { System.out.println("printA ThreadName"+Thread.currentThread().getName()+ "begin"); Thread.sleep(2000); System.out.println("printA ThreadName"+Thread.currentThread().getName()+ "end"); } catch (InterruptedException e) { e.printStackTrace(); } } synchronized public static void printB(){ System.out.println("printb ThreadName"+Thread.currentThread().getName()+ "begin"); System.out.println("printb ThreadName"+Thread.currentThread().getName()+ "end"); } }
package com.cky.thread; import com.cky.bean.Service; /** * Created by edison on 2017/12/8. */ public class ThreadA extends Thread{ @Override public void run() { super.run(); Service.printA(); } }
package com.cky.thread; import com.cky.bean.Service; /** * Created by edison on 2017/12/8. */ public class ThreadB extends Thread{ @Override public void run() { super.run(); Service.printB(); } }
package com.cky.test; import com.cky.bean.Service; import com.cky.thread.ThreadA; import com.cky.thread.ThreadB; /** * Created by edison on 2017/12/8. */ public class Test { public static void main(String[] args) { Service service = new Service(); ThreadA threadA = new ThreadA(); threadA.setName("a"); threadA.start(); ThreadB threadB = new ThreadB(); threadB.setName("b"); threadB.start(); } }
printA ThreadNameabegin
printA ThreadNameaend
printb ThreadNamebbegin
printb ThreadNamebend
Process finished with exit code 0
程序运行结果:
同步的效果,和将synchronized关键字加到非static方法上使用的效果一致,但还是存在本质上的区别,synchronized加到static静态方法上时给Class类上锁,而加到非static静态方法上是给对象上锁。
为了验证不是同一个锁,测试如下。
package com.cky.bean; /** * Created by edison on 2017/12/8. */ public class Service { synchronized public static void printA() { try { System.out.println("printA ThreadName"+Thread.currentThread().getName()+ "begin"); Thread.sleep(2000); System.out.println("printA ThreadName"+Thread.currentThread().getName()+ "end"); } catch (InterruptedException e) { e.printStackTrace(); } } synchronized public static void printB(){ System.out.println("printb ThreadName"+Thread.currentThread().getName()+ "begin"); System.out.println("printb ThreadName"+Thread.currentThread().getName()+ "end"); } synchronized public void printC(){ System.out.println("printc ThreadName"+Thread.currentThread().getName()+ "begin"); System.out.println("printc ThreadName"+Thread.currentThread().getName()+ "end"); } }
package com.cky.thread; import com.cky.bean.Service; /** * Created by edison on 2017/12/8. */ public class ThreadA extends Thread{ @Override public void run() { super.run(); Service.printA(); } }
package com.cky.thread; import com.cky.bean.Service; /** * Created by edison on 2017/12/8. */ public class ThreadB extends Thread{ @Override public void run() { super.run(); Service.printB(); } }
package com.cky.thread; import com.cky.bean.Service; /** * Created by edison on 2017/12/8. */ public class ThreadC extends Thread{ private Service service; public ThreadC(Service service) { this.service = service; } @Override public void run() { super.run(); service.printC(); } }
package com.cky.test; import com.cky.bean.Service; import com.cky.thread.ThreadA; import com.cky.thread.ThreadB; import com.cky.thread.ThreadC; /** * Created by edison on 2017/12/8. */ public class Test { public static void main(String[] args) { Service service = new Service(); ThreadA threadA = new ThreadA(); threadA.setName("a"); threadA.start(); ThreadB threadB = new ThreadB(); threadB.setName("b"); threadB.start(); ThreadC threadC = new ThreadC(service); threadC.setName("c"); threadC.start(); } }
C:\itsoft\jdk\bin\java -Didea.launcher.port=7532 "-Didea.launcher.bin.path=C:\itsoft\idea\IntelliJ IDEA 2016.3.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\itsoft\jdk\jre\lib\charsets.jar;C:\itsoft\jdk\jre\lib\deploy.jar;C:\itsoft\jdk\jre\lib\ext\access-bridge-32.jar;C:\itsoft\jdk\jre\lib\ext\cldrdata.jar;C:\itsoft\jdk\jre\lib\ext\dnsns.jar;C:\itsoft\jdk\jre\lib\ext\jaccess.jar;C:\itsoft\jdk\jre\lib\ext\jfxrt.jar;C:\itsoft\jdk\jre\lib\ext\localedata.jar;C:\itsoft\jdk\jre\lib\ext\nashorn.jar;C:\itsoft\jdk\jre\lib\ext\sunec.jar;C:\itsoft\jdk\jre\lib\ext\sunjce_provider.jar;C:\itsoft\jdk\jre\lib\ext\sunmscapi.jar;C:\itsoft\jdk\jre\lib\ext\sunpkcs11.jar;C:\itsoft\jdk\jre\lib\ext\zipfs.jar;C:\itsoft\jdk\jre\lib\javaws.jar;C:\itsoft\jdk\jre\lib\jce.jar;C:\itsoft\jdk\jre\lib\jfr.jar;C:\itsoft\jdk\jre\lib\jfxswt.jar;C:\itsoft\jdk\jre\lib\jsse.jar;C:\itsoft\jdk\jre\lib\management-agent.jar;C:\itsoft\jdk\jre\lib\plugin.jar;C:\itsoft\jdk\jre\lib\resources.jar;C:\itsoft\jdk\jre\lib\rt.jar;C:\多线程核心技术\第一章\out\production\第一章;C:\itsoft\idea\IntelliJ IDEA 2016.3.3\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain com.cky.test.Test
printA ThreadNameabegin
printc ThreadNamecbegin
printc ThreadNamecend
printA ThreadNameaend
printb ThreadNamebbegin
printb ThreadNamebend
结果可见:
a和b和c是异步执行的,而不是按顺序执行的,因为一个是对象锁,一个是class锁,而class锁可以对所有的实例起作用
测试如下
package com.cky.bean; /** * Created by edison on 2017/12/8. */ public class Service { synchronized public static void printA() { try { System.out.println("printA ThreadName"+Thread.currentThread().getName()+ "begin"); Thread.sleep(2000); System.out.println("printA ThreadName"+Thread.currentThread().getName()+ "end"); } catch (InterruptedException e) { e.printStackTrace(); } } synchronized public static void printB(){ System.out.println("printb ThreadName"+Thread.currentThread().getName()+ "begin"); System.out.println("printb ThreadName"+Thread.currentThread().getName()+ "end"); } }
package com.cky.thread; import com.cky.bean.Service; /** * Created by edison on 2017/12/8. */ public class ThreadA extends Thread{ private Service service; public ThreadA(Service service) { this.service = service; } @Override public void run() { super.run(); service.printA(); } }
package com.cky.thread; import com.cky.bean.Service; /** * Created by edison on 2017/12/8. */ public class ThreadB extends Thread{ private Service service; public ThreadB(Service service) { this.service = service; } @Override public void run() { super.run(); service.printB(); } }
package com.cky.test; import com.cky.bean.Service; import com.cky.thread.ThreadA; import com.cky.thread.ThreadB; /** * Created by edison on 2017/12/8. */ public class Test { public static void main(String[] args) { Service service1 = new Service(); Service service2 = new Service(); ThreadA threadA = new ThreadA(service1); threadA.setName("a"); threadA.start(); ThreadB threadB = new ThreadB(service2); threadB.setName("b"); threadB.start(); } }
printA ThreadNameabegin
printA ThreadNameaend
printb ThreadNamebbegin
printb ThreadNamebend
同步synchronized(class)代码块的作用和synchronized static 方法一样,
测试如下修改Service类如下
package com.cky.bean; /** * Created by edison on 2017/12/8. */ public class Service { public static void printA() { try { synchronized (Service.class) { System.out.println("printA ThreadName"+Thread.currentThread().getName()+ "begin"); Thread.sleep(2000); System.out.println("printA ThreadName"+Thread.currentThread().getName()+ "end"); } } catch (InterruptedException e) { e.printStackTrace(); } } public static void printB(){ synchronized(Service.class) { System.out.println("printb ThreadName"+Thread.currentThread().getName()+ "begin"); System.out.println("printb ThreadName"+Thread.currentThread().getName()+ "end"); } } }
printA ThreadNameabegin
printA ThreadNameaend
printb ThreadNamebbegin
printb ThreadNamebend
以上是关于2.2.9静态同步synchronized方法与synchronized(class)代码块的主要内容,如果未能解决你的问题,请参考以下文章
在java中的synchronized方法或块中使用静态成员