转载

synchronized(this)、synchronized(class)与synchronized(Object)的区别

在多线程开发中,我们经常看到synchronized(this)、synchronized(*.class)与synchronized(任意对象)这几种类型同步方法。但是是否知道这几种写法有什么区别了?下面根据代码来分析:


synchronized代码块间的同步性

  1. package com.zwz.thread.demo1;
  2. public class ObjectService {
  3. public void serviceMethodA(){
  4. try {
  5. synchronized (this) {
  6. System.out.println("A begin time="+System.currentTimeMillis());
  7. Thread.sleep(2000);
  8. System.out.println("A end time="+System.currentTimeMillis());
  9. }
  10. } catch (InterruptedException e) {
  11. e.printStackTrace();
  12. }
  13. }
  14. public void serviceMethodB(){
  15. synchronized (this) {
  16. System.out.println("B begin time="+System.currentTimeMillis());
  17. System.out.println("B end time="+System.currentTimeMillis());
  18. }
  19. }
  20. }


  1. package com.zwz.thread.demo1;
  2. public class ThreadA extends Thread {
  3. private ObjectService objectService;
  4. public ThreadA(ObjectService objectService){
  5. super();
  6. this.objectService=objectService;
  7. }
  8. @Override
  9. public void run() {
  10. super.run();
  11. objectService.serviceMethodA();
  12. }
  13. }


  1. package com.zwz.thread.demo1;
  2. public class ThreadB extends Thread {
  3. private ObjectService objectService;
  4. public ThreadB(ObjectService objectService){
  5. super();
  6. this.objectService=objectService;
  7. }
  8. @Override
  9. public void run() {
  10. super.run();
  11. objectService.serviceMethodB();
  12. }
  13. }


  1. package com.zwz.thread.demo1;
  2. public class MainTest {
  3. public static void main(String[] args) {
  4. ObjectService service=new ObjectService();
  5. ThreadA a=new ThreadA(service);
  6. a.setName("a");
  7. a.start();
  8. ThreadB b=new ThreadB(service);
  9. b.setName("b");
  10. b.start();
  11. }
  12. }


运行结果:


结论:

当一个线程访问ObjectService的一个synchronized (this)同步代码块时,其它线程对同一个ObjectService中其它的synchronized (this)同步代码块的访问将是堵塞,这说明synchronized (this)使用的对象监视器是一个。


验证synchronized (this)代码块是锁定当前对象

  1. package com.zwz.thread.demo2;
  2. public class ObjectService {
  3. public void objectMethodA(){
  4. System.out.println("run----objectMethodA");
  5. }
  6. public void objectMethodB(){
  7. synchronized (this) {
  8. try {
  9. for (int i = 1; i <= 10; i++) {
  10. System.out.println("synchronized thread name:"+Thread.currentThread().getName()+"-->i="+i);
  11. Thread.sleep(1000);
  12. }
  13. } catch (InterruptedException e) {
  14. e.printStackTrace();
  15. }
  16. }
  17. }
  18. }


  1. package com.zwz.thread.demo2;
  2. public class ThreadA extends Thread {
  3. private ObjectService objectService;
  4. public ThreadA(ObjectService objectService) {
  5. super();
  6. this.objectService = objectService;
  7. }
  8. @Override
  9. public void run() {
  10. super.run();
  11. objectService.objectMethodA();
  12. }
  13. }


  1. package com.zwz.thread.demo2;
  2. public class ThreadB extends Thread {
  3. private ObjectService objectService;
  4. public ThreadB(ObjectService objectService) {
  5. super();
  6. this.objectService = objectService;
  7. }
  8. @Override
  9. public void run() {
  10. super.run();
  11. objectService.objectMethodB();
  12. }
  13. }


  1. package com.zwz.thread.demo2;
  2. public class MainTest {
  3. public static void main(String[] args) throws InterruptedException {
  4. ObjectService service=new ObjectService();
  5. ThreadB b=new ThreadB(service);
  6. b.start();
  7. Thread.sleep(2000);
  8. ThreadA a=new ThreadA(service);
  9. a.start();
  10. }
  11. }


运行结果:


可以看到objectMethodA方法异步执行了,下面我们将objectMethodA()加上同步。

  1. package com.zwz.thread.demo2;
  2. public class ObjectService {
  3. public synchronized void objectMethodA(){
  4. System.out.println("run----objectMethodA");
  5. }
  6. public void objectMethodB(){
  7. synchronized (this) {
  8. try {
  9. for (int i = 1; i <= 10; i++) {
  10. System.out.println("synchronized thread name:"+Thread.currentThread().getName()+"-->i="+i);
  11. Thread.sleep(1000);
  12. }
  13. } catch (InterruptedException e) {
  14. e.printStackTrace();
  15. }
  16. }
  17. }
  18. }


运行结果:


结论:

上面三个小例子我们可以知道,多个线程调用同一个对象中的不同名称的synchronized同步方法或synchronized(this)同步代码块时,是同步的。

