Java并发编程之线程管理(基本线程同步4)

1.3   读写Lock

Lock机制最具重大意义的改进之一就是读写锁(ReadWriteLock)接口和ReentrantReadWriteLock类,这个类是唯一一个实现这个读写锁的类。在这个类中,有两个锁,一个事读操作,一个事写操作。有超过一个线程并行的进行读操作,但是写操作只能是某一个线程。当一个线程做写操作时,其它的线程不能能够进行读操作。

下面使用一个实例来说明这一点。定义一个读者和写者,分别读和写价格。

先定义价格打印类PrincesInfo:

 

importjava.util.concurrent.locks.ReadWriteLock;
importjava.util.concurrent.locks.ReentrantReadWriteLock;
 
/**
 * This class simulates the store oftwo prices. We will
 * have a writer that stores theprices and readers that
 * consult this prices
 *
 */
public class PricesInfo {
   
    /**
     * The two prices
     */
    private doubleprice1;
    private doubleprice2;
   
    /**
     * Lock to control the access to the prices
     */
    private ReadWriteLock lock;
   
    /**
     * Constructor of the class. Initializes theprices and the Lock
     */
    public PricesInfo(){
        price1=1.0;
        price2=2.0;
        lock=new ReentrantReadWriteLock();
    }
 
    /**
     * Returns the first price
     * @return the first price
     */
    public doublegetPrice1() {
        lock.readLock().lock();
        double value=price1;
        lock.readLock().unlock();
        return value;
    }
 
    /**
     * Returns the second price
     * @return the second price
     */
    public doublegetPrice2() {
        lock.readLock().lock();
        double value=price2;
        lock.readLock().unlock();
        return value;
    }
 
    /**
     * Establish the prices
     * @param price1 The price of the first product
     * @param price2 The price of the second product
     */
    public voidsetPrices(doubleprice1, doubleprice2) {
        lock.writeLock().lock();
        this.price1=price1;
        this.price2=price2;
        lock.writeLock().unlock();
    }
}

 

定义读者类Reader:

 

/**
 * This class implements a readerthat consults the prices
 *
 */
public class Reader implements Runnable {
 
    /**
     * Class that stores the prices
     */
    private PricesInfo pricesInfo;
   
    /**
     * Constructor of the class
     * @param pricesInfo object that stores the prices
     */
    public Reader (PricesInfo pricesInfo){
        this.pricesInfo=pricesInfo;
    }
   
    /**
     * Core method of the reader. Consults the twoprices and prints them
     * to the console
     */
    @Override
    public voidrun() {
        for (int i=0; i<10; i++){
            System.out.printf("%s: Price 1: %f\n",Thread.currentThread().getName(),pricesInfo.getPrice1());
            System.out.printf("%s: Price 2: %f\n",Thread.currentThread().getName(),pricesInfo.getPrice2());
        }
    }
 
}

定义写者类Writer:
/**
 * This class implements a writerthat establish the prices
 *
 */
public class Writer implements Runnable {
 
    /**
     * Class that stores the prices
     */
    private PricesInfo pricesInfo;
   
    /**
     * Constructor of the class
     * @param pricesInfo object that stores the prices
     */
    public Writer(PricesInfo pricesInfo){
        this.pricesInfo=pricesInfo;
    }
   
