synchronized 同步语句块和同步方法区别

public class PersonService {

private Person person;

private int count=0;

public PersonService() {
}

public PersonService(Person person) {
    this.person = person;
}

/**
 * 同步方法锁定的是当前对象,等同同步代码块 synchronized(this){}
 */
public synchronized void testMethod() {
    for (int i = 0; i <10; i++) {
        count++;
        System.out.println(Thread.currentThread().getName()+":"+count);
    }
}

/**
 * PersonService.class 是Class类的实例,线程获取的对象锁是class锁,
 * class锁对类的所有对象实例起到同步作用
 */
public void testClass() {
    synchronized (PersonService.class) {
        for (int i = 0; i <10; i++) {
            count++;
            System.out.println(Thread.currentThread().getName()+":"+count);
        }
    }

}

/**
 * this表示当前运行的对象,synchronized锁定的是当前对象,
 * 如果线程的this不同,则不是同一个锁,会进行异步操作而不是同步操作
 */
public void testThis() {
    synchronized (this){
        for (int i = 0; i < 10; i++) {
            count++;
            System.out.println(Thread.currentThread().getName()+":"+count);
        }
    }
}

/**
 * 参数传递object,synchronized锁定的是object对象,如果线程获取的object对象不同,
 * 则不是同一个对象锁,会进行异步操作而不是同步操作
 * @param object
 */
public void testObject(Object object) {
    synchronized (object) {
        for (int i = 0; i <10; i++) {
            count++;
            System.out.println(Thread.currentThread().getName()+":"+count);
        }
    }
}

}



public class PersonThread extends Thread {

private PersonService personService;

public PersonThread(PersonService personService) {
    this.personService = personService;
}

@Override
public void run() {
//       personService.testObject(personService);//运行代码获取结果
//        personService.testClass();//运行代码获取结果
    personService.testThis();
}
}


public class Demo {
public static void main(String[] args) {

    PersonService personService = new PersonService();
    测试不是同一个对象锁的结果:测试结果1
    PersonThread p1 = new PersonThread(personService);
    PersonThread p2 = new PersonThread(new PersonService());
    PersonThread p3 = new PersonThread(new PersonService());
    PersonThread p4 = new PersonThread(new PersonService());
    PersonThread p5 = new PersonThread(new PersonService());
    
      //测试同一个对象锁的结果:测试结果2
//        PersonThread p1 = new PersonThread(personService);
//        PersonThread p2 = new PersonThread(personService);
//        PersonThread p3 = new PersonThread(personService);
//        PersonThread p4 = new PersonThread(personService);
//        PersonThread p5 = new PersonThread(personService);
    p1.start();
    p2.start();
    p3.start();
    p4.start();
    p5.start();

}
}

测试结果1:线程之间异步执行
Thread-2:1
Thread-1:1
Thread-2:2
Thread-1:2
Thread-2:3
Thread-2:4
Thread-1:3
Thread-1:4
Thread-1:5
Thread-1:6
Thread-1:7
Thread-2:5
Thread-1:8
Thread-3:1
Thread-0:1
Thread-1:9
Thread-2:6
Thread-1:10
Thread-0:2
Thread-0:3
Thread-0:4
Thread-0:5
Thread-3:2
Thread-0:6
Thread-4:1
Thread-2:7
Thread-4:2
Thread-0:7
Thread-3:3
Thread-0:8
Thread-4:3
Thread-2:8
Thread-2:9
Thread-2:10
Thread-4:4
Thread-4:5
Thread-4:6
Thread-4:7
Thread-4:8
Thread-4:9
Thread-4:10
Thread-0:9
Thread-0:10
Thread-3:4
Thread-3:5
Thread-3:6
Thread-3:7
Thread-3:8
Thread-3:9
Thread-3:10

测试结果2:线程之间同步执行
Thread-0:1
Thread-0:2
Thread-0:3
Thread-0:4
Thread-0:5
Thread-0:6
Thread-0:7
Thread-0:8
Thread-0:9
Thread-0:10
Thread-2:11
Thread-2:12
Thread-2:13
Thread-2:14
Thread-2:15
Thread-2:16
Thread-2:17
Thread-2:18
Thread-2:19
Thread-2:20
Thread-1:21
Thread-1:22
Thread-1:23
Thread-1:24
Thread-1:25
Thread-1:26
Thread-1:27
Thread-1:28
Thread-1:29
Thread-1:30
Thread-4:31
Thread-4:32
Thread-4:33
Thread-4:34
Thread-4:35
Thread-4:36
Thread-4:37
Thread-4:38
Thread-4:39
Thread-4:40
Thread-3:41
Thread-3:42
Thread-3:43
Thread-3:44
Thread-3:45
Thread-3:46
Thread-3:47
Thread-3:48
Thread-3:49
Thread-3:50

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.ios高性能编程 (1).内层 最小的内层平均值和峰值(2).耗电量 高效的算法和数据结构(3).初始化时...
    欧辰_OSR阅读 29,612评论 8 265
  • [{"reportDate": "2018-01-23 23:28:49","fluctuateCause": n...
    加勒比海带_4bbc阅读 783评论 1 2
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,948评论 18 139
  • 这个坑,从年前离京那一刻起就开始了,已经历时一个月,是我呆过最久,也最难受的一个。今天走在路上我忽然在想,这个坑好...
    艺菲_夏阅读 300评论 0 0
  • 小确幸是什么,是可以感知到的细小而又确定的幸福。你可以去发现生活中的美,找到属于自己的小确幸,也可以自己努力去创造...
    圆圆哎咿呀阅读 240评论 2 2