Skip to content

Commit 8fcd8f9

Browse files
Add program to print current date
1 parent 79e6815 commit 8fcd8f9

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

app/script_21.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#Python multithreading example to print current date.
2+
#1. Define a subclass using Thread class.
3+
#2. Instantiate the subclass and trigger the thread.
4+
5+
import threading
6+
import datetime
7+
8+
class myThread (threading.Thread):
9+
def __init__(self, name, counter):
10+
threading.Thread.__init__(self)
11+
self.threadID = counter
12+
self.name = name
13+
self.counter = counter
14+
def run(self):
15+
print "Starting " + self.name
16+
print_date(self.name, self.counter)
17+
print "Exiting " + self.name
18+
19+
def print_date(threadName, counter):
20+
datefields = []
21+
today = datetime.date.today()
22+
datefields.append(today)
23+
print "%s[%d]: %s" % ( threadName, counter, datefields[0] )
24+
25+
# Create new threads
26+
thread1 = myThread("Thread", 1)
27+
thread2 = myThread("Thread", 2)
28+
29+
# Start new Threads
30+
thread1.start()
31+
thread2.start()
32+
33+
thread1.join()
34+
thread2.join()
35+
print "Exiting the Program!!!"

0 commit comments

Comments
 (0)