本文主要是介绍【十八掌●基本功篇】第一掌:Java之多线程--锁,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
这一篇博文是【大数据技术●降龙十八掌】系列文章的其中一篇,点击查看目录:大数据技术●降龙十八掌
synchronized可以实现同步访问,java.util.concurrent.locks包下的Lock也可以实现同步访问。
synchronized是java中的一个关键字,是Java语言内置的特性。处于synchronized作用下的代码块或者方法代码块,是不允许两个线程同时进入,不管是读操作还是写操作,都是不允许同时进行的,这在高并发的情况下对性能影响很大。而Lock更复杂,控制也更精细,另外Lock提供的功能更丰富。
1、Lock
Lock是一个接口,定义如下:
public interface Lock {void lock();void lockInterruptibly() throws InterruptedException;boolean tryLock();boolean tryLock(long time, TimeUnit unit) throws InterruptedException;void unlock();Condition newCondition();
}
方法 | 说明 |
---|---|
lock() | 获取锁,如果锁被其他线程占用了,就等待 |
tryLock() | 尝试获取锁,如果获取成功就返回true,如果获取失败,就返回false,这个不同于lock(),tryLock(),获取不到锁也会立即返回,而不是等待 |
unlock() | 释放锁,因为锁不会自动释放,所以通常将unlock()方法放到finally里,以保证肯定会被释放掉 |
2、ReadWriteLock
ReadWriteLock是一个接口,它定义了读锁和写锁,可以使得多个线程可以同时读。定义如下:
public interface ReadWriteLock {Lock readLock();Lock writeLock();
}
3、ReentrantReadWriteLock
ReentrantReadWriteLock是个类,它实现了ReadWriteLock接口。在应用读锁和写锁时候,如果一个线程占用了写锁,那么其他线程不能获取写锁也不能占用读锁,只有写完其他线程才能读和写,当一个线程占用了读锁,那么其他线程可以获取读锁,但是不能占用写锁,也就是只能读不能写。这样就比synchronized提高了读取时候的效率,因为synchronized作用下,不管读取还是写入,都必须一个线程一个线程地进行。
下面是一个ReentrantReadWriteLock的应用实例,模拟多个客户端对同一个银行账号进行操作,操作有:存钱、取钱、查询余额,保证存钱和取钱这两个写操作不能同时进行,但是在没有写入的时候,多个查询余额操作可以同时进行。
package lock;import java.util.Date;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;/*** Created by 鸣宇淳 on 2017/12/19.* 多客户端对银行账号进行存取查询的例子*/
public class MyLockDemo {public static void main(String[] args) throws InterruptedException {//创建银行账户Account account = new Account("6222022222212154578", 1000);//创建一个锁ReadWriteLock lock = new ReentrantReadWriteLock(false);//三个客户端同时操作Client client1 = new Client("存钱1", 1, account, 500, lock);Client client2 = new Client("取钱2", 2, account, 800, lock);Client client3 = new Client("查询3", 3, account, 0, lock);Client client4 = new Client("查询4", 3, account, 0, lock);Client client5 = new Client("存钱5", 1, account, 100, lock);Thread t1 = new Thread(client1);Thread t2 = new Thread(client2);Thread t3 = new Thread(client3);Thread t4 = new Thread(client4);Thread t5 = new Thread(client5);System.out.println(new Date() + "账户内起始金额为1000\n---------------");t1.start();t2.start();t3.start();t4.start();t5.start();}
}package lock;import java.util.Date;
import java.util.concurrent.locks.ReadWriteLock;/*** Created by 鸣宇淳 on 2017/12/19.、* 存取客户端*/
public class Client implements Runnable {private String name; //客户端操作人private int saveOrWithdrawOrGet;//1.存钱 2.取钱 3.查询余额private ReadWriteLock lock;//锁private Account account;//操作账号private int num;//存取金额public Client(String name, int saveOrWithdrawOrGet, Account account, int num, ReadWriteLock lock) {this.name = name;this.saveOrWithdrawOrGet = saveOrWithdrawOrGet;this.lock = lock;this.account = account;this.num = num;}public void run() {if (this.saveOrWithdrawOrGet == 1) {//存钱try {System.out.println(new Date() +this.name + "to....申请锁存钱");lock.writeLock().lock();int r = account.saveMoney(this.num);System.out.println(new Date() +this.name + "存钱" + this.num + ",余额为" + r);Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();} finally {lock.writeLock().unlock();}} else if (this.saveOrWithdrawOrGet == 2) {//取钱try {System.out.println(new Date() +this.name + "to....申请锁取钱");lock.writeLock().lock();int r = account.withdrawMoney(num);System.out.println(new Date() + this.name + "取钱" + this.num + ",余额为" + r);Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();} finally {lock.writeLock().unlock();}} else if (this.saveOrWithdrawOrGet == 3) {//查询余额try {System.out.println(new Date() +this.name + "to....申请锁查询");lock.readLock().lock();int r = account.getMoney();System.out.println(new Date() +this.name + "查询余额,余额为" + r);Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();} finally {lock.readLock().unlock();}}}
}package lock;/*** Created by 鸣宇淳 on 2017/12/19.* 银行账户*/
public class Account {private String cardcode;private int money;public Account(String cardcode, int money) {this.cardcode = cardcode;this.money = money;}//存钱public int saveMoney(int num) {this.money = this.money + num;return this.money;}//取钱public int withdrawMoney(int num) {if (num <= this.money) {this.money = this.money - num;}return this.money;}//查询余额public int getMoney() {return this.money;}
}
运行以上代码,输入如下:
Thu Dec 28 11:09:22 CST 2017账户内起始金额为1000
---------------
Thu Dec 28 11:09:22 CST 2017取钱2to....申请锁取钱
Thu Dec 28 11:09:22 CST 2017存钱5to....申请锁存钱
Thu Dec 28 11:09:22 CST 2017查询3to....申请锁查询
Thu Dec 28 11:09:22 CST 2017查询4to....申请锁查询
Thu Dec 28 11:09:22 CST 2017存钱1to....申请锁存钱
Thu Dec 28 11:09:22 CST 2017存钱5存钱100,余额为1100
Thu Dec 28 11:09:24 CST 2017取钱2取钱800,余额为300
Thu Dec 28 11:09:26 CST 2017查询3查询余额,余额为300
Thu Dec 28 11:09:26 CST 2017查询4查询余额,余额为300
Thu Dec 28 11:09:28 CST 2017存钱1存钱500,余额为800
通过输出可以看到,客户端12345同时申请各自的锁,存储5客户端11:09:22首先抢到了写锁,进行存钱操作后休眠了2秒,这个过程中,其他线程不管是申请写锁或者读锁,都不能得到锁,2秒后11:09:24取钱2客户端抢到了写锁,进程取钱操作后,休眠2秒,这个过程中同样其他线程不能读也不能写,11:09:26查询3客户端得到读锁,然后也休眠2秒,这个过程,查询4可以得到赌说,但是存钱1不能得到写锁,也就是只能读不能写,只有当2秒后,查询3和查询4都释放了读锁后,存钱1才能抢到写锁,进行存钱操作。
这篇关于【十八掌●基本功篇】第一掌:Java之多线程--锁的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!