Careercup - Microsoft面试题 - 6366101810184192
2014-05-10 22:30
原题:
Design database locks to allow r/w concurrency and data consistency.
题目:设计一个支持并行读写并保证数据一致性的数据库锁。
解法:这是什么范畴的面试题?对于我这种完全没有相关项目经验的人,还真是无从下手。翻了书之后顺便重新复习了一下共享锁、互斥锁的概念。说白了,这些都是课本上的基础知识,可是毕业没多久就忘光了。看来知识不用,保质期比水果还短。然后我琢磨着锁的模型,写了个模拟的代码。这个代码和SQL毫无关系,可能也和正确答案相去甚远。姑妄言之,姑妄听之吧。
代码:
// http://www.careercup.com/question?id=6366101810184192
import java.util.concurrent.Semaphore; public class FooBar {
public static final int MAX_CONCURRENT_READ = 100;
private Semaphore sharedSemaphore;
private Semaphore exclusiveSemaphore; public FooBar() {
// TODO Auto-generated constructor stub
this.sharedSemaphore = new Semaphore(MAX_CONCURRENT_READ);
this.exclusiveSemaphore = new Semaphore(1);
} public void reader() {
// The reader here is not to return a value, but to perform read()
// action. Thus it is 'void reader()'.
while (exclusiveSemaphore.availablePermits() < 1) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} try {
sharedSemaphore.acquire();
System.out.println("Performing read() operation.");
sharedSemaphore.release();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public void writer() {
while (exclusiveSemaphore.availablePermits() < 1
&& sharedSemaphore.availablePermits() < MAX_CONCURRENT_READ) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} try {
exclusiveSemaphore.acquire();
System.out.println("Performing write() operation.");
exclusiveSemaphore.release();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public static void main(String[] args) {
FooBar fooBar = new FooBar(); fooBar.reader();
fooBar.writer();
}
}
Careercup - Microsoft面试题 - 6366101810184192的更多相关文章
- Careercup - Microsoft面试题 - 6314866323226624
2014-05-11 05:29 题目链接 原题: Design remote controller for me. 题目:设计一个遥控器. 解法:遥控什么?什么遥控?传统的红外线信号吗?我只能随便说 ...
- Careercup - Microsoft面试题 - 24308662
2014-05-12 07:31 题目链接 原题: I have heard this question many times in microsoft interviews. Given two a ...
- Careercup - Microsoft面试题 - 5700293077499904
2014-05-12 00:02 题目链接 原题: For a given map (ie Bing map) given longitude/latitude/ how would you desi ...
- Careercup - Microsoft面试题 - 5204967652589568
2014-05-11 23:57 题目链接 原题: identical balls. one ball measurements ........ dead easy. 题目:9个看起来一样的球,其中 ...
- Careercup - Microsoft面试题 - 5175246478901248
2014-05-11 23:52 题目链接 原题: design an alarm clock for a deaf person. 题目:为聋人设计闹钟? 解法:聋人听不见,那么闪光.震动都可行.睡 ...
- Careercup - Microsoft面试题 - 5718181884723200
2014-05-11 05:55 题目链接 原题: difference between thread and process. 题目:请描述进程和线程的区别. 解法:操作系统理论题.标准答案在恐龙书 ...
- Careercup - Microsoft面试题 - 5173689888800768
2014-05-11 05:21 题目链接 原题: Complexity of a function: int func_fibonacci ( int n) { ) { return n; } el ...
- Careercup - Microsoft面试题 - 6282862240202752
2014-05-11 03:56 题目链接 原题: Given an integer array. Perform circular right shift by n. Give the best s ...
- Careercup - Microsoft面试题 - 5428361417457664
2014-05-11 03:37 题目链接 原题: You have three jars filled with candies. One jar is filled with banana can ...
随机推荐
- Python开发的3种命令执行方法
在python开发中,我们常常需要执行命令,修改相关信息.那对于初学者来说,python中如何执行命令呢?今天,小编就为大家分享3种python命令执行的方法. 1. 使用os.system(&quo ...
- ASP.NET MVC5 高级编程 第3章 视图
参考资料<ASP.NET MVC5 高级编程>第5版 第3章 视图 3.1 视图的作用 视图的职责是向用户提供界面. 不像基于文件的框架,ASP.NET Web Forms 和PHP ,视 ...
- kettle的windows安装
1.首先去官网下载安装包,这个安装包在所有平台上是通用的. 2.kettle是java语言开发的,所以需要配置JAVA_HOME 3.解压kettle的安装包 4.配置环境变量,KETTLE_HOME ...
- php简单缓存类
<?phpclass Cache { private $cache_path;//path for the cache private $cache_expire;//seconds ...
- 安装mysql 5.7 最完整版教程
Step1: 检测系统是否自带安装mysql #yum list installed | grep mysql Step2: 删除系统自带的mysql及其依赖 命令: yum remove mysql ...
- 使用WMIC永久设置你的环境变量
关于wmic,引用一下这位哥们儿的话http://technet.microsoft.com/en-us/library/bb742610.aspx: WMIC扩展WMI(Windows Manage ...
- android 特效UI实现
弧形菜单 https://github.com/daCapricorn/ArcMenu
- svn的使用!!!
1.SVN:subversion(子级源代码版本控制管理软件) 2.SVN的作用 (1)避免开发同一项目不会出现代码覆盖. (2)同一文件可以创建许多不同的版本,并可以随时查看不同版本的内容. (3) ...
- Linux之kernal分析与启动20160610
说一下LINUX内核的分析与启动: 一. 内核启动流程,据此配置内核(机器ID) 1.1 修改Makefile 1.2 选择默认配置 : make s3c2410_defconfig 1.3 make ...
- 6.python字符串-内置方法列举
所谓内置方法,就是凡是字符串都能用的方法,这个方法在创建字符串的类中,下面是总结: 首先,我们要学习一个获取帮助的内置函数 help(对象) ,对象可以是一个我们创建出来的,也可以是创建对象的那个类, ...