Skip to content

Commit 4337d63

Browse files
threads (revise)
1 parent 5808ae5 commit 4337d63

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

threads.py

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# THREAD's
2+
3+
import time
4+
import threading
5+
6+
7+
#Q1.Create a threading process such that it sleeps for 5 seconds and then prints out a message.
8+
9+
'''
10+
def task():
11+
print("Wait for 5 seconds")
12+
time.sleep(5)
13+
print("Hello")
14+
15+
threading.Thread(target=task).start()
16+
17+
'''
18+
19+
###############################
20+
21+
22+
#Q2. Make a thread that prints numbers from 1-10, waits for 1 sec between
23+
24+
'''
25+
def task():
26+
for i in range(1,11):
27+
print(i)
28+
time.sleep(1)
29+
30+
threading.Thread(target=task).start()
31+
32+
'''
33+
34+
#################################
35+
36+
37+
#Q3.Make a list that has 5 elements.Create a threading process that prints the 5 elements of the list with a delay of multiple of 2 sec between each display. Delay goes like 2sec-4sec-6sec-8sec-10sec
38+
39+
'''
40+
l1=[1,2,3,4,5]
41+
42+
def task():
43+
delay=2
44+
for i in l1:
45+
print(i)
46+
time.sleep(delay)
47+
delay+=2
48+
49+
threading.Thread(target=task).start()
50+
'''
51+
52+
#################################
53+
54+
55+
#Q4.Call factorial function using thread.
56+
57+
'''
58+
def fact(num,d):
59+
f=1
60+
for i in range(2,num+1):
61+
f*=i
62+
time.sleep(d)
63+
print("Factorial of %d is %d"%(num,f))
64+
65+
n=int(input("Enter number: "))
66+
threading.Thread(target=fact,args=(n,2)).start()
67+
'''
68+
69+
##################### END

0 commit comments

Comments
 (0)