[concurrency-interest] Is it possible to prevent out of order execution by using reader writer lock?

Yan Cheng CHEOK yccheok at yahoo.com
Tue Feb 1 09:36:06 EST 2011


Hello all,

>From http://java.sun.com/docs/books/jls/second_edition/html/memory.doc.html and http://g.oswego.edu/dl/jmm/cookbook.html, it is possible to prevent out of order execution by using "volatile" or "synchronized" method. 

However, may I know, is it possible to to use reader writer lock to prevent out of order execution?

Code which is having out of order execution problem
===================================================
void fun_by_thread_1() {
    this.isNuclearFactory = true;
    this.factory = new NuclearFactory();
}

void fun_by_thread_2() {
    Factory _factory = this.factory;
    if (this.isNuclearFactory) {
        // Do not operate nuclear factory!!!
        return;
    }
    // If out-of-order execution happens, _factory might 
    // be NuclearFactory instance.
    _factory.operate();
}

Factory factory = new FoodFactory();
boolean isNuclearFactory = false;



Solved by using volatile keyword
================================
void fun_by_thread_1() {
    this.isNuclearFactory = true;
    this.factory = new NuclearFactory();
}

void fun_by_thread_2() {
    Factory _factory = this.factory;
    if (this.isNuclearFactory) {
        // Do not operate nuclear factory!!!
        return;
    }
    // If out-of-order execution happens, _factory might 
    // be NuclearFactory instance.
    _factory.operate();
}

volatile Factory factory = new FoodFactory();
volatile boolean isNuclearFactory = false;



Solved by using synchronized keyword
====================================
void fun_by_thread_1() {
    synchronized(this) {
        this.isNuclearFactory = true;
        this.factory = new NuclearFactory();
    }
}

void fun_by_thread_2() {
    synchronized(this) {
        Factory _factory = this.factory;
        if (this.isNuclearFactory) {
            // Do not operate nuclear factory!!!
            return;
        }
    }
    // If out-of-order execution happens, _factory might 
    // be NuclearFactory instance.
    _factory.operate();
}

Factory factory = new FoodFactory();
boolean isNuclearFactory = false;



Solved by using Reader Writer Lock - Is this a correct way?
===========================================================
void fun_by_thread_1() {
    writerLock.lock();
    try {
        this.isNuclearFactory = true;
        this.factory = new NuclearFactory();
    } finally {
        writerLock.unlock();
    }
}

void fun_by_thread_2() {
    readerLock.lock();
    Factory _factory = this.factory;    
    try {        
        if (this.isNuclearFactory) {
            // Do not operate nuclear factory!!!
            return;
        }
    } finally {
        readerLock.unlock();
    }
    
    // If out-of-order execution happens, _factory might 
    // be NuclearFactory instance.
    _factory.operate();
}

Factory factory = new FoodFactory();
boolean isNuclearFactory = false;
private final java.util.concurrent.locks.Lock readerLock;
private final java.util.concurrent.locks.Lock writerLock;

Thanks and Regards
Yan Cheng CHEOK


      


More information about the Concurrency-interest mailing list