A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 全海波 中级黑马   /  2012-8-2 19:48  /  2729 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 全海波 于 2012-8-4 09:37 编辑
  1. class ProducerConsumerDemo
  2. {
  3. public static void main(String[] args)
  4. {
  5. Resource r = new Resource();

  6. Producer pro = new Producer(r);
  7. Consumer con = new Consumer(r);

  8. Thread t1 = new Thread(pro);
  9. Thread t2 = new Thread(pro);
  10. Thread t3 = new Thread(con);
  11. Thread t4 = new Thread(con);

  12. t1.start();
  13. t2.start();
  14. t3.start();
  15. t4.start();

  16. }
  17. }

  18. class Resource
  19. {
  20. private String name;
  21. private int count = 1;
  22. private boolean flag = false;
  23. // t1 t2
  24. public synchronized void set(String name)
  25. {
  26. while(flag)
  27. try{this.wait();}catch(Exception e){}
  28. this.name = name+"--"+count++;

  29. System.out.println(Thread.currentThread().getName()+"...生产者.."+this.name);
  30. flag = true;
  31. this.notifyAll();
  32. }

  33. public synchronized void out()
  34. {
  35. while(!flag)
  36. try{wait();}catch(Exception e){}//t3(放弃资格) t4(放弃资格)
  37. System.out.println(Thread.currentThread().getName()+"...消费者........."+this.name);
  38. flag = false;
  39. this.notifyAll();
  40. }
  41. }

  42. class Producer implements Runnable
  43. {
  44. private Resource res;

  45. Producer(Resource res)
  46. {
  47. this.res = res;
  48. }
  49. public void run()
  50. {
  51. while(true)
  52. {
  53. res.set("+商品+");
  54. }
  55. }
  56. }

  57. class Consumer implements Runnable
  58. {
  59. private Resource res;

  60. Consumer(Resource res)
  61. {
  62. this.res = res;
  63. }
  64. public void run()
  65. {
  66. while(true)
  67. {
  68. res.out();
  69. }
  70. }
  71. }
复制代码
  1. import java.util.concurrent.locks.*;

  2. class ProducerConsumerDemo2
  3. {
  4. public static void main(String[] args)
  5. {
  6. Resource r = new Resource();

  7. Producer pro = new Producer(r);
  8. Consumer con = new Consumer(r);

  9. Thread t1 = new Thread(pro);
  10. Thread t2 = new Thread(pro);
  11. Thread t3 = new Thread(con);
  12. Thread t4 = new Thread(con);

  13. t1.start();
  14. t2.start();
  15. t3.start();
  16. t4.start();

  17.    }
  18. }
  19. class Resource
  20. {
  21. private String name;
  22. private int count = 1;
  23. private boolean flag = false;
  24. // t1 t2
  25. private Lock lock = new ReentrantLock();

  26. private Condition condition_pro = lock.newCondition();
  27. private Condition condition_con = lock.newCondition();



  28. public void set(String name)throws InterruptedException
  29. {
  30. lock.lock();
  31. try
  32. {
  33. while(flag)
  34. condition_pro.await();//t1,t2
  35. this.name = name+"--"+count++;

  36. System.out.println(Thread.currentThread().getName()+"...生产者.."+this.name);
  37. flag = true;
  38. condition_con.signal();
  39. }
  40. finally
  41. {
  42. lock.unlock();
  43. }
  44. }
  45. public void out()throws InterruptedException
  46. {
  47. lock.lock();
  48. try
  49. {
  50. while(!flag)
  51. condition_con.await();
  52. System.out.println(Thread.currentThread().getName()+"...消费者........."+this.name);
  53. flag = false;
  54. condition_pro.signal();
  55. }
  56. finally
  57. {
  58. lock.unlock();
  59. }

  60. }
  61. }

  62. class Producer implements Runnable
  63. {
  64. private Resource res;

  65. Producer(Resource res)
  66. {
  67. this.res = res;
  68. }
  69. public void run()
  70. {
  71. while(true)
  72. {
  73. try
  74. {
  75. res.set("+商品+");
  76. }
  77. catch (InterruptedException e)
  78. {
  79. }

  80. }
  81. }
  82. }

  83. class Consumer implements Runnable
  84. {
  85. private Resource res;

  86. Consumer(Resource res)
  87. {
  88. this.res = res;
  89. }
  90. public void run()
  91. {
  92. while(true)
  93. {
  94. try
  95. {
  96. res.out();
  97. }
  98. catch (InterruptedException e)
  99. {
  100. }
  101. }
  102. }
  103. }
复制代码
问,以上两者的区别?优势在和地方?

1 个回复

倒序浏览
synchronized是在JVM层面上实现的,不但可以通过一些监控工具监控synchronized的锁定,而且在代码执行时出现异常,JVM会自动释放锁定,但是使用Lock则不行,lock是通过代码实现的,要保证锁定一定会被释放,就必须将unLock()放到finally{}中。
在资源竞争不是很激烈的情况下,Synchronized的性能要优于ReetrantLock,但是在资源竞争很激烈的情况下,Synchronized的性能会下降几十倍,但是ReetrantLock的性能能维持常态。
这是刚才查到的
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马