浅谈多线程中的锁的几种用法总结(必看)

 更新时间:2017年05月22日 10:43:23   投稿:jingxian  
下面小编就为大家带来一篇浅谈多线程中的锁的几种用法总结(必看)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

一、ReentrantLock

package com.ietree.basicskill.mutilthread.lock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by Administrator on 2017/5/17.
 */
public class UseReentrantLock {

  private Lock lock = new ReentrantLock();

  public void method1(){
    try {
      lock.lock();
      System.out.println("当前线程:" + Thread.currentThread().getName() + "进入method1..");
      Thread.sleep(1000);
      System.out.println("当前线程:" + Thread.currentThread().getName() + "退出method1..");
      Thread.sleep(1000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    } finally {

      lock.unlock();
    }
  }

  public void method2(){
    try {
      lock.lock();
      System.out.println("当前线程:" + Thread.currentThread().getName() + "进入method2..");
      Thread.sleep(2000);
      System.out.println("当前线程:" + Thread.currentThread().getName() + "退出method2..");
      Thread.sleep(1000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    } finally {

      lock.unlock();
    }
  }

  public static void main(String[] args) {

    final UseReentrantLock ur = new UseReentrantLock();
    Thread t1 = new Thread(new Runnable() {
      @Override
      public void run() {
        ur.method1();
        ur.method2();
      }
    }, "t1");

    t1.start();
    try {
      Thread.sleep(10);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    //System.out.println(ur.lock.getQueueLength());
  }

}

二、ReentrantReadWriteLock

package com.ietree.basicskill.mutilthread.lock;

import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * Created by Administrator on 2017/5/17.
 */
public class UseReentrantReadWriteLock {

  private ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();
  private ReentrantReadWriteLock.ReadLock readLock = rwLock.readLock();
  private ReentrantReadWriteLock.WriteLock writeLock = rwLock.writeLock();

  public void read(){
    try {
      readLock.lock();
      System.out.println("当前线程:" + Thread.currentThread().getName() + "进入...");
      Thread.sleep(3000);
      System.out.println("当前线程:" + Thread.currentThread().getName() + "退出...");
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      readLock.unlock();
    }
  }

  public void write(){
    try {
      writeLock.lock();
      System.out.println("当前线程:" + Thread.currentThread().getName() + "进入...");
      Thread.sleep(3000);
      System.out.println("当前线程:" + Thread.currentThread().getName() + "退出...");
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      writeLock.unlock();
    }
  }

  public static void main(String[] args) {

    final UseReentrantReadWriteLock urrw = new UseReentrantReadWriteLock();

    Thread t1 = new Thread(new Runnable() {
      @Override
      public void run() {
        urrw.read();
      }
    }, "t1");
    Thread t2 = new Thread(new Runnable() {
      @Override
      public void run() {
        urrw.read();
      }
    }, "t2");
    Thread t3 = new Thread(new Runnable() {
      @Override
      public void run() {
        urrw.write();
      }
    }, "t3");
    Thread t4 = new Thread(new Runnable() {
      @Override
      public void run() {
        urrw.write();
      }
    }, "t4");

//    t1.start();
//    t2.start();

//    t1.start(); // R
//    t3.start(); // W

    t3.start();
    t4.start();
  }
}

三、Condition

package com.ietree.basicskill.mutilthread.lock;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by Administrator on 2017/5/17.
 */
public class UseCondition {
  private Lock lock = new ReentrantLock();
  private Condition condition = lock.newCondition();

  public void method1(){
    try {
      lock.lock();
      System.out.println("当前线程:" + Thread.currentThread().getName() + "进入等待状态..");
      Thread.sleep(3000);
      System.out.println("当前线程:" + Thread.currentThread().getName() + "释放锁..");
      condition.await();  // Object wait
      System.out.println("当前线程:" + Thread.currentThread().getName() +"继续执行...");
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      lock.unlock();
    }
  }

  public void method2(){
    try {
      lock.lock();
      System.out.println("当前线程:" + Thread.currentThread().getName() + "进入..");
      Thread.sleep(3000);
      System.out.println("当前线程:" + Thread.currentThread().getName() + "发出唤醒..");
      condition.signal();    //Object notify
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      lock.unlock();
    }
  }

  public static void main(String[] args) {

    final UseCondition uc = new UseCondition();
    Thread t1 = new Thread(new Runnable() {
      @Override
      public void run() {
        uc.method1();
      }
    }, "t1");
    Thread t2 = new Thread(new Runnable() {
      @Override
      public void run() {
        uc.method2();
      }
    }, "t2");
    t1.start();

    t2.start();
  }
}

四、ManyCondition

package com.ietree.basicskill.mutilthread.lock;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by Administrator on 2017/5/17.
 */
public class UseManyCondition {
  private ReentrantLock lock = new ReentrantLock();
  private Condition c1 = lock.newCondition();
  private Condition c2 = lock.newCondition();

  public void m1(){
    try {
      lock.lock();
      System.out.println("当前线程:" +Thread.currentThread().getName() + "进入方法m1等待..");
      c1.await();
      System.out.println("当前线程:" +Thread.currentThread().getName() + "方法m1继续..");
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      lock.unlock();
    }
  }

  public void m2(){
    try {
      lock.lock();
      System.out.println("当前线程:" +Thread.currentThread().getName() + "进入方法m2等待..");
      c1.await();
      System.out.println("当前线程:" +Thread.currentThread().getName() + "方法m2继续..");
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      lock.unlock();
    }
  }

  public void m3(){
    try {
      lock.lock();
      System.out.println("当前线程:" +Thread.currentThread().getName() + "进入方法m3等待..");
      c2.await();
      System.out.println("当前线程:" +Thread.currentThread().getName() + "方法m3继续..");
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      lock.unlock();
    }
  }

  public void m4(){
    try {
      lock.lock();
      System.out.println("当前线程:" +Thread.currentThread().getName() + "唤醒..");
      c1.signalAll();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      lock.unlock();
    }
  }

  public void m5(){
    try {
      lock.lock();
      System.out.println("当前线程:" +Thread.currentThread().getName() + "唤醒..");
      c2.signal();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      lock.unlock();
    }
  }

  public static void main(String[] args) {


    final UseManyCondition umc = new UseManyCondition();
    Thread t1 = new Thread(new Runnable() {
      @Override
      public void run() {
        umc.m1();
      }
    },"t1");
    Thread t2 = new Thread(new Runnable() {
      @Override
      public void run() {
        umc.m2();
      }
    },"t2");
    Thread t3 = new Thread(new Runnable() {
      @Override
      public void run() {
        umc.m3();
      }
    },"t3");
    Thread t4 = new Thread(new Runnable() {
      @Override
      public void run() {
        umc.m4();
      }
    },"t4");
    Thread t5 = new Thread(new Runnable() {
      @Override
      public void run() {
        umc.m5();
      }
    },"t5");

    t1.start();  // c1
    t2.start();  // c1
    t3.start();  // c2


    try {
      Thread.sleep(2000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }

    t4.start();  // c1
    try {
      Thread.sleep(2000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    t5.start();  // c2

  }
}

以上这篇浅谈多线程中的锁的几种用法总结(必看)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • java中lambda表达式简单用例

    java中lambda表达式简单用例

    让我们从最简单的例子开始,来学习如何对一个string列表进行排序。我们首先使用Java 8之前的方法来实现
    2016-09-09
  • 使用java实现LIS算法,出操队形的问题

    使用java实现LIS算法,出操队形的问题

    下面小编就为大家带来一篇使用java实现LIS算法,出操队形的问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-09-09
  • Java语言实现简单FTP软件 FTP软件本地窗口实现(5)

    Java语言实现简单FTP软件 FTP软件本地窗口实现(5)

    这篇文章主要为大家详细介绍了Java语言实现简单FTP软件,FTP软件本地窗口的实现方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-03-03
  • 深入理解Spring Cache框架

    深入理解Spring Cache框架

    今天给大家分析一下 Spring 框架本身对这些缓存具体实现的支持和融合。使用 Spring Cache 将大大的减少我们的Spring项目中缓存使用的复杂度,提高代码可读性。本文将从以下几个方面来认识Spring Cache框架。感兴趣的小伙伴们可以参考一下
    2018-11-11
  • 如何去掉IntelliJ IDEA中mybatis对应的xml文件警告

    如何去掉IntelliJ IDEA中mybatis对应的xml文件警告

    这篇文章主要介绍了如何去掉IntelliJ IDEA中mybatis对应的xml文件警告问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-04-04
  • Spring5使用JSR 330标准注解的方法

    Spring5使用JSR 330标准注解的方法

    从Spring3.0之后,除了Spring自带的注解,我们也可以使用JSR330的标准注解,本文主要介绍了Spring5使用JSR 330标准注解,感兴趣的可以了解一下
    2021-09-09
  • Java中如何获取图片文件格式(后缀)

    Java中如何获取图片文件格式(后缀)

    这篇文章主要介绍了Java中如何获取图片文件格式(后缀),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-06-06
  • Spring Boot如何开启并使用邮件服务

    Spring Boot如何开启并使用邮件服务

    这篇文章主要介绍了Spring Boot如何开启并使用邮件服务,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-06-06
  • Springboot如何利用拦截器拦截请求信息收集到日志详解

    Springboot如何利用拦截器拦截请求信息收集到日志详解

    一些系统经常需要关注用户请求的具体信息,如用户信息、请求参数、响应结果等等,在SpringBoot应用中可通过拦截器的方式统一处理,下面这篇文章主要给大家介绍了关于Springboot如何利用拦截器拦截请求信息收集到日志的相关资料,需要的朋友可以参考下
    2021-08-08
  • poi导出word表格的操作讲解

    poi导出word表格的操作讲解

    这篇文章主要介绍了poi导出word表格的操作讲解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-10-10

最新评论