栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java 多线程Synchronized和Lock的区别

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java 多线程Synchronized和Lock的区别

引言

  在多线程中,为了使线程安全,我们经常会使用synchronized和Lock进行代码同步和加锁,但是具体两者有什么区别,什么场景下适合用什么可能还不大清楚,主要的区别大致如下:

区别

    1、synchronized是java关键字,而Lock是java中的一个接口

    2、synchronized会自动释放锁,而Lock必须手动释放锁

    3、synchronized是不可中断的,Lock可以中断也可以不中断

    4、通过Lock可以知道线程有没有拿到锁,而synchronized不能

    5、synchronized能锁住方法和代码块,而Lock只能锁住代码块

    6、Lock可以使用读锁提高多线程读效率

    7、synchronized是非公平锁,ReentranLock可以控制是否公平锁

从Lock接口中我们可以看到主要有5个方法,这些方法的功能从注释中可以看出:

lock():获取锁,如果锁被暂用则一直等待
unlock():释放锁
tryLock(): 注意返回类型是boolean,如果获取锁的时候锁被占用就返回false,否则返回true
tryLock(long time, TimeUnit unit):比起tryLock()就是给了一个时间期限,保证等待参数时间
lockInterruptibly():用该锁的获得方式,如果线程在获取锁的阶段进入了等待,那么可以中断此线程,先去做别的事   通过 以上的解释,大致可以解释在上个部分中“锁类型(lockInterruptibly())”,“锁状态(tryLock())”等问题,还有就是前面子所获取的过程我所写的“大致就是可以尝试获得锁,线程可以不会一直等待”用了“可以”的原因。
lock():
public class LockTest {
  private Lock lock = new ReentrantLock();

  private void method(Thread thread) {
    lock.lock();
    try {
      System.out.println(thread.getName() + " has gotten the lock!");
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      System.out.println(thread.getName() + " has unlocked the lock!");
      lock.unlock();
    }
  }

  public static void main(String[] args) {
    final LockTest test = new LockTest();

    Thread t1 = new Thread(new Runnable() {
      @Override
      public void run() {
 test.method(Thread.currentThread());
      }
    }, "t1");
    Thread t2 = new Thread(new Runnable() {
      @Override
      public void run() {
 test.method(Thread.currentThread());
      }
    }, "t2");
    t1.start();
    t2.start();
  }

}

运行结果:

t1 has gotten the lock!
t1 has unlocked the lock!
t2 has gotten the lock!
t2 has unlocked the lock!
tryLock():
public class LockTest {
  private Lock lock = new ReentrantLock();

  private void method(Thread thread) {

    if (lock.tryLock()) {
      lock.lock();
      try {
 System.out.println(thread.getName() + " has gotten the lock!");
      } catch (Exception e) {
 e.printStackTrace();
      } finally {
 System.out.println(thread.getName() + " has unlocked the lock!");
 lock.unlock();
      }
    } else {
      System.out.println("I'm "+thread.getName()+". Someone has gotten the lock!");
    }
  }

  public static void main(String[] args) {
    LockTest test = new LockTest();

    Thread t1 = new Thread(() -> test.method(Thread.currentThread()), "t1");
    Thread t2 = new Thread(new Runnable() {
      @Override
      public void run() {
 test.method(Thread.currentThread());
      }
    }, "t2");
    t1.start();
    t2.start();
  }
}

运行结果:

t1 has gotten the lock!
t1 has unlocked the lock!
I'm t2. Someone has gotten the lock!

看到这里相信大家也都会使用如何使用Lock了吧,关于tryLock(long time, TimeUnit unit)和lockInterruptibly()不再赘述。前者主要存在一个等待时间,在测试代码中写入一个等待时间,后者主要是等待中断,会抛出一个中断异常,常用度不高,喜欢探究可以自己深入研究。

以上就是Java 多线程Synchronized和Lock的区别的详细内容,更多关于Java 多线程Synchronized和Lock的资料请关注考高分网其它相关文章!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/129605.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号