校招刷题群
高效刷题 迎战校招
校招精选试题
近年面笔经面经群内分享
Java刷题群 前端刷题群 产品运营群
首页 > 专业课 > 线程相关
题目

python多线程实现循环打印 abc

解答

非阻塞版

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
import threading
import time
 
def print_a():
    global value
    global lock
    global stop_flag
    while stop_flag:
        while True:
            if value == 0 or value == 3:
                break
        lock.acquire()
        value = 1
        time.sleep(1)
        print("aaa")
        lock.release() 
 
def print_b():
    global value
    global lock
    global stop_flag
    while stop_flag:
        while True:
            if value == 1:
                break
        lock.acquire()
        value = 2
        time.sleep(1)
        print("bbb")
        lock.release()
 
def print_c():
    global value
    global lock
    global stop_flag
    while stop_flag:
        while True:
            if value == 2:
                break
        lock.acquire()
        value = 3
        time.sleep(1)
        print("ccc")
        lock.release()
 
if __name__ == "__main__":
    stop_flag = True
    value = 0
    threads = []
    lock = threading.Lock()
    thread_a = threading.Thread(target=print_a)
    thread_b = threading.Thread(target=print_b)
    thread_c = threading.Thread(target=print_c)
    threads.append(thread_a)
    threads.append(thread_b)
    threads.append(thread_c)
 
    for thread in threads:
        thread.start()
    time.sleep(5)
    stop_flag = False

阻塞版

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
66
67
68
69
70
71
72
73
74
75
76
77
import threading
import time
 
def print_a():
    global value
    global stop_flag
    global lock
    global con
    while stop_flag:
        try:
            lock.acquire()
            while value != 0 and value != 3:
                con.wait()
            time.sleep(1)
            value = 1
            print("aaa")
            con.notify_all()
        finally:
            lock.release()
 
 
def print_b():
    global value
    global stop_flag
    global lock
    global con
    while stop_flag:
        try:
            lock.acquire()
            while value != 1:
                con.wait()
            time.sleep(1)
            value = 2
            print("bbb")
            con.notify_all()
        finally:
            lock.release()
     
 
def print_c():
    global value
    global stop_flag
    global lock
    global con
    while stop_flag:
        try:           
            lock.acquire()
            while value != 2:
                con.wait()
            time.sleep(1)
            value = 3
            print("ccc")
            con.notify_all()
        finally:
            lock.release()
 
if __name__ == "__main__":
    stop_flag = True
    value = 0
    threads = []
    # 注意这里使用的是条件变量
    lock = threading.Lock()
    con = threading.Condition(lock=lock)
    thread_a = threading.Thread(target=print_a)
    thread_b = threading.Thread(target=print_b)
    thread_c = threading.Thread(target=print_c)
    threads.append(thread_a)
    threads.append(thread_b)
    threads.append(thread_c)
 
    for thread in threads:
        thread.start()
    time.sleep(5)
    print("stop")
    stop_flag = False
    for thread in threads:
        thread.join()
C 0条回复 评论

帖子还没人回复快来抢沙发