Java马士兵高并发编程视频学习笔记(二)

1.ReentrantLock的简单使用  

Reentrant n.再进入java

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

能够完成synchronized相同的做用,但必须手动释放锁this

package com.dingyu2; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; /** * Reentrant n.再进入 * ReentrantLock 一个可重入互斥Lock具备与使用synchronized方法和语句访问的隐式监视锁相同的基本行为和语义,但具备扩展功能。(从jdk1.8中文版复制而来) * 能够完成synchronized相同的做用,但必须手动释放锁 * @author dingyu * */
public class ReentrantLock1 { private Lock lock = new ReentrantLock(); public void m1() { try { lock.lock();//synchronized(this)相似,锁定的是堆的对象 for (int i = 0; i < 10; i++) System.out.println("m1-" + i); } catch (Exception e) { System.out.println("m1启动"); } finally { System.out.println("m1结束"); lock.unlock(); } } public void m2() { try { lock.lock(); for (int i = 0; i < 10; i++) System.out.println("m2-" + i); } catch (Exception e) { System.out.println("m2启动"); } finally { System.out.println("m2结束"); lock.unlock(); } } public static void main(String[] args) { ReentrantLock1 reentrantLock1 = new ReentrantLock1(); new Thread(() -> reentrantLock1.m1()).start(); new Thread(() -> reentrantLock1.m2()).start(); } }

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

package com.dingyu2; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; /** * ReentrantLock对synchronized的扩展之tryLock() * * @author dingyu * */
public class ReentrantLock2 { private Lock lock = new ReentrantLock(); public void m1() { lock.lock();// 一直锁着,不手动释放, 和synchronized(this)相似,锁定的是堆的对象
 } public void m2() { boolean isNotLock = lock.tryLock();// 若是别的进程锁着就返回false,若是没锁返回true // 咱们能够根据有没有锁来执行本身的逻辑,而不须要等着锁的释放,更加灵活
        if (isNotLock) { System.out.println("lock对象没有被锁定"); } else { System.out.println("lock对象被锁定了"); } } public static void main(String[] args) { ReentrantLock2 reentrantLock2 = new ReentrantLock2(); new Thread(() -> reentrantLock2.m1()).start(); new Thread(() -> reentrantLock2.m2()).start(); } }

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

package com.dingyu2; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; /** * ReentranLock对synchronized的扩展:能够被另外的线程打断 * 由于m1方法一直占着锁,m2永远不可能获得锁,既然得不到锁,咱们就关闭m2好了,这时候得用lockInterruptibly * * @author dingyu * */
public class ReentrantLock3 { private Lock lock = new ReentrantLock(); public void m1() { lock.lock(); try { System.out.println("t1 start"); while (true) { } } finally { lock.unlock(); System.out.println("t1 end"); } } public void m2() { try { lock.lockInterruptibly(); System.out.println("t2 start"); } catch (InterruptedException e) { System.out.println("t2被打断了"); } finally { if (lock.tryLock()) lock.unlock(); System.out.println("t2 end"); } } public static void main(String[] args) { ReentrantLock3 reentrantLock3 = new ReentrantLock3(); Thread t1 = new Thread(() -> reentrantLock3.m1(), "t1"); t1.start(); Thread t2 = new Thread(() -> reentrantLock3.m2(), "t2"); t2.start(); t2.interrupt(); } }

4.ReentrantLock对synchronized的扩展 : 能够指定公平锁

import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; /** * ReentrantLock对synchronized的扩展 : 能够指定公平锁,哪一个线程等待时间长,哪一个先执行 * 在构造函数中放入ture参数 * * @author dingyu * */
public class ReentrantLock4 { private Lock lock = new ReentrantLock(true); public void m1() { for (int i = 0; i < 10; i++) { try { lock.lock(); System.out.println(Thread.currentThread().getName() + "running"); } finally { lock.unlock(); } } } public static void main(String[] args) { ReentrantLock4 lock4 = new ReentrantLock4(); new Thread(()->lock4.m1(),"t1").start(); new Thread(()->lock4.m1(),"t2").start(); } }

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

package com.dingyu2; import java.util.LinkedList; /** * 使用wait和notifyAll实现消费者生产者模式 * * @author dingyu * */
public class ProduceConsumer { private final LinkedList<Integer> lists = new LinkedList<Integer>(); private final int MAX = 10; private int count = 0; public synchronized void put(Integer i) { while (lists.size() == MAX) { // wait大多数状况和while一块儿用
            try { this.wait();// 若是满了我就释放锁,而且等待
            } catch (InterruptedException e) { e.printStackTrace(); } } lists.add(i);// 生产一个
        count++; this.notifyAll();// 叫醒消费者能够消费啦
 } public synchronized Integer get() { while (lists.size() == 0) { try { this.wait();// 若是集合为空,不能消费,释放锁,等着
            } catch (InterruptedException e) { e.printStackTrace(); } } Integer num = lists.removeFirst(); count--; this.notifyAll();// 叫醒生产者,能够继续生产啦
        return num; } }

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

package com.dingyu2; /** * 使用Condition 完成生产者消费者模式 * @author dingyu * */

import java.util.LinkedList; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class ProduceConsumer2 { private final LinkedList<Integer> lists = new LinkedList<Integer>(); private final int MAX = 10; private int count = 0; private Lock lock = new ReentrantLock(); private Condition p = lock.newCondition();// 生产者
    private Condition c = lock.newCondition();// 消费者

    public void put(Integer i) { try { lock.lock(); while (lists.size() == MAX) { try { p.await(); } catch (InterruptedException e) { e.printStackTrace(); } } lists.add(i); count++; c.signalAll(); } finally { lock.unlock(); } } public Integer get() { Integer i = null; try { lock.lock(); while (lists.size() == 0) { try { c.await(); } catch (InterruptedException e) { e.printStackTrace(); } } i = lists.removeFirst(); count++; p.signalAll(); } finally { lock.unlock(); } return i; } }

 7.ThreadLocal 线程局部变量  每一个线程中的这个变量归本身线程管

package com.dingyu; public class ThreadLocal1 { private ThreadLocal<Integer> tl = new ThreadLocal<Integer>(); public void m1() { System.out.println(tl.get()); } public void m2() { tl.set(7898); } public static void main(String[] args) { ThreadLocal1 local1 = new ThreadLocal1(); new Thread(() -> local1.m2()).start(); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } new Thread(() -> local1.m1()).start(); } }
相关文章
相关标签/搜索