leetcode 1114 Print in Order, sample concurrency in Python
Problem
Suppose we have a class:
2
3
4
5
public void first() { print("first"); }
public void second() { print("second"); }
public void third() { print("third"); }
}The same instance of
Foowill be passed to three different threads. Thread A will callfirst(), thread B will callsecond(), and thread C will callthird(). Design a mechanism and modify the program to ensure thatsecond()is executed afterfirst(), andthird()is executed aftersecond().
Print in order is a simple concurrency programming problem, especially in Python. But it’s my first concurrency program in Python, so record in here.
Solution
I simply use two locks to achieve synchoronization.
| 1 | import threading |