Skip to content

Instantly share code, notes, and snippets.

@DengYiping
Created September 12, 2016 12:48
Show Gist options
  • Save DengYiping/45d5eb97ed142ad872f5cb6f4553d674 to your computer and use it in GitHub Desktop.
Save DengYiping/45d5eb97ed142ad872f5cb6f4553d674 to your computer and use it in GitHub Desktop.
package util
import java.util.concurrent.locks.ReentrantReadWriteLock
/**
* Created by Scott on 9/12/16.
*/
class ReadWriteLocker {
private val rwlock = new ReentrantReadWriteLock()
def readSync[T,R](func:T => R): T => R ={
def new_func(para:T):R = {
rwlock.readLock().lock()
val result = func(para)
rwlock.readLock().unlock()
result
}
new_func
}
def writeSync[T,R](func:T => R): T => R ={
def new_func(para:T):R = {
rwlock.writeLock().lock()
val result = func(para)
rwlock.writeLock().unlock()
result
}
new_func
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment