Java Concurrent Lock ReentrantLock简介

Java concurrent包下面的lock功能介绍,摘自java核心编程第八绻 多线程 章节

直接上代码:

Listing 14–8 Bank.java
1. import java.util.concurrent.locks.*;
2.
3. /**
4. * A bank with a number of bank accounts that uses locks for serializing access.
5. * @version 1.30 2004-08-01
6. * @author Cay Horstmann
7. */
8. public class Bank
9. {
10. /**
11. * Constructs the bank.
12. * @param n the number of accounts
13. * @param initialBalance the initial balance for each account
14. */
15. public Bank(int n, double initialBalance)
16. {
17. accounts = new double[n];
18. for (int i = 0; i < accounts.length; i++)
19. accounts[i] = initialBalance;
20. bankLock = new ReentrantLock();
21. sufficientFunds = bankLock.newCondition();
22. }
23.
24. /**
25. * Transfers money from one account to another.
26. * @param from the account to transfer from
27. * @param to the account to transfer to
28. * @param amount the amount to transfer
29. */
30  public void transfer(int from, int to, double amount) throws InterruptedException
31. {
32. bankLock.lock();
33. try
34. {
35. while (accounts[from] < amount)
36. sufficientFunds.await();
37. System.out.print(Thread.currentThread());
38. accounts[from] -= amount;
39. System.out.printf(" %10.2f from %d to %d", amount, from, to);
40. accounts[to] += amount;
41. System.out.printf(" Total Balance: %10.2f%n", getTotalBalance());
42. sufficientFunds.signalAll();
43. }
44. finally
45. {
46. bankLock.unlock();
47. }
48. }
49.
50. /**
51. * Gets the sum of all account balances.
52. * @return the total balance
53. */
54. public double getTotalBalance()
55. {
56. bankLock.lock();
57. try
58. {
59. double sum = 0;
60.
61. for (double a : accounts)
62. sum += a;
63.
64. return sum;
65. }
66. finally
67. {
68. bankLock.unlock();
69. }
70. }
71.
72. /**
73. * Gets the number of accounts in the bank.
74. * @return the number of accounts
75. */
76. public int size()
77. {
78. return accounts.length;
79. }
80.
81. private final double[] accounts;
82. private Lock bankLock;
83. private Condition sufficientFunds;
84. }

(1)ReentrantLock 可重入锁,使用lock()和unlock()来获取和释放锁

(2)newCondition() 创建可以使用wait(),notify()等同方法的对象,该对象可以使用的方法

void await()
puts this thread on the wait set for this condition.
• void signalAll()
unblocks all threads in the wait set for this condition.
• void signal()
unblocks one randomly selected thread in the wait set for this condition.

使用signalAll()方法,激活调用await()方法阻塞的线程,线程恢复以后,将从调用await方法的地方,继续往下执行。

(3)如果可以使用synchronized,wait,notify等方法,满足我们条件的话,尽量使用这些,因为这样代码量少,而且不容易出错。



Read、Write Lock

1. Construct a ReentrantReadWriteLock object:
private ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
2. Extract read and write locks:
private Lock readLock = rwl.readLock();
private Lock writeLock = rwl.writeLock();

• Lock readLock()
gets a read lock that can be acquired by multiple readers, excluding all writers.
• Lock writeLock()
gets a write lock that excludes all other readers and writers.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值