专业房产网站建设公司排名,seo优化提升排名,外贸营销主题怎么写,作文生成器编写多线程Java应用程序并不是小菜一碟。 必须格外小心#xff0c;因为同步不良会使您的应用程序一s不振。 JVM堆由所有线程共享。 如果多个线程需要同时使用相同的对象或静态类变量#xff0c;则必须谨慎管理对共享数据的线程访问。 从1.5版开始#xff0c;JSDK中包含了在并… 编写多线程Java应用程序并不是小菜一碟。 必须格外小心因为同步不良会使您的应用程序一s不振。 JVM堆由所有线程共享。 如果多个线程需要同时使用相同的对象或静态类变量则必须谨慎管理对共享数据的线程访问。 从1.5版开始JSDK中包含了在并发编程中通常有用的实用程序类。 在Java中 sync关键字用于获取对象的排他锁。 当线程获取对象的锁以进行读取或写入时其他线程必须等待直到该对象的锁被释放。 想想一个场景有许多读取器线程经常读取共享数据而只有一个写入器线程更新共享数据。 读取时不必排他地锁定对共享数据的访问因为除非有写入操作否则可以并行执行多个读取操作。 在本文中我将提供Java 1.5 API文档中引入的ReadWriteLock接口的示例用法。 在Java Api文档中它说 ReadWriteLock维护一对关联的锁 一种用于只读操作另一种用于写操作。 读取锁可以由多个读取器线程同时保持 只要没有作家。 写锁是排他的。 读取器线程可以同时读取共享数据。 读取操作不会阻止其他读取操作。 执行SQL SELECT语句时就是这种情况。 但是写操作是排他的。 这意味着当写入器线程持有用于修改共享数据的锁时所有读取器和其他写入器均被阻止。 Writer.java此类表示更新共享数据的线程。 Writer使用ReadWriteLock的WriteLock专门锁定对字典的访问。 package deneme.readwritelock;public class Writer extends Thread{private boolean runForestRun true;private Dictionary dictionary null;public Writer(Dictionary d, String threadName) {this.dictionary d;this.setName(threadName);}Overridepublic void run() {while (this.runForestRun) { String [] keys dictionary.getKeys();for (String key : keys) {String newValue getNewValueFromDatastore(key);//updating dictionary with WRITE LOCKdictionary.set(key, newValue);}//update every secondstry {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}public void stopWriter(){this.runForestRun false;this.interrupt();}public String getNewValueFromDatastore(String key){//This part is not implemented. Out of scope of this artilereturn newValue;}} Reader.java此类表示读取共享数据的线程。 package deneme.readwritelock;public class Reader extends Thread{private Dictionary dictionary null;public Reader(Dictionary d, String threadName) {this.dictionary d;this.setName(threadName);}private boolean runForestRun true;Overridepublic void run() {while (runForestRun) {String [] keys dictionary.getKeys();for (String key : keys) {//reading from dictionary with READ LOCKString value dictionary.get(key);//make what ever you want with the value.System.out.println(key : value);}//update every secondstry {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}public void stopReader(){this.runForestRun false;this.interrupt();}} Dictionary.java这是一个简单且线程安全的字典。 读操作通过ReadLock进行管理写操作更新通过WriteLock进行管理。 package deneme.readwritelock;import java.util.HashMap;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantReadWriteLock;public class Dictionary {private final ReentrantReadWriteLock readWriteLock new ReentrantReadWriteLock();private final Lock read readWriteLock.readLock();private final Lock write readWriteLock.writeLock();private HashMapString, String dictionary new HashMapString, String();public void set(String key, String value) {write.lock();try {dictionary.put(key, value);} finally {write.unlock();}}public String get(String key) {read.lock();try{return dictionary.get(key);} finally {read.unlock();}}public String[] getKeys(){read.lock();try{String keys[] new String[dictionary.size()];return dictionary.keySet().toArray(keys);} finally {read.unlock();}}public static void main(String[] args) {Dictionary dictionary new Dictionary();dictionary.set(java, object oriented);dictionary.set(linux, rulez);Writer writer new Writer(dictionary, Mr. Writer);Reader reader1 new Reader(dictionary ,Mrs Reader 1);Reader reader2 new Reader(dictionary ,Mrs Reader 2);Reader reader3 new Reader(dictionary ,Mrs Reader 3);Reader reader4 new Reader(dictionary ,Mrs Reader 4);Reader reader5 new Reader(dictionary ,Mrs Reader 5);writer.start();reader1.start();reader2.start();reader3.start();reader4.start();reader5.start();}} 参考来自我们的JCG合作伙伴 Ilkin Ulas的Java中的ReadWriteLock示例 您的所有博客都属于我们博客。 翻译自: https://www.javacodegeeks.com/2012/04/java-concurrency-with-readwritelock.html