欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java面试--Synchronized和Lock区别

程序员文章站 2022-05-04 20:54:24
...

面试题:Synchronized和Lock区别(大疆面试题、网易笔试题)

一、Synchronized和Lock区别
Java面试--Synchronized和Lock区别
二、Lock源码

public interface Lock {
    //获取锁,如果锁被暂用则一直等待
    void lock();
    //用该锁的获得方式,如果线程在获取锁的阶段进入了等待,那么可以中断此线程,先去做别的事  
    void lockInterruptibly() throws InterruptedException;
    //注意返回类型是boolean,如果获取锁的时候锁被占用就返回false,否则返回true
    boolean tryLock();
    //比起tryLock()就是给了一个时间期限,保证等待参数时间
    boolean tryLock(long time, TimeUnit unit) throws InterruptedException;
    //释放锁
    void unlock();
}

下面是Lock一般使用的例子,注意ReentrantLock是Lock接口的实现:

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class LockTest {
    private Lock lock = new ReentrantLock();
    //需要参与同步的方法
    private void method(Thread thread){
        lock.lock();
        try {
            System.out.println("线程名"+thread.getName() + "获得了锁");
        }catch(Exception e){
            e.printStackTrace();
        } finally {
            System.out.println("线程名"+thread.getName() + "释放了锁");
            lock.unlock();
        }
    }

    public static void main(String[] args) {
        LockTest lockTest = new LockTest();
        //线程1
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                lockTest.method(Thread.currentThread());
            }
        }, "t1");

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

        t1.start();
        t2.start();
    }
}
//运行结果
//线程名t1获得了锁
//线程名t1释放了锁
//线程名t2获得了锁
//线程名t2释放了锁

tryLock()方法:

package com.brickworkers;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class LockTest {
    private Lock lock = new ReentrantLock();

    //需要参与同步的方法
    private void method(Thread thread){
        if(lock.tryLock()){
            try {
                System.out.println("线程名"+thread.getName() + "获得了锁");
            }catch(Exception e){
                e.printStackTrace();
            } finally {
                System.out.println("线程名"+thread.getName() + "释放了锁");
                lock.unlock();
            }
        }else{
            System.out.println("我是"+Thread.currentThread().getName()+"有人占着锁,我就不要啦");
        }
    }

    public static void main(String[] args) {
        LockTest lockTest = new LockTest();
        //线程1
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                lockTest.method(Thread.currentThread());
            }
        }, "t1");

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

        t1.start();
        t2.start();
    }
}
//运行结果:
//线程名t2获得了锁
//我是t1有人占着锁,我就不要啦
//线程名t2释放了锁

Lock中可以自己控制锁是否公平,而且,默认的是非公平锁,以下是ReentrantLock的构造函数:

public ReentrantLock() {
        sync = new NonfairSync();//默认非公平锁
}

转载请标明出处,原文地址:https://blog.csdn.net/weixin_41835916 如果觉得本文对您有帮助,请点击支持一下,您的支持是我写作最大的动力,谢谢。
Java面试--Synchronized和Lock区别