经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Java » 查看文章
Java马士兵高并发编程视频学习笔记(二)
来源:cnblogs  作者:DingYu  时间:2018/11/20 14:54:04  对本文有异议

1.ReentrantLock的简单使用  

Reentrant n.再进入

ReentrantLock 一个可重入互斥Lock具有与使用synchronized方法和语句访问的隐式监视锁相同的基本行为和语义,但具有扩展功能。(从jdk1.8中文版复制而来)

可以完成synchronized相同的作用,但必须手动释放锁

  1. package com.dingyu2;
  2. import java.util.concurrent.locks.Lock;
  3. import java.util.concurrent.locks.ReentrantLock;
  4. /**
  5. * Reentrant n.再进入
  6. * ReentrantLock 一个可重入互斥Lock具有与使用synchronized方法和语句访问的隐式监视锁相同的基本行为和语义,但具有扩展功能。(从jdk1.8中文版复制而来)
  7. * 可以完成synchronized相同的作用,但必须手动释放锁
  8. * @author dingyu
  9. *
  10. */
  11. public class ReentrantLock1 {
  12. private Lock lock = new ReentrantLock();
  13. public void m1() {
  14. try {
  15. lock.lock();//synchronized(this)类似,锁定的是堆的对象
  16. for (int i = 0; i < 10; i++)
  17. System.out.println("m1-" + i);
  18. } catch (Exception e) {
  19. System.out.println("m1启动");
  20. } finally {
  21. System.out.println("m1结束");
  22. lock.unlock();
  23. }
  24. }
  25. public void m2() {
  26. try {
  27. lock.lock();
  28. for (int i = 0; i < 10; i++)
  29. System.out.println("m2-" + i);
  30. } catch (Exception e) {
  31. System.out.println("m2启动");
  32. } finally {
  33. System.out.println("m2结束");
  34. lock.unlock();
  35. }
  36. }
  37. public static void main(String[] args) {
  38. ReentrantLock1 reentrantLock1 = new ReentrantLock1();
  39. new Thread(() -> reentrantLock1.m1()).start();
  40. new Thread(() -> reentrantLock1.m2()).start();
  41. }
  42. }

2.ReentrantLock对synchronized的扩展之tryLock()

  1. package com.dingyu2;
  2. import java.util.concurrent.locks.Lock;
  3. import java.util.concurrent.locks.ReentrantLock;
  4. /**
  5. * ReentrantLock对synchronized的扩展之tryLock()
  6. *
  7. * @author dingyu
  8. *
  9. */
  10. public class ReentrantLock2 {
  11. private Lock lock = new ReentrantLock();
  12. public void m1() {
  13. lock.lock();// 一直锁着,不手动释放, 和synchronized(this)类似,锁定的是堆的对象
  14. }
  15. public void m2() {
  16. boolean isNotLock = lock.tryLock();// 如果别的进程锁着就返回false,如果没锁返回true
  17. // 我们可以根据有没有锁来执行自己的逻辑,而不需要等着锁的释放,更加灵活
  18. if (isNotLock) {
  19. System.out.println("lock对象没有被锁定");
  20. } else {
  21. System.out.println("lock对象被锁定了");
  22. }
  23. }
  24. public static void main(String[] args) {
  25. ReentrantLock2 reentrantLock2 = new ReentrantLock2();
  26. new Thread(() -> reentrantLock2.m1()).start();
  27. new Thread(() -> reentrantLock2.m2()).start();
  28. }
  29. }

3.ReentranLock对synchronized的扩展:可以被另外的线程打断

  1. package com.dingyu2;
  2. import java.util.concurrent.locks.Lock;
  3. import java.util.concurrent.locks.ReentrantLock;
  4. /**
  5. * ReentranLock对synchronized的扩展:可以被另外的线程打断
  6. * 因为m1方法一直占着锁,m2永远不可能得到锁,既然得不到锁,我们就关闭m2好了,这时候得用lockInterruptibly
  7. *
  8. * @author dingyu
  9. *
  10. */
  11. public class ReentrantLock3 {
  12. private Lock lock = new ReentrantLock();
  13. public void m1() {
  14. lock.lock();
  15. try {
  16. System.out.println("t1 start");
  17. while (true) {
  18. }
  19. } finally {
  20. lock.unlock();
  21. System.out.println("t1 end");
  22. }
  23. }
  24. public void m2() {
  25. try {
  26. lock.lockInterruptibly();
  27. System.out.println("t2 start");
  28. } catch (InterruptedException e) {
  29. System.out.println("t2被打断了");
  30. } finally {
  31. if (lock.tryLock())
  32. lock.unlock();
  33. System.out.println("t2 end");
  34. }
  35. }
  36. public static void main(String[] args) {
  37. ReentrantLock3 reentrantLock3 = new ReentrantLock3();
  38. Thread t1 = new Thread(() -> reentrantLock3.m1(), "t1");
  39. t1.start();
  40. Thread t2 = new Thread(() -> reentrantLock3.m2(), "t2");
  41. t2.start();
  42. t2.interrupt();
  43. }
  44. }

