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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

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

  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. }[code]import java.util.concurrent.locks.*;

  72. class ProducerConsumerDemo2
  73. {
  74. public static void main(String[] args)
  75. {
  76. Resource r = new Resource();

  77. Producer pro = new Producer(r);
  78. Consumer con = new Consumer(r);

  79. Thread t1 = new Thread(pro);
  80. Thread t2 = new Thread(pro);
  81. Thread t3 = new Thread(con);
  82. Thread t4 = new Thread(con);

  83. t1.start();
  84. t2.start();
  85. t3.start();
  86. t4.start();

  87.    }
  88. }
  89. class Resource
  90. {
  91. private String name;
  92. private int count = 1;
  93. private boolean flag = false;
  94. // t1 t2
  95. private Lock lock = new ReentrantLock();

  96. private Condition condition_pro = lock.newCondition();
  97. private Condition condition_con = lock.newCondition();



  98. public void set(String name)throws InterruptedException
  99. {
  100. lock.lock();
  101. try
  102. {
  103. while(flag)
  104. condition_pro.await();//t1,t2
  105. this.name = name+"--"+count++;

  106. System.out.println(Thread.currentThread().getName()+"...生产者.."+this.name);
  107. flag = true;
  108. condition_con.signal();
  109. }
  110. finally
  111. {
  112. lock.unlock();
  113. }
  114. }
  115. public void out()throws InterruptedException
  116. {
  117. lock.lock();
  118. try
  119. {
  120. while(!flag)
  121. condition_con.await();
  122. System.out.println(Thread.currentThread().getName()+"...消费者........."+this.name);
  123. flag = false;
  124. condition_pro.signal();
  125. }
  126. finally
  127. {
  128. lock.unlock();
  129. }

  130. }
  131. }

  132. class Producer implements Runnable
  133. {
  134. private Resource res;

  135. Producer(Resource res)
  136. {
  137. this.res = res;
  138. }
  139. public void run()
  140. {
  141. while(true)
  142. {
  143. try
  144. {
  145. res.set("+商品+");
  146. }
  147. catch (InterruptedException e)
  148. {
  149. }

  150. }
  151. }
  152. }

  153. class Consumer implements Runnable
  154. {
  155. private Resource res;

  156. Consumer(Resource res)
  157. {
  158. this.res = res;
  159. }
  160. public void run()
  161. {
  162. while(true)
  163. {
  164. try
  165. {
  166. res.out();
  167. }
  168. catch (InterruptedException e)
  169. {
  170. }
  171. }
  172. }
  173. }
复制代码
[/code]
问这两者有什么区别?第二者的优势何在?

1 个回复

倒序浏览
看来楼主是想问lock和synchronized的区别了
好长的两段代码,而且还写在了一块

首先synchronized是在JVM层面上实现的
在代码执行时出现异常,JVM会自动释放锁定
但是使用Lock则不行
lock是通过代码实现的
要保证锁定一定会被释放,就必须将unLock()放到finally{}中

其次使用 synchronized ,如果T不释放,T1将一直等下去,不能被中断
如果 使用lock,如果T不释放,可以使T1在等待了足够长的时间以后,中断等待
这就致使在资源竞争不是很激烈的情况下,Synchronized的性能要优于lock
但是在资源竞争很激烈的情况下,Synchronized的性能会成倍下降,lock却性能能维持常态
希望对你有所帮助。。。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马