亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

鎖如何在多線程中使用

發布時間:2020-11-20 15:20:22 來源:億速云 閱讀:144 作者:Leah 欄目:編程語言

本篇文章為大家展示了鎖如何在多線程中使用,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

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

  }
}

上述內容就是鎖如何在多線程中使用,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

乌拉特中旗| 响水县| 康保县| 无为县| 麻城市| 林州市| 舞钢市| 天峨县| 嘉禾县| 二连浩特市| 保德县| 甘肃省| 惠州市| 东阳市| 中宁县| 嵊州市| 保山市| 藁城市| 汉川市| 弥勒县| 景宁| 乌海市| 温州市| 县级市| 唐海县| 兖州市| 商洛市| 吉隆县| 芒康县| 永德县| 沐川县| 唐河县| 徐汇区| 镇沅| 台东县| 江孜县| 宾阳县| 灌南县| 积石山| 阿勒泰市| 攀枝花市|