美文网首页
synchronized 同步语句块和同步方法区别

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

作者: 虾米爱螃蟹 | 来源:发表于2018-08-10 12:50 被阅读0次
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

相关文章

网友评论

      本文标题:synchronized 同步语句块和同步方法区别

      本文链接:https://www.haomeiwen.com/subject/xvukbftx.html