18034727885
2026-06-26 253f680fb9666751e0f9a8e5e8f2f9e3eb1894e3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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()
 
}