0%

6种方式实现多线程交替打印10次ABC

考察点

  • 线程同步工具熟悉和理解程度
  • 代码规范,是否注重代码可读性、整洁性,考察编码习惯
  • 代码熟练度,具体语言掌握情况
  • 能否说出每种实现的原理、细节 注意事项、适用场景

synchronized

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
private const val STATE_A = 1
private const val STATE_B = 2
private const val STATE_C = 3

@Volatile
private var state = STATE_A

fun main() {
val lock = Object()
Thread {
repeat(10) {
synchronized(lock) {
while (state != STATE_A) lock.wait()
println("A")
state = STATE_B
lock.notifyAll()
}
}
}.start()
Thread {
repeat(10) {
synchronized(lock) {
while (state != STATE_B) lock.wait()
println("B")
state = STATE_C
lock.notifyAll()
}
}
}.start()
Thread {
repeat(10) {
synchronized(lock) {
while (state != STATE_C) lock.wait()
println("C")
state = STATE_A
lock.notifyAll()
}
}
}.start()
}

ReentrantLock

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
import java.util.concurrent.locks.ReentrantLock
import kotlin.concurrent.withLock

private const val STATE_A = 1
private const val STATE_B = 2
private const val STATE_C = 3

@Volatile
private var state = STATE_A

fun main() {
val lock = ReentrantLock()
val c1 = lock.newCondition()
val c2 = lock.newCondition()
val c3 = lock.newCondition()
Thread {
repeat(10) {
lock.withLock {
while (state != STATE_A) c1.await()
println("A")
state = STATE_B
c2.signal()
}
}
}.start()
Thread {
repeat(10) {
lock.withLock {
while (state != STATE_B) c2.await()
println("B")
state = STATE_C
c3.signal()
}
}
}.start()
Thread {
repeat(10) {
lock.withLock {
while (state != STATE_C) c3.await()
println("C")
state = STATE_A
c1.signal()
}
}
}.start()
}

Sempahore

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
import java.util.concurrent.Semaphore

fun main() {
val s1 = Semaphore(1)
val s2 = Semaphore(0)
val s3 = Semaphore(0)
Thread {
repeat(10) {
s1.acquire()
println("A")
s2.release()
}
}.start()
Thread {
repeat(10) {
s2.acquire()
println("B")
s3.release()
}
}.start()
Thread {
repeat(10) {
s3.acquire()
println("C")
s1.release()
}
}.start()
}

CountDownLatch

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
import java.util.concurrent.CountDownLatch

fun main() {
var latchA = CountDownLatch(1)
var latchB = CountDownLatch(1)
var latchC = CountDownLatch(1)
Thread {
repeat(10) {
latchA.await()
latchA = CountDownLatch(1)
println("A")
latchB.countDown()
}
}.start()
Thread {
repeat(10) {
latchB.await()
latchB = CountDownLatch(1)
println("B")
latchC.countDown()
}
}.start()
Thread {
repeat(10) {
latchC.await()
latchC = CountDownLatch(1)
println("C")
latchA.countDown()
}
}.start()
latchA.countDown()
}

CyclicBarrier

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
import java.util.concurrent.CyclicBarrier

fun main() {
val cb1 = CyclicBarrier(2)
val cb2 = CyclicBarrier(2)
val cb3 = CyclicBarrier(2)
Thread {
repeat(10) {
cb3.await()
println("A")
cb1.await()
}
}.start()
Thread {
repeat(10) {
cb1.await()
println("B")
cb2.await()
}
}.start()
Thread {
repeat(10) {
cb2.await()
println("C")
cb3.await()
}
}.start()
cb3.await()
}

无锁自旋

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
private const val STATE_A = 1
private const val STATE_B = 2
private const val STATE_C = 3

@Volatile
private var state = STATE_A

fun main() {
Thread {
repeat(10) {
while (state != STATE_A) {
}
println("A")
state = STATE_B
}
}.start()
Thread {
repeat(10) {
while (state != STATE_B) {
}
println("B")
state = STATE_C
}
}.start()
Thread {
repeat(10) {
while (state != STATE_C) {
}
println("C")
state = STATE_A
}
}.start()
}