1、synchronized同步方法

①对其它的synchronized同步方法或synchronized(this)同步代码块调用是堵塞状态;

②同一时间只有一个线程执行synchronized同步方法中的代码。

2、synchronized(this)同步代码块

①对其它的synchronized同步方法或synchronized(this)同步代码块调用是堵塞状态;

②同一时间只有一个线程执行synchronized同步方法中的代码。


将任意对象作为对象监视器

  1. package com.zwz.thread.demo3;
  2. public class ObjectService {
  3. private String uname;
  4. private String pwd;
  5. String lock=new String();
  6. public void setUserNamePassWord(String userName,String passWord){
  7. try {
  8. synchronized (lock) {
  9. System.out.println("thread name="+Thread.currentThread().getName()
  10. +" 进入代码快:"+System.currentTimeMillis());
  11. uname=userName;
  12. Thread.sleep(3000);
  13. pwd=passWord;
  14. System.out.println("thread name="+Thread.currentThread().getName()
  15. +" 进入代码快:"+System.currentTimeMillis()+"入参uname:"+uname+"入参pwd:"+pwd);
  16. }
  17. } catch (InterruptedException e) {
  18. e.printStackTrace();
  19. }
  20. }
  21. }


  1. package com.zwz.thread.demo3;
  2. public class ThreadA extends Thread {
  3. private ObjectService objectService;
  4. public ThreadA(ObjectService objectService) {
  5. super();
  6. this.objectService = objectService;
  7. }
  8. @Override
  9. public void run() {
  10. objectService.setUserNamePassWord("a", "aa");
  11. }
  12. }


  1. package com.zwz.thread.demo3;
  2. public class ThreadB extends Thread {
  3. private ObjectService objectService;
  4. public ThreadB(ObjectService objectService) {
  5. super();
  6. this.objectService = objectService;
  7. }
  8. @Override
  9. public void run() {
  10. objectService.setUserNamePassWord("b", "bb");
  11. }
  12. }


  1. package com.zwz.thread.demo3;
  2. public class MainTest {
  3. public static void main(String[] args) {
  4. ObjectService service=new ObjectService();
  5. ThreadA a=new ThreadA(service);
  6. a.setName("A");
  7. a.start();
  8. ThreadB b=new ThreadB(service);
  9. b.setName("B");
  10. b.start();
  11. }
  12. }


运行结果:


下面我把String lock=new String();放在方法中会有啥结果了:

  1. package com.zwz.thread.demo3;
  2. public class ObjectService {
  3. private String uname;
  4. private String pwd;
  5. public void setUserNamePassWord(String userName,String passWord){
  6. try {
  7. String lock=new String();
  8. synchronized (lock) {
  9. System.out.println("thread name="+Thread.currentThread().getName()
  10. +" 进入代码快:"+System.currentTimeMillis());
  11. uname=userName;
  12. Thread.sleep(3000);
  13. pwd=passWord;
  14. System.out.println("thread name="+Thread.currentThread().getName()
  15. +" 进入代码快:"+System.currentTimeMillis()+"入参uname:"+uname+"入参pwd:"+pwd);
  16. }
  17. } catch (InterruptedException e) {
  18. e.printStackTrace();
  19. }
  20. }
  21. }


运行结果:



结论:

多个线程持有对象监视器作为同一个对象的前提下,同一时间只有一个线程可以执行synchronized(任意自定义对象)同步代码快。


synchronized(任意自定义对象)与synchronized同步方法共用

  1. package com.zwz.thread.demo4;
  2. public class ObjectService {
  3. private String lock=new String();
  4. public void methodA(){
  5. try {
  6. synchronized (lock) {
  7. System.out.println("a begin");
  8. Thread.sleep(3000);
  9. System.out.println("a end");
  10. }
  11. } catch (InterruptedException e) {
  12. e.printStackTrace();
  13. }
  14. }
  15. public synchronized void methodB(){
  16. System.out.println("b begin");
  17. System.out.println("b end");
  18. }
  19. }


  1. package com.zwz.thread.demo4;
  2. public class ThreadA extends Thread {
  3. private ObjectService objectService;
  4. public ThreadA(ObjectService objectService) {
  5. super();
  6. this.objectService = objectService;
  7. }
  8. @Override
  9. public void run() {
  10. objectService.methodA();
  11. }
  12. }


  1. package com.zwz.thread.demo4;
  2. public class ThreadB extends Thread {
  3. private ObjectService objectService;
  4. public ThreadB(ObjectService objectService) {
  5. super();
  6. this.objectService = objectService;
  7. }
  8. @Override
  9. public void run() {
  10. objectService.methodB();
  11. }
  12. }


  1. package com.zwz.thread.demo4;
  2. public class MainTest {
  3. public static void main(String[] args) {
  4. ObjectService service=new ObjectService();
  5. ThreadA a=new ThreadA(service);
  6. a.setName("A");
  7. a.start();
  8. ThreadB b=new ThreadB(service);
  9. b.setName("B");
  10. b.start();
  11. }
  12. }


