2014-05-10 22:30

题目链接

原题:

  1. Design database locks to allow r/w concurrency and data consistency.

题目:设计一个支持并行读写并保证数据一致性的数据库锁。

解法:这是什么范畴的面试题?对于我这种完全没有相关项目经验的人,还真是无从下手。翻了书之后顺便重新复习了一下共享锁、互斥锁的概念。说白了,这些都是课本上的基础知识,可是毕业没多久就忘光了。看来知识不用,保质期比水果还短。然后我琢磨着锁的模型,写了个模拟的代码。这个代码和SQL毫无关系,可能也和正确答案相去甚远。姑妄言之,姑妄听之吧。

代码:

  1. // http://www.careercup.com/question?id=6366101810184192
  2. import java.util.concurrent.Semaphore;
  3.  
  4. public class FooBar {
  5. public static final int MAX_CONCURRENT_READ = 100;
  6. private Semaphore sharedSemaphore;
  7. private Semaphore exclusiveSemaphore;
  8.  
  9. public FooBar() {
  10. // TODO Auto-generated constructor stub
  11. this.sharedSemaphore = new Semaphore(MAX_CONCURRENT_READ);
  12. this.exclusiveSemaphore = new Semaphore(1);
  13. }
  14.  
  15. public void reader() {
  16. // The reader here is not to return a value, but to perform read()
  17. // action. Thus it is 'void reader()'.
  18. while (exclusiveSemaphore.availablePermits() < 1) {
  19. try {
  20. Thread.sleep(50);
  21. } catch (InterruptedException e) {
  22. // TODO Auto-generated catch block
  23. e.printStackTrace();
  24. }
  25. }
  26.  
  27. try {
  28. sharedSemaphore.acquire();
  29. System.out.println("Performing read() operation.");
  30. sharedSemaphore.release();
  31. } catch (InterruptedException e) {
  32. // TODO Auto-generated catch block
  33. e.printStackTrace();
  34. }
  35. }
  36.  
  37. public void writer() {
  38. while (exclusiveSemaphore.availablePermits() < 1
  39. && sharedSemaphore.availablePermits() < MAX_CONCURRENT_READ) {
  40. try {
  41. Thread.sleep(50);
  42. } catch (InterruptedException e) {
  43. // TODO Auto-generated catch block
  44. e.printStackTrace();
  45. }
  46. }
  47.  
  48. try {
  49. exclusiveSemaphore.acquire();
  50. System.out.println("Performing write() operation.");
  51. exclusiveSemaphore.release();
  52. } catch (InterruptedException e) {
  53. // TODO Auto-generated catch block
  54. e.printStackTrace();
  55. }
  56. }
  57.  
  58. public static void main(String[] args) {
  59. FooBar fooBar = new FooBar();
  60.  
  61. fooBar.reader();
  62. fooBar.writer();
  63. }
  64. }

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. C++类成员函数的 重载、覆盖和隐藏区别

    重载:成员函数被重载的特征: (1)相同的范围(在同一个类中): (2)函数名字相同: (3)参数不同: (4)virtual 关键字可有可无. #include <iostream> u ...

  2. [原]hdu2602 Bone Collector (01背包)

    本文出自:http://blog.csdn.net/svitter 题意:典型到不能再典型的01背包.给了我一遍AC的快感. //=================================== ...

  3. win7如何设置某个软件不弹出用户账户控制

    手动修改注册表: 在 HKEY_CURRENT_USERS\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers 键下面 ...

  4. jLink V8调试exynos 4412 u-boot的几点补充

    /** ****************************************************************************** * @author    Maox ...

  5. Linux ssh exit,启动的后台进程不会停止

    一般情况下,想要通过终端长时间运行任务,需要使用nohup 或者 screen,如果不使用会怎么样呢?来测试一下   描述: 场景1:ssh登录机器,通过添加(&),启动任务到后台,通过exi ...

  6. IE9 以下版本浏览器兼容HTML5的方法,使用百度静态资源的html5shiv包

    <!--[if lt IE9]> <script src="http://apps.bdimg.com/libs/html5shiv/3.7/html5shiv.min.j ...

  7. 学习c的第7天

    #include <stdio.h> int main() { int x=0; if (x==0) { printf("x为假\n"); } else { print ...

  8. Linux rabbitmq的安装和安装amqp的php插件

    RabbitMQ是一个消息代理.它的核心原理非常简单:接收和发送消息.你可以把它想像成一个邮局:你把信件放入邮箱,邮递员就会把信件投递到你的收件人处.在这个比喻中,RabbitMQ是一个邮箱.邮局.邮 ...

  9. PHP判断用户所在国家并跳转对应的目录

    <?php // 淘宝API查询国家代码 $url = "http://ip.taobao.com/service/getIpInfo.php?ip=".get_client ...

  10. 18)Java八股文名词

      >VO:   value-object >DTO: Data Transform Object >DTD: Document Type Definition      文档类型定 ...