    /**
     * Core method of the writer. Establish theprices
     */
    @Override
    public voidrun() {
        for (int i=0; i<3; i++) {
            System.out.printf("Writer: Attempt to modify the prices.\n");
            pricesInfo.setPrices(Math.random()*10,Math.random()*8);
            System.out.printf("Writer: Prices have been modified.\n");
            try {
                Thread.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
 
}

定义测试类ReadWriteLock:

public class ReadWriteLockTest {
 
    /**
     * Main class of the example
     * @param args
     */
    public staticvoidmain(String[] args) {
 
        // Creates an object to store the prices
        PricesInfo pricesInfo=new PricesInfo();
       
        Reader readers[]=new Reader[5];
        Thread threadsReader[]=new Thread[5];
       
        // Creates five readers and threads to run them
        for (int i=0; i<5; i++){
            readers[i]=new Reader(pricesInfo);
            threadsReader[i]=new Thread(readers[i]);
        }
       
        // Creates a writer and a thread to run it
        Writer writer=new Writer(pricesInfo);
        Thread threadWriter=new Thread(writer);
       
        // Starts the threads
        for (int i=0; i<5; i++){
            threadsReader[i].start();
        }
        threadWriter.start();
       
    }
 
}


运行结果:

Writer:Attempt to modify the prices.
Writer:Prices have been modified.
Thread-0:Price 1: 1.000000
Thread-3:Price 1: 1.000000
Thread-1:Price 1: 1.000000
Thread-1:Price 2: 1.844894
Thread-1:Price 1: 8.698622
Thread-1:Price 2: 1.844894
Thread-1:Price 1: 8.698622
Thread-1:Price 2: 1.844894
Thread-1:Price 1: 8.698622
Thread-1:Price 2: 1.844894
Thread-1:Price 1: 8.698622
Thread-1:Price 2: 1.844894
Thread-1:Price 1: 8.698622
Thread-1:Price 2: 1.844894
Thread-1:Price 1: 8.698622
Thread-1:Price 2: 1.844894
Thread-1:Price 1: 8.698622
Thread-1:Price 2: 1.844894
Thread-1:Price 1: 8.698622
Thread-1:Price 2: 1.844894
Thread-1:Price 1: 8.698622
Thread-1:Price 2: 1.844894
Thread-4:Price 1: 1.000000
Thread-4:Price 2: 1.844894
Thread-4:Price 1: 8.698622
Thread-4:Price 2: 1.844894
Thread-4:Price 1: 8.698622
Thread-4:Price 2: 1.844894
Thread-4:Price 1: 8.698622
Thread-4:Price 2: 1.844894
Thread-4:Price 1: 8.698622
Thread-4:Price 2: 1.844894
Thread-4:Price 1: 8.698622
Thread-4:Price 2: 1.844894
Thread-4:Price 1: 8.698622
Thread-4:Price 2: 1.844894
Thread-4:Price 1: 8.698622
Thread-4:Price 2: 1.844894
Thread-4:Price 1: 8.698622
Thread-4:Price 2: 1.844894
Thread-4:Price 1: 8.698622
Thread-4:Price 2: 1.844894
Thread-2:Price 1: 1.000000
Thread-3:Price 2: 1.844894
Thread-0:Price 2: 1.844894
Thread-0:Price 1: 8.698622
Thread-0:Price 2: 1.844894
Thread-0:Price 1: 8.698622
Thread-0:Price 2: 1.844894
Writer:Attempt to modify the prices.
Thread-0:Price 1: 8.698622
Thread-3:Price 1: 8.698622
Thread-2:Price 2: 1.844894
Thread-3:Price 2: 6.717506
Thread-0:Price 2: 6.717506
Thread-0:Price 1: 6.478280
Thread-0:Price 2: 6.717506
Writer:Prices have been modified.
Thread-0:Price 1: 6.478280
Thread-0:Price 2: 6.717506
Thread-0:Price 1: 6.478280
Thread-0:Price 2: 6.717506
Thread-0:Price 1: 6.478280
Thread-0:Price 2: 6.717506
Thread-0:Price 1: 6.478280
Thread-0:Price 2: 6.717506
Thread-0:Price 1: 6.478280
Thread-0:Price 2: 6.717506
Thread-3:Price 1: 6.478280
Thread-3:Price 2: 6.717506
Thread-3:Price 1: 6.478280
Thread-3:Price 2: 6.717506
Thread-3:Price 1: 6.478280
Thread-3:Price 2: 6.717506
Thread-3:Price 1: 6.478280
Thread-3:Price 2: 6.717506
Thread-3:Price 1: 6.478280
Thread-3:Price 2: 6.717506
Thread-3:Price 1: 6.478280
Thread-3:Price 2: 6.717506
Thread-3:Price 1: 6.478280
Thread-3:Price 2: 6.717506
Thread-3:Price 1: 6.478280
Thread-3:Price 2: 6.717506
Thread-2:Price 1: 6.478280
Thread-2:Price 2: 6.717506
Thread-2:Price 1: 6.478280
Thread-2:Price 2: 6.717506
Writer:Attempt to modify the prices.
Writer:Prices have been modified.
Thread-2:Price 1: 6.478280
Thread-2:Price 2: 7.824052
Thread-2:Price 1: 7.911020
Thread-2:Price 2: 7.824052
Thread-2:Price 1: 7.911020
Thread-2:Price 2: 7.824052
Thread-2:Price 1: 7.911020
Thread-2:Price 2: 7.824052
Thread-2:Price 1: 7.911020
Thread-2:Price 2: 7.824052
Thread-2:Price 1: 7.911020
Thread-2:Price 2: 7.824052
Thread-2:Price 1: 7.911020
Thread-2:Price 2: 7.824052


Java并发编程之线程管理(基本线程同步4),布布扣,bubuko.com

Java并发编程之线程管理(基本线程同步4)

上一篇:nginx 中http协议的相关配置


下一篇:HTML 常用标签