package com.example.basecore
|
|
import java.util.concurrent.locks.Condition
|
import java.util.concurrent.locks.Lock
|
import java.util.concurrent.locks.ReentrantLock
|
|
|
/**
|
* Example local unit test, which will execute on the development machine (host).
|
*
|
* See [testing documentation](http://d.android.com/tools/testing).
|
*/
|
class ExampleUnitTest {
|
var count = 1
|
val lock = ReentrantLock()
|
val n1 = lock.newCondition()
|
val n2 = lock.newCondition()
|
val n3 = lock.newCondition()
|
val t1 = Thread(object : Runnable {
|
override fun run() {
|
while (true) {
|
lock.lock()
|
if (count %3 != 1) {
|
n1.await()
|
return
|
}
|
println("count is $count")
|
n1.signalAll()
|
lock.unlock()
|
}
|
}
|
|
}).start()
|
val t2 = Thread(object : Runnable {
|
override fun run() {
|
while (true) {
|
lock.lock()
|
if (count % 3 != 2) {
|
n2.await()
|
return
|
}
|
println("count is $count")
|
n2.signalAll()
|
lock.unlock()
|
}
|
}
|
|
}).start()
|
val t3 = Thread(object : Runnable {
|
override fun run() {
|
while (true) {
|
lock.lock()
|
if (count %3 != 0) {
|
n3.await()
|
return
|
}
|
println("count is $count")
|
n3.signalAll()
|
lock.unlock()
|
}
|
}
|
|
}).start()
|
|
}
|