欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-22 10:38:40
一、reentrantlock package com.ietree.basicskill.mutilthread.lock; import java.ut...

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

  }
}

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