赞
踩
参考原文链接:Java synchronized类锁,对象锁详解(转载整理)_student100000的博客-CSDN博客_java synchronized 类锁
目录
- public class Demo implements Runnable{
-
- @Override
- public void run() {
- /**
- * 同步代码块形式--->锁为this,两个线程使用的锁是一样的,
- * 线程1必须要等到线程2释放了该锁后,才能执行
- */
- synchronized (this) {
- System.out.println(Thread.currentThread().getName() + "开始执行");
- try {
- Thread.sleep(2000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println(Thread.currentThread().getName() + "执行结束");
- }
- }
-
- public static void main(String[] args) {
- Demo demo = new Demo();
- new Thread(demo,"线程1").start();
- new Thread(demo,"线程2").start();
- }
- }

代码块中的synchronized(this),指获取类Demo创建的实例对象demo中的内置锁(锁定当前代码块执行者),可以看到线程2必须要等到线程1释放了该锁后,才能执行。
- public class Demo2 implements Runnable{
-
- @Override
- public void run() {
- method();
- }
-
- public synchronized void method(){
- System.out.println(Thread.currentThread().getName() + "开始执行");
- try {
- Thread.sleep(2000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println(Thread.currentThread().getName() + "执行结束");
- }
-
- public static void main(String[] args) {
- Demo2 demo2 = new Demo2();
- new Thread(demo2,"线程1").start();
- new Thread(demo2,"线程2").start();
- }
- }

synchonized指定类demo2创建的实例对象demo2的内置锁(锁定当前方法调用者),可以看到线程2必须等到线程1释放了该锁后,才能执行。
- public class Demo2 implements Runnable{
-
- @Override
- public void run() {
- method();
- }
-
- /**
- * synchronized用在普通方法上,默认的所就是this,当前实例
- */
- public synchronized void method(){
- System.out.println(Thread.currentThread().getName() + "开始执行");
- try {
- Thread.sleep(2000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println(Thread.currentThread().getName() + "执行结束");
- }
-
- public static void main(String[] args) {
- Demo2 t1 = new Demo2();
- Demo2 t2 = new Demo2();
- new Thread(t1,"线程1").start();
- new Thread(t2,"线程2").start();
- }
- }

或
可能还有其他执行结果。
可以看到线程1和线程2的执行顺序不能保证,因为这两个线程获取的Demo2类的不同实例对象的不同内置锁。
- public class Demo3 implements Runnable{
-
- @Override
- public void run() {
- method();
- }
-
- /**
- * synchronized用在静态方法上,默认锁的就是当前所在的Class类,
- * 所以无论是哪个线程访问它,需要的锁都只有一把
- */
- public static synchronized void method(){
- System.out.println(Thread.currentThread().getName() + "执行了");
- try {
- Thread.sleep(2000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println(Thread.currentThread().getName() + "结束了");
- }
-
- public static void main(String[] args) {
- Demo3 t1 = new Demo3();
- Demo3 t2 = new Demo3();
- new Thread(t1,"线程1").start();
- new Thread(t2,"线程2").start();
-
- }
- }

- 线程1执行了
- 线程1结束了
- 线程2执行了
- 线程2结束了
上面这个例子,虽然类Demo3有两个不同的实例,但结果总是线程1先执行,然后线程2再执行。这是因为静态方法获取的是Demo3的类锁,和实例无关。且类锁只有一个,所以实现了同步。
- public class Demo implements Runnable{
-
- @Override
- public void run() {
- /**
- * 所有线程需要的锁都是同一把
- */
- synchronized (Demo.class) {
- System.out.println(Thread.currentThread().getName() + "开始执行");
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println(Thread.currentThread().getName() + "执行结束");
- }
- }
-
- public static void main(String[] args) {
- Demo t1 = new Demo();
- Demo t2 = new Demo();
- new Thread(t1,"线程1").start();
- new Thread(t2,"线程2").start();
- }
- }

获取的是类Demo的类锁
java的内置锁:每个Java对象都可以用做一个实现同步的锁,这些锁称为内置锁。线程进入同步代码块或方法的时候会自动获得该锁,在退出同步代码块或方法时会释放该锁。获得内置锁的唯一途径就是进入这个锁的保护的同步代码块或方法。
java内置锁是一个互斥锁,这就是意味着最多只有一个线程能够获得该锁,当线程A尝试去获得线程B持有的内置锁时,线程A必须等待或者阻塞,直到线程B 释放这个锁,如果线程B不释放这个锁,那么线程A将永远等待下去。
Java的对象锁和类锁:Java的对象锁和类锁在锁的概念上基本上和内置锁是一致的,但是两个锁实际是有很大区别的,对象锁是用于对象实例方法,或者一个对象实例上的,类锁是用与类的静态方法或者一个类的class对象上的。我们知道,类的对象实例可以有很多个,但是每个类只有一个class对象,所以不同对象实例的对象锁是互不干扰的,但是每个类只有一个类锁。但是有一点必须注意的是,其实类锁只是一个概念上的东西,并不是真实存在的,它只是用来帮助我们理解锁定实例方法和静态方法的区别的。
- public class TestSynchronized {
-
- public void test1(){
- /**
- * synchronized修饰同步代码块
- */
- synchronized (this){
- for (int i = 1;i < 6;i++){
- System.out.println(Thread.currentThread().getName() + " : " + i);
- try {
- Thread.sleep(500);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
-
- /**
- * synchronized修饰非静态方法
- */
- public synchronized void test2(){
- for (int i = 1;i < 6;i++){
- System.out.println(Thread.currentThread().getName() + " : " + i);
- try {
- Thread.sleep(500);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
-
- public static void main(String[] args) {
- TestSynchronized demo = new TestSynchronized();
- Thread test1 = new Thread(new Runnable() {
- @Override
- public void run() {
- demo.test1();
- }
- },"test1");
- Thread test2 = new Thread(new Runnable() {
- @Override
- public void run() {
- demo.test2();
- }
- },"test2");
- test1.start();
- test2.start();
- }
- }

上述的代码,第一个方法用了同步代码块的方式进行同步,传入的对象实例是this,表明是当前对象;第二个方法是修饰方法的方式进行同步。因为第一个同步代码块传入的this,所以两个同步代码所需要的对象锁都是同一个TestSynchronized对象的锁,下面main方法开启两个线程,分别别调用test1()和test2()方法,那么两个线程都需要获得该对象锁,另一个线程必须等待。上面也给出了运行的结果可以看到:直到test1执行完毕,释放掉锁,test2线程才开始执行。
把test2方法的synchronized关键字去掉,执行结果会如何呢?
- test2 : 1
- test1 : 1
- test2 : 2
- test1 : 2
- test2 : 3
- test1 : 3
- test1 : 4
- test2 : 4
- test1 : 5
- test2 : 5
上面是执行结果,可以看到,结果输出是交替着进行输出的,这是因为,某个线程得到了对象锁,但是另一个线程还是可以访问没有进行同步的方法或者代码。进行了同步的方法(加锁方法)和没有进行同步的方法(普通方法)是互不影响的,一个线程进入了同步方法,得到了对象锁,其他线程还是可以访问那些没有同步的方法(普通方法)。原因在于:对象的内置锁和对象的状态之间是没有内在的关联的。当获取到与对象关联的内置锁时,并不能阻止其他线程访问该对象,当某个线程获得对象的锁之后,只能阻止其他线程获得同一个锁。
总结:synchronized只是一个内置锁的加锁机制,当某个方法加上synchronized关键字后,就表明要获得该内置锁才能执行,但并不能阻止其他线程访问不需要获得该内置锁的方法。
- public class TestSynchronized {
-
- public void test1(){
- /**
- * synchronized修饰同步代码块,
- * 传的是一个class对象
- */
- synchronized (TestSynchronized.class){
- for (int i = 1;i < 6;i++){
- System.out.println(Thread.currentThread().getName() + " : " + i);
- try {
- Thread.sleep(500);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
-
- /**
- * synchronized修饰静态方法
- */
- public static synchronized void test2(){
- for (int i = 1;i < 6;i++){
- System.out.println(Thread.currentThread().getName() + " : " + i);
- try {
- Thread.sleep(500);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
-
- public static void main(String[] args) {
- TestSynchronized demo = new TestSynchronized();
- Thread test1 = new Thread(new Runnable() {
- @Override
- public void run() {
- demo.test1();
- }
- },"test1");
- Thread test2 = new Thread(new Runnable() {
- @Override
- public void run() {
- TestSynchronized.test2();
- }
- },"test2");
- test1.start();
- test2.start();
- }
- }

- test1 : 1
- test1 : 2
- test1 : 3
- test1 : 4
- test1 : 5
- test2 : 1
- test2 : 2
- test2 : 3
- test2 : 4
- test2 : 5
可以看到线程2要在线程1执行完之后,才开始执行,这是因为两个synchronized加的都是类锁,而且是同一个类的锁,一个类只有一把锁,所以加的是同一个类的同一把锁,所以要等待
- public class TestSynchronized {
-
- /**
- * synchronized修饰非静态方法
- */
- public synchronized void test1(){
- for (int i = 1;i < 6;i++){
- System.out.println(Thread.currentThread().getName() + " : " + i);
- try {
- Thread.sleep(500);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
-
- /**
- * synchronized修饰静态方法
- */
- public static synchronized void test2(){
- for (int i = 1;i < 6;i++){
- System.out.println(Thread.currentThread().getName() + " : " + i);
- try {
- Thread.sleep(500);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
-
- public static void main(String[] args) {
- TestSynchronized demo = new TestSynchronized();
- Thread test1 = new Thread(new Runnable() {
- @Override
- public void run() {
- demo.test1();
- }
- },"test1");
- Thread test2 = new Thread(new Runnable() {
- @Override
- public void run() {
- TestSynchronized.test2();
- }
- },"test2");
- test1.start();
- test2.start();
- }
- }

- test1 : 1
- test2 : 1
- test2 : 2
- test1 : 2
- test2 : 3
- test1 : 3
- test2 : 4
- test1 : 4
- test1 : 5
- test2 : 5
上面代码synchronized同时修饰静态方法和实例方法,但是运行结果是交替进行的,这证明了类锁和对象锁是两个不一样的锁,控制着不同的区域,它们是互不干扰的。同样,线程获得对象锁的同时,也可以获得该类锁,即同时获得两个锁,这是允许的。
既然有了synchronized修饰方法的同步方式,为什么还需要synchronized修饰同步代码块的方式呢?
synchronized的缺陷:当某个线程进入同步方法获得对象锁,那么其他线程访问这里对象的同步方法时,必须等待或者阻塞,如果这个方法代码比较多,那等待线程的时间就会比较长,直到当前执行方法的线程执行完方法,释放锁,等待线程才可以获得锁去执行。如果使用同步代码块,它可以灵活的选择要同步的代码,而不用整个方法都加锁,这样可以提高代码执行效率,类似于Lock的lock()方法和unLock()方法优点。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。