-
Notifications
You must be signed in to change notification settings - Fork 4
/
thread_subclass.py
39 lines (30 loc) · 1.02 KB
/
thread_subclass.py
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
## To use threads you need import Thread using the following code:
from threading import Thread
##Also we use the sleep function to make the thread "sleep"
from time import sleep
## To create a thread in Python you'll want to make your class work as a thread.
## For this, you should subclass your class from the Thread class
class tryThread(Thread):
def __init__(self):
Thread.__init__(self)
self.message = "Hello Parallel Thread!!\n"
##this method thod prints only the message
def print_message(self):
print (self.message)
##The run method prints ten times the message
def run(self):
print ("Thread Starting\n")
x=0
while (x < 10):
self.print_message()
sleep(2)
x += 1
print ("Thread Ended\n")
#start the main process
print ("Process Started")
# create an instance of the HelloWorld class
hello_Python = tryThread()
# print the message...starting the thread
hello_Python.start()
#end the main process
print ("Process Ended")