网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

锁如何在多线程中使用

本篇文章为大家展示了锁如何在多线程中使用,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

10年积累的网站设计、网站建设经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站制作后付款的网站建设流程,更有来凤免费网站建设让你可以放心的选择与我们合作。

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

  }
}

上述内容就是锁如何在多线程中使用,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。


当前名称:锁如何在多线程中使用
浏览地址:http://bjjierui.cn/article/igpepd.html

其他资讯