Java并发编程之线程管理(基本线程同步4)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java并发编程之线程管理(基本线程同步4),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6542字,纯文字阅读大概需要10分钟。
内容图文
![Java并发编程之线程管理(基本线程同步4)](/upload/InfoBanner/zyjiaocheng/1105/cec54a13a4e1460788ac85bee7b7980a.jpg)
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
原文:http://blog.csdn.net/john_f_lau/article/details/22223467
内容总结
以上是互联网集市为您收集整理的Java并发编程之线程管理(基本线程同步4)全部内容,希望文章能够帮你解决Java并发编程之线程管理(基本线程同步4)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。