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的更多相关文章

  1. Careercup - Microsoft面试题 - 6314866323226624

    2014-05-11 05:29 题目链接 原题: Design remote controller for me. 题目:设计一个遥控器. 解法:遥控什么?什么遥控?传统的红外线信号吗?我只能随便说 ...

  2. Careercup - Microsoft面试题 - 24308662

    2014-05-12 07:31 题目链接 原题: I have heard this question many times in microsoft interviews. Given two a ...

  3. Careercup - Microsoft面试题 - 5700293077499904

    2014-05-12 00:02 题目链接 原题: For a given map (ie Bing map) given longitude/latitude/ how would you desi ...

  4. Careercup - Microsoft面试题 - 5204967652589568

    2014-05-11 23:57 题目链接 原题: identical balls. one ball measurements ........ dead easy. 题目:9个看起来一样的球,其中 ...

  5. Careercup - Microsoft面试题 - 5175246478901248

    2014-05-11 23:52 题目链接 原题: design an alarm clock for a deaf person. 题目:为聋人设计闹钟? 解法:聋人听不见,那么闪光.震动都可行.睡 ...

  6. Careercup - Microsoft面试题 - 5718181884723200

    2014-05-11 05:55 题目链接 原题: difference between thread and process. 题目:请描述进程和线程的区别. 解法:操作系统理论题.标准答案在恐龙书 ...

  7. Careercup - Microsoft面试题 - 5173689888800768

    2014-05-11 05:21 题目链接 原题: Complexity of a function: int func_fibonacci ( int n) { ) { return n; } el ...

  8. Careercup - Microsoft面试题 - 6282862240202752

    2014-05-11 03:56 题目链接 原题: Given an integer array. Perform circular right shift by n. Give the best s ...

  9. Careercup - Microsoft面试题 - 5428361417457664

    2014-05-11 03:37 题目链接 原题: You have three jars filled with candies. One jar is filled with banana can ...

随机推荐

  1. Aspose.cell.dll的使用,导excel表

    using System; using System.Web; using EF; using Newtonsoft.Json; using System.Collections.Generic; u ...

  2. subpage和secondary page的区别

    Hi,  Can you tell me the differences between subpage and secondary  page and when to use which ? I n ...

  3. 通过 XML HTTP 加载 XML 文件

    新建一个.aspx文件 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="02-通 ...

  4. 常规轮询请求,客户端用Ajax调webservice的方法

    服务端发布webservice,下图方框中的一定要有 客户端代码 <script type="text/javascript"> $(document).ready(f ...

  5. c# 如何获取项目的根目录

    c# 如何获取项目的根目录 编写程序的时候,经常需要用的项目根目录.自己总结如下 1.取得控制台应用程序的根目录方法     方法1.Environment.CurrentDirectory 取得或设 ...

  6. object

    object对象 定义一个对象 var obj = {}; obj.name = 'Kate'; var obj = {name:'Jerrt'}; var obj = new Object(); O ...

  7. 原生jdbc执行存储过程

    //定时任务,结转 . //表名 fys_sch_lvyou2 ,存储过程名:fys_sch_lvyou2_carrayover //无参调用:{call insertLine} //有参调用:{ca ...

  8. Ueditor图片缩放的设置

    最近在用Ueditor,功能绝逼强大,不过也有遗憾的地方,上传图片的时候自动缩放的小了,想要图片按宽度整体等比缩放,找了好久,研究了下,终于找到解决方法了. 先改前台的的dialogs/image/i ...

  9. SymPy库常用函数

    简介 SymPy是一个符号计算的Python库.它的目标是成为一个全功能的计算机代数系统,同时保持代码简 洁.易于理解和扩展.它完全由Python写成,不依赖于外部库.SymPy支持符号计算.高精度计 ...

  10. mongodb 3.2存储目录结构说明

    [root@hadoop1 mongodb]# tree ./data ./data |-- WiredTiger | |-- WiredTiger.lock | |-- WiredTiger.tur ...