Nachos java版学习(二)
2024-08-29 02:19:21
threads.Lock类 提 供 了 锁 以 保 证 互 斥。
在 临 界 代 码 区 的 两 端 执 行 Lock.acquire()和Lock.release()即可保证同时只有一个线程访问临界代码区,条件变量建立在锁之上,由threads.Condition实现,它是用来保证同步的工具。每一个条件变量拥有一个锁变量 (该锁变量亦可被执行 acquire 和 release 操作, 多个条件变量可共享同一个锁变量)。当处于临界区内的拥有某锁L的当前线程对与锁L联系的条件变量执行sleep操作时,该线程失去锁 L并被挂起。下一个等待锁 L的线程获得锁 L(这个过程由调度程序完成)并进入临界区。当拥有锁 L 的临界区内的当前线程对与锁 L 联系的条件变量执行 wake 操作时 (通常调用wake之后紧接着就是Lock.release),等待在该条件变量上的至多一个被挂起的线程 (由调用sleep引起) 被重新唤醒并设置为就绪状态。若执行wakeall操作,则等待在该条件变量上的所有被挂起的线程都被唤醒。
threads.condition已经实现了一个条件变量 (采用信号量实现),题目要求用屏蔽/禁止中断的方法再实现一下条件变量(写在 threads.condition2中)
public class Condition2 {
/**
* Allocate a new condition variable.
*
* @param conditionLock
* the lock associated with this condition variable. The current
* thread must hold this lock whenever it uses <tt>sleep()</tt>,
* <tt>wake()</tt>, or <tt>wakeAll()</tt>.
*/
public Condition2(Lock conditionLock) {
this.conditionLock = conditionLock;
this.waitedThreads = new LinkedList<KThread>();
} /**
* Atomically release the associated lock and go to sleep on this condition
* variable until another thread wakes it using <tt>wake()</tt>. The current
* thread must hold the associated lock. The thread will automatically
* reacquire the lock before <tt>sleep()</tt> returns.
*/
public void sleep() {
Lib.assertTrue(conditionLock.isHeldByCurrentThread());//确定当前进程Held the Block
boolean intStatus = Machine.interrupt().disable();
waitedThreads.add(KThread.currentThread());
conditionLock.release();
KThread.sleep();
conditionLock.acquire();
Machine.interrupt().restore(intStatus);
} /**
* Wake up at most one thread sleeping on this condition variable. The
* current thread must hold the associated lock.
*/
public void wake() {
Lib.assertTrue(conditionLock.isHeldByCurrentThread());//Test if the current thread holds this lock.
boolean intStatus = Machine.interrupt().disable();
if (!waitedThreads.isEmpty())
waitedThreads.remove().ready();
Machine.interrupt().restore(intStatus);
} /**
* Wake up all threads sleeping on this condition variable. The current
* thread must hold the associated lock.
*/
public void wakeAll() {
Lib.assertTrue(conditionLock.isHeldByCurrentThread());
while (!waitedThreads.isEmpty()) {
wake();
}
} private LinkedList<KThread> waitedThreads;
private Lock conditionLock;
}
最新文章
- java分享第十六天( java读取properties文件的几种方法&;java配置文件持久化:static块的作用)
- java:使用匿名类直接new接口
- whoami 和 Who am i
- 标准模板库(STL)学习探究之stack
- Linux服务器集群系统(一)--转
- c#跨窗体调用操作
- Python Socket,How to Create Socket Server? - 网络编程实例
- 20160127 linux 学习笔记
- WISE安装程序增加注册控制
- OC弱语法
- LeetCode——Combinations
- 基于Petri网的工作流分析和移植
- C# Ioc ASP.NET MVC Dependency Injection
- 【一天一道LeetCode】#108. Convert Sorted Array to Binary Search Tree
- Python爬虫入门教程 42-100 爬取儿歌多多APP数据-手机APP爬虫部分
- ACM字符串输入问题
- ceph hammer 0.94.10手动部署方法Ceph Hammer版(0.94.10)手动部署for CentOS 7.x
- 微信小程序无法定位
- linux的时间问题
- Asp.Net Form表单控件的回车默认事件
热门文章
- /dev/null和/dev/zero的作用
- Ubuntu18.04开机动画(bootsplash)安装
- unity当两个以上Android插件冲突,怎么配置manifest
- elasticsearch update方法报错: Too many dynamic script compilations within, max: [75/5m]
- ELK系列(7) - 测试环境下Logstash异常退出:block in multi_receive_encoded
- chrome-添加JSON-handler插件
- php手记之04-tp5数据库操作
- centos7 windows7 双系统重新构建引导和启动顺序
- [转] node.js express的安装与部署,以及pm2下的运行启动。
- 002-创建型-03-单例模式(Singleton)【7种】、spring单例及原理