运行结果:


结论:

使用synchronized(任意自定义对象)进行同步操作,对象监视器必须是同一个对象。不过不是同一个,运行就是异步执行了。


静态同步synchronized方法与synchronized(*.class)代码块


静态同步synchronized方法

  1. package com.zwz.thread.demo6;
  2. public class ObjectService {
  3. public synchronized static void methodA(){
  4. try {
  5. System.out.println("static methodA begin 线程名称:"+Thread.currentThread().getName()+" times:"+System.currentTimeMillis());
  6. Thread.sleep(3000);
  7. System.out.println("static methodA end 线程名称:"+Thread.currentThread().getName()+" times:"+System.currentTimeMillis());
  8. } catch (InterruptedException e) {
  9. e.printStackTrace();
  10. }
  11. }
  12. public synchronized static void methodB(){
  13. System.out.println("static methodB begin 线程名称:"+Thread.currentThread().getName()+" times:"+System.currentTimeMillis());
  14. System.out.println("static methodB end 线程名称:"+Thread.currentThread().getName()+" times:"+System.currentTimeMillis());
  15. }
  16. }

  1. package com.zwz.thread.demo6;
  2. public class ThreadA extends Thread {
  3. @Override
  4. public void run() {
  5. ObjectService.methodA();
  6. }
  7. }

  1. package com.zwz.thread.demo6;
  2. public class ThreadB extends Thread {
  3. @Override
  4. public void run() {
  5. ObjectService.methodB();
  6. }
  7. }

  1. package com.zwz.thread.demo6;
  2. public class MainTest {
  3. public static void main(String[] args) {
  4. ThreadA a=new ThreadA();
  5. a.setName("A");
  6. a.start();
  7. ThreadB b=new ThreadB();
  8. b.setName("B");
  9. b.start();
  10. }
  11. }

运行结果:

结论:
synchronized应用在static方法上,那是对当前对应的*.Class进行持锁。

synchronized(*.class)代码块

  1. package com.zwz.thread.demo7;
  2. public class ObjectService {
  3. public void methodA(){
  4. try {
  5. synchronized (ObjectService.class) {
  6. System.out.println("methodA begin 线程名称:"+Thread.currentThread().getName()+" times:"+System.currentTimeMillis());
  7. Thread.sleep(3000);
  8. System.out.println("methodA end 线程名称:"+Thread.currentThread().getName()+" times:"+System.currentTimeMillis());
  9. }
  10. } catch (InterruptedException e) {
  11. e.printStackTrace();
  12. }
  13. }
  14. public void methodB(){
  15. synchronized (ObjectService.class) {
  16. System.out.println("methodB begin 线程名称:"+Thread.currentThread().getName()+" times:"+System.currentTimeMillis());
  17. System.out.println("methodB end 线程名称:"+Thread.currentThread().getName()+" times:"+System.currentTimeMillis());
  18. }
  19. }
  20. }

  1. package com.zwz.thread.demo7;
  2. public class ThreadA extends Thread {
  3. private ObjectService objectService;
  4. public ThreadA(ObjectService objectService) {
  5. super();
  6. this.objectService = objectService;
  7. }
  8. @Override
  9. public void run() {
  10. objectService.methodA();
  11. }
  12. }

  1. package com.zwz.thread.demo7;
  2. public class ThreadB extends Thread {
  3. private ObjectService objectService;
  4. public ThreadB(ObjectService objectService) {
  5. super();
  6. this.objectService = objectService;
  7. }
  8. @Override
  9. public void run() {
  10. objectService.methodB();
  11. }
  12. }

  1. package com.zwz.thread.demo7;
  2. public class MainTest {
  3. public static void main(String[] args) {
  4. ObjectService service=new ObjectService();
  5. ThreadA a=new ThreadA(service);
  6. a.setName("A");
  7. a.start();
  8. ThreadB b=new ThreadB(service);
  9. b.setName("B");
  10. b.start();
  11. }
  12. }

运行结果:

上面测试方法是共同对象,下面我们分别实例化一个对象:
  1. package com.zwz.thread.demo7;
  2. public class MainTest {
  3. public static void main(String[] args) {
  4. ObjectService service1=new ObjectService();
  5. ObjectService service2=new ObjectService();
  6. ThreadA a=new ThreadA(service1);
  7. a.setName("A");
  8. a.start();
  9. ThreadB b=new ThreadB(service2);
  10. b.setName("B");
  11. b.start();
  12. }
  13. }

运行结果:

结论:
同步synchronized(*.class)代码块的作用其实和synchronized static方法作用一样。Class锁对类的所有对象实例起作用。
原文链接:https://blog.csdn.net/luckey_zh/article/details/53815694
正文到此结束
Loading...