服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

读锁和写锁的使用

读锁和写锁的使用


/*

software architecture design patterns in java
by partha kuchana 

auerbach publications

*/

public class readwritelocktest {

  public static void main(string[] args) {
    item item = new item("compscience-i");
    new membertransaction("member1", item, "statuscheck");
    new membertransaction("member2", item, "statuscheck");
    new membertransaction("member3", item, "checkout");
    new membertransaction("member4", item, "checkout");
    new membertransaction("member5", item, "checkout");
    new membertransaction("member6", item, "statuscheck");

  }
}

class item {

  private string name;

  private readwritelock rwlock;

  private string status;

  public item(string n) {
    name = n;
    rwlock = new readwritelock();
    status = "n";
  }

  public void checkout(string member) {
    rwlock.getwritelock();
    status = "y";
    system.out.println(member + " has been issued a write lock-chkout");
    rwlock.done();
  }

  public string getstatus(string member) {
    rwlock.getreadlock();
    system.out.println(member + " has been issued a read lock");
    rwlock.done();
    return status;
  }

  public void checkin(string member) {
    rwlock.getwritelock();
    status = "n";
    system.out.println(member + " has been issued a write lock-chkin");
    rwlock.done();
  }
}

class readwritelock {

  private object lockobj;

  private int totalreadlocksgiven;

  private boolean writelockissued;

  private int threadswaitingforwritelock;

  public readwritelock() {
    lockobj = new object();
    writelockissued = false;
  }

  /*
   * a read lock can be issued if there is no currently issued write lock and
   * there is no thread(s) currently waiting for the write lock
   */

  public void getreadlock() {
    synchronized (lockobj) {
      while ((writelockissued|| (threadswaitingforwritelock != 0)) {
        try {
          lockobj.wait();
        catch (interruptedexception e) {
          //
        }
      }
      //system.out.println(" read lock issued");
      totalreadlocksgiven++;
    }
  }

  /*
   * a write lock can be issued if there is no currently issued read or write
   * lock
   */

  public void getwritelock() {
    synchronized (lockobj) {
      threadswaitingforwritelock++;

      while ((totalreadlocksgiven != 0|| (writelockissued)) {
        try {
          lockobj.wait();
        catch (interruptedexception e) {
          //
        }
      }
      //system.out.println(" write lock issued");
      threadswaitingforwritelock--;
      writelockissued = true;

    }
  }

  //used for releasing locks
  public void done() {
    synchronized (lockobj) {

      //check for errors
      if ((totalreadlocksgiven == 0&& (!writelockissued)) {
        system.out.println(" error: invalid call to release the lock");
        return;
      }
      if (writelockissued)
        writelockissued = false;
      else
        totalreadlocksgiven--;

      lockobj.notifyall();
    }

  }

}

class membertransaction extends thread {

  private string name;

  private item item;

  private string operation;

  public membertransaction(string n, item i, string p) {
    name = n;
    item = i;
    operation = p;
    start();
  }

  public void run() {
    //all members first read the status
    item.getstatus(name);

    if (operation.equals("checkout")) {
      system.out.println("/n" + name + " is ready to checkout the item.");
      item.checkout(name);
      try {
        sleep(1);
      catch (interruptedexception e) {
        //
      }
      item.checkin(name);
    }
  }
}

扫描关注微信公众号