关键字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:itsoftjdkinjava -Didea.launcher.port=7532 "-Didea.launcher.bin.path=C:itsoftideaIntelliJ IDEA 2016.3.3in" -Dfile.encoding=UTF-8 -classpath "C:itsoftjdkjrelibcharsets.jar;C:itsoftjdkjrelibdeploy.jar;C:itsoftjdkjrelibextaccess-bridge-32.jar;C:itsoftjdkjrelibextcldrdata.jar;C:itsoftjdkjrelibextdnsns.jar;C:itsoftjdkjrelibextjaccess.jar;C:itsoftjdkjrelibextjfxrt.jar;C:itsoftjdkjrelibextlocaledata.jar;C:itsoftjdkjrelibext
ashorn.jar;C:itsoftjdkjrelibextsunec.jar;C:itsoftjdkjrelibextsunjce_provider.jar;C:itsoftjdkjrelibextsunmscapi.jar;C:itsoftjdkjrelibextsunpkcs11.jar;C:itsoftjdkjrelibextzipfs.jar;C:itsoftjdkjrelibjavaws.jar;C:itsoftjdkjrelibjce.jar;C:itsoftjdkjrelibjfr.jar;C:itsoftjdkjrelibjfxswt.jar;C:itsoftjdkjrelibjsse.jar;C:itsoftjdkjrelibmanagement-agent.jar;C:itsoftjdkjrelibplugin.jar;C:itsoftjdkjrelib
esources.jar;C:itsoftjdkjrelib
t.jar;C:多线程核心技术第一章outproduction第一章;C:itsoftideaIntelliJ IDEA 2016.3.3libidea_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