Java 多线程 ReentrantReadWriteLock ExecutorService

Java 多线程 ReentrantReadWriteLock ExecutorService

package thread.readwrite;

import java.util.List;

public class Read implements Runnable {
  private String name;
  private RWLogic logic;

  public Read(String name, RWLogic logic) {
    this.name = name;
    this.logic = logic;
  }

  public void run() {
    // 取数据
    List<User> list = logic.read(this.name);
    StringBuffer buffer = new StringBuffer();
    for (User obj : list) {
      buffer.append(" " + obj.getUsername() + " " + obj.getSalary());
    }
    System.out.println("read, " + this.name + " , " + buffer.toString());

    try {
      Thread.sleep(200);
    } catch (InterruptedException ex) {
    }
  }
}
package thread.readwrite;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class RWLogic {
  private final List<User> list = new ArrayList<User>(0);
  private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
  private final Lock r = rwl.readLock();
  private final Lock w = rwl.writeLock();

  public List<User> read(String name) {
    r.lock();
    System.out.println(name + " Acquires the readLock.");
    try {
      return list;
    } finally {
      System.out.println(name + " Releases the readLock.");
      r.unlock();
    }
  }

  public void write(String name, User user) {
    w.lock();
    System.out.println(name + " Acquires the writeLock.");
    try {
      //long timer = System.currentTimeMillis();
      for (int i = 0; i < 300000000; i++) {
        
      }
      //System.out.println(System.currentTimeMillis()-timer);

      list.add(user);

    } finally {
      System.out.println(name + " Releases the writeLock.");
      w.unlock();
    }
  }
}
package thread.readwrite;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Test {
  /**
   * @author jzh add 2011-12-24
   * @param args
   */
  public static void main(String args[]) {
    // 创建一个线程池(创建一个可根据需要创建新线程的线程池)
    // Executors 类提供了用于此包中所提供的执行程序服务的工厂方法
    ExecutorService pool = Executors.newCachedThreadPool();

      RWLogic logic = new RWLogic();

      Runnable writeTask1 = new Write("发工资", logic, new User("jzh", 15000));
      Runnable readTask1 = new Read("查工资", logic);
      Runnable writeTask2 = new Write("发工资", logic, new User("howsky", 13500));
      Runnable readTask2 = new Read("查工资", logic);
      Runnable writeTask3 = new Write("发工资", logic, new User("jiang", 11500));
      Runnable readTask3 = new Read("查工资", logic);
      
    pool.submit(writeTask1);
    pool.submit(readTask1);
    pool.submit(writeTask2);
    pool.submit(writeTask3);
    pool.submit(readTask2);
    pool.submit(readTask3);

    pool.shutdownNow();

  }
}
package thread.readwrite;

public class User {
  private String username;
  private double salary;

  public User(String username, double salary) {
    this.username = username;
    this.salary = salary;
  }

  public String getUsername() {
    return username;
  }

  public void setUsername(String username) {
    this.username = username;
  }

  public double getSalary() {
    return salary;
  }

  public void setSalary(double salary) {
    this.salary = salary;
  }
}
package thread.readwrite;

public class Write implements Runnable {
  private String name;
  private RWLogic logic;
  private User user;

  public Write(String name, RWLogic logic, User user) {
    this.name = name;
    this.logic = logic;
    this.user = user;
  }

  public void run() {
    logic.write(this.name, this.user);
    
    try {
      Thread.sleep(200);
    } catch (InterruptedException ex) {
    }
  }
}

 

发表回复

您的电子邮箱地址不会被公开。