Art of Multiprocessor Programming 答案 ch8 p95
来源:互联网 发布:阿里云系统刷安卓系统 编辑:程序博客网 时间:2024/04/27 23:08
public class Account { /**优先存款方式*/ public static final int PREFER = 1; /**普通存款方式*/ public static final int NORMAL = 2; private int surplus;//结余 private int preferRequests; private Lock lock = new ReentrantLock(); private Condition condition = lock.newCondition(); public Account() { surplus = 0; preferRequests = 0; } /** * 结余 += k * * @param k */ public void deposit(int k) { lock.lock(); try { surplus += k; System.out.println("Thread " + Thread.currentThread().getName() + " deposit "+ k); condition.signalAll(); } finally { lock.unlock(); } } /** * 当结余 >= k 时,结余 -= k * * @param k */ public void withdraw(int k, int style) { lock.lock(); try { if (style == NORMAL) { //preferRequests>0 表示有优先的取款在等待,则普通取款不能进行下去 while (surplus < k || preferRequests > 0) { condition.await(); } surplus -= k; System.out.println("Normale Thread " + Thread.currentThread().getName() + " withdraw "+ k); } else if (style == PREFER) { ++preferRequests; while (surplus < k) { condition.await(); } surplus -= k; --preferRequests; System.out.println("Prefer Thread " + Thread.currentThread().getName() + " withdraw "+ k); condition.signalAll(); } else { throw new InterruptedException(); } } catch (InterruptedException e) { e.printStackTrace(); condition.signalAll(); } finally { lock.unlock(); } } /** * 向另一个账户转账 * @param k 转账金额 * @param reserve */ public void transfer(int k,Account reserve){ lock.lock(); try { reserve.withdraw(k,NORMAL); deposit(k); }finally { lock.unlock(); } }}
private static void test_account() { Account[] accounts = new Account[2]; for (int i = 0; i < accounts.length; ++i) { accounts[i] = new Account(); } TestAccountThread tester1 = new TestAccountThread(accounts[0], accounts[1]); TestAccountThread tester2 = new TestAccountThread(accounts[1], accounts[0]); tester1.start(); tester2.start(); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("After sleep "); for (Account account : accounts) { account.deposit(1000); } }
(3)不一定全部会返回,因为可能在其他线程的condition上死锁,boss notify自己的锁线程感应不到。
测试结果:
线程tester1,tester2转账均失败,出现死锁
0 0
- Art of Multiprocessor Programming 答案 ch8 p95
- Art of Multiprocessor Programming 答案 ch8
- Art of Multiprocessor Programming 答案 ch8 p93
- Art of Multiprocessor Programming 答案 ch8 p96
- Art of Multiprocessor Programming 答案 ch8 p97
- Art of Multiprocessor Programming 答案 ch3
- Art of Multiprocessor Programming 答案 ch2
- Art of Multiprocessor Programming 答案 ch4
- Art of Multiprocessor Programming 答案 ch5
- Art of Multiprocessor Programming 答案 ch6
- Art of Multiprocessor Programming 答案 ch7
- Art of Multiprocessor Programming 答案 ch9
- Art of Multiprocessor Programming 答案 ch10
- Art of Multiprocessor Programming 答案 ch11
- Art of Multiprocessor Programming 答案 ch12
- Art of Multiprocessor Programming 答案 ch13
- Art of Multiprocessor Programming 答案 ch14
- Art of Multiprocessor Programming 答案 ch15
- HDU ACM 4510 小Q系列故事——为什么时光不能倒流
- hg
- Java Web利用POI导出Excel简单例子
- MySQL 5.7.7编译安装过程
- android BatteryManager电池管理
- Art of Multiprocessor Programming 答案 ch8 p95
- WIFI基本知识
- tnsping用法
- Android Studio导入项目
- 杭电ACM 2029:Palindromes _easy version
- java多线程并发处理之阻塞队列LinkedBlockingQueue用法
- 数组的简单处理方式
- 操作系统概念学习笔记 8 进程
- 利用caffe训练网络的步骤