Created
November 22, 2018 10:31
-
-
Save inikolaev/2659e0213a5a087fcddee223a3d4e5fd to your computer and use it in GitHub Desktop.
Demonstration of write lock with upgrade in Kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.concurrent.locks.ReentrantReadWriteLock | |
import kotlin.concurrent.read | |
import kotlin.concurrent.write | |
fun lockWithoutUpgrade() { | |
val lock = ReentrantReadWriteLock() | |
println("Acquire read lock") | |
lock.readLock().lock() | |
println("Inside read lock") | |
println("Acquire write lock") | |
lock.writeLock().lock() | |
println("Inside write lock") | |
lock.writeLock().unlock() | |
println("Released write lock") | |
lock.readLock().unlock() | |
println("Released read lock") | |
} | |
fun lockWithUpgrade() { | |
val lock = ReentrantReadWriteLock() | |
println("Acquire read lock") | |
lock.read { | |
println("Inside read lock") | |
println("Acquire write lock") | |
lock.write { | |
println("Inside write lock") | |
} | |
println("Released write lock") | |
} | |
println("Released read lock") | |
} | |
fun main(args: Array<String>) { | |
lockWithUpgrade() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment