-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcode0.py
More file actions
35 lines (24 loc) · 892 Bytes
/
code0.py
File metadata and controls
35 lines (24 loc) · 892 Bytes
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
# code0.py
import threading
import time
def worker():
print("{} starting...".format(threading.currentThread().getName()))
# This stops the thread execution for 2 seconds.
time.sleep(2)
print("{} exiting...".format(threading.currentThread().getName()))
def service():
print("{} starting...".format(threading.currentThread().getName()))
# This stops the thread execution for 4 seconds.
time.sleep(4)
print("{} exiting...".format(threading.currentThread().getName()))
# We create two named threads
t1 = threading.Thread(name='Thread 1', target=service)
w1 = threading.Thread(name='Thread 2', target=worker)
# This uses the default name (Thread-i)
w2 = threading.Thread(target=worker)
# All threads are executed
w1.start()
w2.start()
t1.start()
# The following will be printed before the threads finish executing
print('\nThree threads were created\n')