4.ReentrantLock对synchronized的扩展 : 可以指定公平锁

  1. import java.util.concurrent.locks.Lock;
  2. import java.util.concurrent.locks.ReentrantLock;
  3. /**
  4. * ReentrantLock对synchronized的扩展 : 可以指定公平锁,哪个线程等待时间长,哪个先执行
  5. * 在构造函数中放入ture参数
  6. *
  7. * @author dingyu
  8. *
  9. */
  10. public class ReentrantLock4 {
  11. private Lock lock = new ReentrantLock(true);
  12. public void m1() {
  13. for (int i = 0; i < 10; i++) {
  14. try {
  15. lock.lock();
  16. System.out.println(Thread.currentThread().getName() + "running");
  17. } finally {
  18. lock.unlock();
  19. }
  20. }
  21. }
  22. public static void main(String[] args) {
  23. ReentrantLock4 lock4 = new ReentrantLock4();
  24. new Thread(()->lock4.m1(),"t1").start();
  25. new Thread(()->lock4.m1(),"t2").start();
  26. }
  27. }

5.使用wait和notifyAll实现消费者生产者模式

  1. package com.dingyu2;
  2. import java.util.LinkedList;
  3. /**
  4. * 使用wait和notifyAll实现消费者生产者模式
  5. *
  6. * @author dingyu
  7. *
  8. */
  9. public class ProduceConsumer {
  10. private final LinkedList<Integer> lists = new LinkedList<Integer>();
  11. private final int MAX = 10;
  12. private int count = 0;
  13. public synchronized void put(Integer i) {
  14. while (lists.size() == MAX) { // wait大多数情况和while一起用
  15. try {
  16. this.wait();// 如果满了我就释放锁,并且等待
  17. } catch (InterruptedException e) {
  18. e.printStackTrace();
  19. }
  20. }
  21. lists.add(i);// 生产一个
  22. count++;
  23. this.notifyAll();// 叫醒消费者可以消费啦
  24. }
  25. public synchronized Integer get() {
  26. while (lists.size() == 0) {
  27. try {
  28. this.wait();// 如果集合为空,不能消费,释放锁,等着
  29. } catch (InterruptedException e) {
  30. e.printStackTrace();
  31. }
  32. }
  33. Integer num = lists.removeFirst();
  34. count--;
  35. this.notifyAll();// 叫醒生产者,可以继续生产啦
  36. return num;
  37. }
  38. }

6.使用Condition 完成生产者消费者模式

  1. package com.dingyu2;
  2. /**
  3. * 使用Condition 完成生产者消费者模式
  4. * @author dingyu
  5. *
  6. */
  7.  
  8. import java.util.LinkedList;
  9. import java.util.concurrent.locks.Condition;
  10. import java.util.concurrent.locks.Lock;
  11. import java.util.concurrent.locks.ReentrantLock;
  12. public class ProduceConsumer2 {
  13. private final LinkedList<Integer> lists = new LinkedList<Integer>();
  14. private final int MAX = 10;
  15. private int count = 0;
  16. private Lock lock = new ReentrantLock();
  17. private Condition p = lock.newCondition();// 生产者
  18. private Condition c = lock.newCondition();// 消费者
  19.  
  20. public void put(Integer i) {
  21. try {
  22. lock.lock();
  23. while (lists.size() == MAX) {
  24. try {
  25. p.await();
  26. } catch (InterruptedException e) {
  27. e.printStackTrace();
  28. }
  29. }
  30. lists.add(i);
  31. count++;
  32. c.signalAll();
  33. } finally {
  34. lock.unlock();
  35. }
  36. }
  37. public Integer get() {
  38. Integer i = null;
  39. try {
  40. lock.lock();
  41. while (lists.size() == 0) {
  42. try {
  43. c.await();
  44. } catch (InterruptedException e) {
  45. e.printStackTrace();
  46. }
  47. }
  48. i = lists.removeFirst();
  49. count++;
  50. p.signalAll();
  51. } finally {
  52. lock.unlock();
  53. }
  54. return i;
  55. }
  56. }

 7.ThreadLocal 线程局部变量  每个线程中的这个变量归自己线程管

  1. package com.dingyu;
  2. public class ThreadLocal1 {
  3. private ThreadLocal<Integer> tl = new ThreadLocal<Integer>();
  4. public void m1() {
  5. System.out.println(tl.get());
  6. }
  7. public void m2() {
  8. tl.set(7898);
  9. }
  10. public static void main(String[] args) {
  11. ThreadLocal1 local1 = new ThreadLocal1();
  12. new Thread(() -> local1.m2()).start();
  13. try {
  14. Thread.sleep(5000);
  15. } catch (InterruptedException e) {
  16. e.printStackTrace();
  17. }
  18. new Thread(() -> local1.m1()).start();
  19. }
  20. }

 

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号