File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change
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!!!"
You can’t perform that action at this time.
0 commit comments