欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

淺談多線程中的鎖的幾種用法總結(jié)(必看)

 更新時間:2017年05月22日 10:43:23   投稿:jingxian  
下面小編就為大家?guī)硪黄獪\談多線程中的鎖的幾種用法總結(jié)(必看)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

一、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("當(dāng)前線程:" + Thread.currentThread().getName() + "進入method1..");
      Thread.sleep(1000);
      System.out.println("當(dāng)前線程:" + Thread.currentThread().getName() + "退出method1..");
      Thread.sleep(1000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    } finally {

      lock.unlock();
    }
  }

  public void method2(){
    try {
      lock.lock();
      System.out.println("當(dāng)前線程:" + Thread.currentThread().getName() + "進入method2..");
      Thread.sleep(2000);
      System.out.println("當(dāng)前線程:" + 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("當(dāng)前線程:" + Thread.currentThread().getName() + "進入...");
      Thread.sleep(3000);
      System.out.println("當(dāng)前線程:" + Thread.currentThread().getName() + "退出...");
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      readLock.unlock();
    }
  }

  public void write(){
    try {
      writeLock.lock();
      System.out.println("當(dāng)前線程:" + Thread.currentThread().getName() + "進入...");
      Thread.sleep(3000);
      System.out.println("當(dāng)前線程:" + 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("當(dāng)前線程:" + Thread.currentThread().getName() + "進入等待狀態(tài)..");
      Thread.sleep(3000);
      System.out.println("當(dāng)前線程:" + Thread.currentThread().getName() + "釋放鎖..");
      condition.await();  // Object wait
      System.out.println("當(dāng)前線程:" + Thread.currentThread().getName() +"繼續(xù)執(zhí)行...");
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      lock.unlock();
    }
  }

  public void method2(){
    try {
      lock.lock();
      System.out.println("當(dāng)前線程:" + Thread.currentThread().getName() + "進入..");
      Thread.sleep(3000);
      System.out.println("當(dāng)前線程:" + Thread.currentThread().getName() + "發(fā)出喚醒..");
      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("當(dāng)前線程:" +Thread.currentThread().getName() + "進入方法m1等待..");
      c1.await();
      System.out.println("當(dāng)前線程:" +Thread.currentThread().getName() + "方法m1繼續(xù)..");
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      lock.unlock();
    }
  }

  public void m2(){
    try {
      lock.lock();
      System.out.println("當(dāng)前線程:" +Thread.currentThread().getName() + "進入方法m2等待..");
      c1.await();
      System.out.println("當(dāng)前線程:" +Thread.currentThread().getName() + "方法m2繼續(xù)..");
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      lock.unlock();
    }
  }

  public void m3(){
    try {
      lock.lock();
      System.out.println("當(dāng)前線程:" +Thread.currentThread().getName() + "進入方法m3等待..");
      c2.await();
      System.out.println("當(dāng)前線程:" +Thread.currentThread().getName() + "方法m3繼續(xù)..");
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      lock.unlock();
    }
  }

  public void m4(){
    try {
      lock.lock();
      System.out.println("當(dāng)前線程:" +Thread.currentThread().getName() + "喚醒..");
      c1.signalAll();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      lock.unlock();
    }
  }

  public void m5(){
    try {
      lock.lock();
      System.out.println("當(dāng)前線程:" +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

  }
}

以上這篇淺談多線程中的鎖的幾種用法總結(jié)(必看)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • java中l(wèi)ambda表達式簡單用例

    java中l(wèi)ambda表達式簡單用例

    讓我們從最簡單的例子開始,來學(xué)習(xí)如何對一個string列表進行排序。我們首先使用Java 8之前的方法來實現(xiàn)
    2016-09-09
  • 使用java實現(xiàn)LIS算法,出操隊形的問題

    使用java實現(xiàn)LIS算法,出操隊形的問題

    下面小編就為大家?guī)硪黄褂胘ava實現(xiàn)LIS算法,出操隊形的問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-09-09
  • Java語言實現(xiàn)簡單FTP軟件 FTP軟件本地窗口實現(xiàn)(5)

    Java語言實現(xiàn)簡單FTP軟件 FTP軟件本地窗口實現(xiàn)(5)

    這篇文章主要為大家詳細(xì)介紹了Java語言實現(xiàn)簡單FTP軟件,F(xiàn)TP軟件本地窗口的實現(xiàn)方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • 深入理解Spring Cache框架

    深入理解Spring Cache框架

    今天給大家分析一下 Spring 框架本身對這些緩存具體實現(xiàn)的支持和融合。使用 Spring Cache 將大大的減少我們的Spring項目中緩存使用的復(fù)雜度,提高代碼可讀性。本文將從以下幾個方面來認(rèn)識Spring Cache框架。感興趣的小伙伴們可以參考一下
    2018-11-11
  • 如何去掉IntelliJ IDEA中mybatis對應(yīng)的xml文件警告

    如何去掉IntelliJ IDEA中mybatis對應(yīng)的xml文件警告

    這篇文章主要介紹了如何去掉IntelliJ IDEA中mybatis對應(yīng)的xml文件警告問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • Spring5使用JSR 330標(biāo)準(zhǔn)注解的方法

    Spring5使用JSR 330標(biāo)準(zhǔn)注解的方法

    從Spring3.0之后,除了Spring自帶的注解,我們也可以使用JSR330的標(biāo)準(zhǔn)注解,本文主要介紹了Spring5使用JSR 330標(biāo)準(zhǔn)注解,感興趣的可以了解一下
    2021-09-09
  • Java中如何獲取圖片文件格式(后綴)

    Java中如何獲取圖片文件格式(后綴)

    這篇文章主要介紹了Java中如何獲取圖片文件格式(后綴),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • Spring Boot如何開啟并使用郵件服務(wù)

    Spring Boot如何開啟并使用郵件服務(wù)

    這篇文章主要介紹了Spring Boot如何開啟并使用郵件服務(wù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-06-06
  • Springboot如何利用攔截器攔截請求信息收集到日志詳解

    Springboot如何利用攔截器攔截請求信息收集到日志詳解

    一些系統(tǒng)經(jīng)常需要關(guān)注用戶請求的具體信息,如用戶信息、請求參數(shù)、響應(yīng)結(jié)果等等,在SpringBoot應(yīng)用中可通過攔截器的方式統(tǒng)一處理,下面這篇文章主要給大家介紹了關(guān)于Springboot如何利用攔截器攔截請求信息收集到日志的相關(guān)資料,需要的朋友可以參考下
    2021-08-08
  • poi導(dǎo)出word表格的操作講解

    poi導(dǎo)出word表格的操作講解

    這篇文章主要介紹了poi導(dǎo)出word表格的操作講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-10-10

最新評論