Java面试题

本文深入探讨了Java中的同步机制,包括synchronized、volatile、Lock及ReentrantLock等关键字和接口的使用场景与区别,并通过示例代码说明了这些同步工具的工作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

52.说一下 synchronized 底层实现原理?

synchronized确保在运行时的任何时候只有一个方法或代码块可以进入临界区,还确保共享变量的内存可见性。  

Java中每一个对象都可以作为锁,这是synchronized实现同步的基础:

①普通同步方法,锁是当前实例对象。

示例:

public class Test42 implements Runnable {
    int count = 0;

    @Override
    public synchronized void run() {
        for (int i = 0; i < 5; i++) {
            System.out.println(Thread.currentThread().getName() + ":" + count++);
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Test42 test1 = new Test42();
        Test42 test2 = new Test42();
        Thread thread1 = new Thread(test1, "thread1");
        Thread thread2 = new Thread(test2, "thread2");
        thread1.start();
        thread2.start();
    }
}

②静态同步方法,锁是当前类的class对象。

示例:

public class Test43 implements Runnable {
    static int count = 0;

    @Override
    public synchronized void run() {
        increase();
    }

    private synchronized static void increase() {
        for (int i = 0; i < 5; i++) {
            System.out.println(Thread.currentThread().getName() + ":" + count++);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Test43 test1 = new Test43();
        Test43 test2 = new Test43();
        Thread thread1 = new Thread(test1, "thread1");
        Thread thread2 = new Thread(test2, "thread2");
        thread1.start();
        thread2.start();
    }
}

③同步方法块,锁是括号里面的对象。

示例:

public class Test44 implements Runnable {
    static int count = 0;

    @Override
    public synchronized void run() {
        increase();
    }

    private void increase() {
        synchronized (this) {
            for (int i = 0; i < 5; i++) {
                System.out.println(Thread.currentThread().getName() + ":" + count++);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Test44 test1 = new Test44();
        Test44 test2 = new Test44();
        Thread thread1 = new Thread(test1, "thread1");
        Thread thread2 = new Thread(test2, "thread2");
        thread1.start();
        thread2.start();
    }
}

53. synchronized 和 volatile 的区别是什么?

①synchronized可以作用于变量、方法、对象;volatile只能作用于变量。②synchronized可以保证线程间变量的修改可见性和原子性;volatile只保证变量的修改可见性,无法保证原子性。③synchronized线程阻塞,volatile线程不阻塞。④synchronized标记的变量可以被编译器优化;volatile标记的变量不会被编译器优化。

54. synchronized 和 Lock 有什么区别?

①Synchronized是关键字,内置语言实现,Lock是接口。②Synchronized在线程发生异常时会自动释放锁,因此不会发生异常死锁。Lock异常时不会自动释放锁,所以需要在finally中实现释放锁。③Lock是可以中断锁,Synchronized是非中断锁,必须等待线程执行完成释放锁。④Lock锁适合大量同步的代码的同步问题,synchronized锁适合代码少量的同步问题。

55. synchronized 和 ReentrantLock 区别是什么?

①synchronized是JVM 层面实现的;ReentrantLock是JDK 代码层面实现。②synchronized竞争锁时会一直等待;ReentrantLock可以尝试获取锁,并得到获取结果。③synchronized 获取锁无法设置超时;ReentrantLock 可以设置获取锁的超时时间。④synchronized在加锁代码块执行完或者出现异常,自动释放锁;ReentrantLock不会自动释放锁,需要在 finally{}代码块显示释放。

synchronized示例:

public static void main(String[] args) {
    Test45 t1 = new Test45();
    Thread thread1 = new Thread((Runnable) t1, "thread1");
    Thread thread2 = new Thread((Runnable) t1, "thread2");
    thread1.start();
    thread2.start();
}

@Override
public void run() {
    synchronized (this) {
        for (int i = 0; i < 5; i++)
            System.out.println(Thread.currentThread().getName() + ":" + i);
    }
}

ReentrantLock示例:

public static void main(String[] args) {
    Test45 t1 = new Test45();
    Thread thread1 = new Thread((Runnable) t1, "thread1");
    Thread thread2 = new Thread((Runnable) t1, "thread2");
    thread1.start();
    thread2.start();
}

private Lock lock = new ReentrantLock();

public void run() {
    lock.lock();
    try {
        for (int i = 0; i < 5; i++)
            System.out.println(Thread.currentThread().getName() + ":" + i);
    } finally {
        lock.unlock();
    }
}

56. 说一下 atomic 的原理?

Atomic包中的类基本的特性就是在多线程环境下将属性设置为atomic可确保数据的一致读取  。他会保证数据只能被一个线程占用,也就是说当一个线程写一个属性的时候,它会用自旋锁锁定这个属性,其他线程不允许读取。

示例:

static AtomicInteger ai = new AtomicInteger(1);

public static void main(String[] args) {
    System.out.println(ai.getAndIncrement());
    System.out.println(ai.get());
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值