Java教程

java锁的应用

本文主要是介绍java锁的应用,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1、重量级锁sychronized

    public synchronized String testLock01() {
        // todo 业务逻辑
        return "test01";
    }

    public String testLock02() {
        synchronized (this) {
            System.out.println("true = " + true);
        }
        return "test02";
    }

 

2、reentrantLock

    final ReentrantLock reentrantLock = new ReentrantLock();

    public String test02() {
        reentrantLock.lock();
        System.out.println("销住的代码");
        reentrantLock.unlock();
        return "test02";
    }

 

这篇关于java锁的应用的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!