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

作者:jingxian 时间:2021-12-23 08:05:55 

一、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

}
}
标签:多线程,锁
0
投稿

猜你喜欢

  • Unity游戏开发实现场景切换示例

    2022-12-12 16:06:20
  • Java判断两个日期相差天数的方法

    2021-11-29 05:55:07
  • Java实现生成JSON字符串的三种方式分享

    2022-05-20 15:21:31
  • Java Map所有的值转为String类型

    2022-09-05 11:53:09
  • jcrop 网页截图工具(插件)开发

    2022-10-21 22:30:19
  • Springboot+WebSocket实现一对一聊天和公告的示例代码

    2022-06-16 11:32:33
  • Spring Boot整合Mybatis并完成CRUD操作的实现示例

    2023-11-09 04:36:46
  • java实现数字转大写的方法

    2021-11-04 21:39:45
  • 基于Android AppWidgetProvider的使用介绍

    2021-09-27 08:48:19
  • C#遍历操作系统下所有驱动器的方法

    2022-06-29 09:12:14
  • 详解Spring Boot Profiles 配置和使用

    2021-10-05 22:54:57
  • C#实现Xml序列化与反序列化的方法

    2022-07-26 22:16:23
  • JAVA多线程知识汇总

    2021-08-03 09:04:47
  • List集合多个复杂字段判断去重的案例

    2022-08-01 16:23:28
  • C#如何从byte[]中直接读取Structure实例详解

    2021-11-07 22:02:00
  • 深入理解C#中常见的委托

    2022-01-15 07:53:05
  • Java数据结构之优先级队列(PriorityQueue)用法详解

    2023-11-18 13:00:50
  • Java JDK 二分法 分析demo(推荐)

    2022-02-28 23:29:21
  • 详解C#中的委托

    2022-05-10 06:01:43
  • Android ScrollView实现下拉弹回动画效果

    2023-04-27 20:31:46
  • asp之家 软件编程 m.aspxhome.com