1.
import threading
def new():
for x in range(6):
print("Child Thread Here!!")
t1=Thread(target=new)
t1.start()
t1.join()
print("Main Thread Here!!")
2. import time
import threading
def calc_square(number):
print("Calculate square numbers: ")
for i in numbers:
time.sleep(0.2) #artificial time-delay
print('square: ', str(n*n))
def calc_cube(number):
print("Calculate cube numbers: ")
for i in numbers:
time.sleep(0.2)
print('cube: ', str(n*n*n))
arr = [2,3,8,9]
t = time.time()
t1 = threading.Thread(target = cal_square,args=(arr,))
t2 = threading.Thread(target = cal_cube,args=(arr,))
# creating two threads here t1 & t2
t1.start()
t2.start()
# starting threads here parallelly by usign start function.
t1.join() # this join() will wait until the cal_square() function is finised.
t2.join() # this join() will wait unit the cal_cube() function is finised.
print("Successed!")
3. import _thread
import time
# Define a function for the thread
def print_time( threadName, delay):
count = 0
while count < 5:
time.sleep(delay)
count += 1
print ("%s: %s" % ( threadName, time.ctime(time.time()) ))
# Create two threads as follows
try:
_thread.start_new_thread( print_time, ("Thread-1", 2, ) )
_thread.start_new_thread( print_time, ("Thread-2", 4, ) )
except:
print ("Error: unable to start thread")
while 1:
pass
4.
import threading
import time
class myThread (threading.Thread):
def __init__(self, threadID, name, counter):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.counter = counter
def run(self):
print ("Starting " + self.name)
# Get lock to synchronize threads
threadLock.acquire()
print_time(self.name, self.counter, 3)
# Free lock to release next thread
threadLock.release()
def print_time(threadName, delay, counter):
while counter:
time.sleep(delay)
print ("%s: %s" % (threadName, time.ctime(time.time())))
counter -= 1
threadLock = threading.Lock()
threads = []
# Create new threads
thread1 = myThread(1, "Thread-1", 1)
thread2 = myThread(2, "Thread-2", 2)
# Start new Threads
thread1.start()
thread2.start()
# Add threads to thread list
threads.append(thread1)
threads.append(thread2)
# Wait for all threads to complete
for t in threads:
t.join()
print ("Exiting Main Thread")
Methods of Thread class in Python
1. active_count()
2. enumerate()
3. isAlive()
4. join()
5. join(seconds)
1. Program: active_count() method of Thread class in Python
from threading import *
import time
def display():
print(current_thread().getName(), "...started")
time.sleep(3)
print(current_thread().getName(), "...ended")
print("The Number of active Threads:", active_count())
t1=Thread(target=display, name="ChildThread1")
t2=Thread(target=display, name="ChildThread2")
t3=Thread(target=display, name="ChildThread3")
t1.start()
t2.start()
t3.start()
print("The Number of active Threads:", active_count())
time.sleep(5)
print("The Number of active Threads:", active_count())
2. Program: enumerate() function of Thread class (demo10.py)
from threading import *
import time
def display():
print(current_thread().getName(), "...started")
time.sleep(3)
print(current_thread().getName(), "...ended")
t1=Thread(target=display, name="ChildThread1")
t2=Thread(target=display, name="ChildThread2")
t3=Thread(target=display, name="ChildThread3")
t1.start()
t2.start()
t3.start()
l=enumerate()
for t in l:
print("Thread Name:", t.name)
time.sleep(5)
3.Program: isAlive() method in Python
from threading import *
import time
def display():
print(current_thread().getName(), "...started")
time.sleep(3)
print(current_thread().getName(), "...ended")
print("The Number of active Threads:", active_count())
t1=Thread(target=display, name="ChildThread1")
t2=Thread(target=display, name="ChildThread2")
t1.start()
t2.start()
print(t1.name,"is Alive :",t1.isAlive())
print(t2.name,"is Alive :",t2.isAlive())
time.sleep(5)
print(t1.name,"is Alive :",t1.isAlive())
print(t2.name,"is Alive :",t2.isAlive())
4.timer program
import threading
import time
def hello():
print("hello, Timer")
if __name__ == '__main__':
t = threading.Timer(3.0, hello)
t.start()
After 3 seconds, "hello, Timer" will be printed.
#Take code from textbook and modify
5.implement producer consumer problem
6.fibonacci,factorial,summation