Import _thread
Import time
#1 thread functions
def print_time (thread_name,delaytime):
Count =0;
While Count <5:
Time.sleep (Delaytime);
Count + =1;
Print'%s---%s '% (Thread_name,time.ctime (Time.time ())))
#创建两个线程
Try
# function: Call the Start_new_thread () function in the _thread module to generate a new thread
# thread function, arguments passed to the thread function
_thread.start_new (Print_time, ("Thread-1",2))
_thread.start_new (Print_time, ("Thread-2",4))
Except
Print"Erro","Unable to start thread")
While1:
Pass
###################################################################
C:\python3.7\python.exe d:/python-test/Multithreading/multithreading. py
thread-1---Tue Nov28 09:24:26 2017
thread-2---Tue Nov28 09:24:28 2017
thread-1---Tue Nov28 09:24:28 2017
thread-1---Tue Nov28 09:24:30 2017
thread-2---Tue Nov28 09:24:32 2017
thread-1---Tue Nov28 09:24:32 2017
thread-1---Tue Nov28 09:24:34 2017
thread-2---Tue Nov28 09:24:36 2017
thread-2---Tue Nov28 09:24:40 2017
thread-2---Tue Nov28 09:24:44 2017
###################################################################
#2 Threading Module
Import threading
Import time
Exitflag =0
Class Mythread (threading. Thread):
' consists of class members, methods, and properties. From threading. Thread inherits the creation of a new subclass, and after instantiation calls the Start method to start a new thread, called the thread's Run method '
Def__INIT__ (Self,threadid,name,counter):
The ' _init_ method is a special method, called the constructor or initialization method of a class, which is called when an instance of the class is created.
Threading. Thread.__INIT__ (Self#self代表类的实例, self is necessary when defining a method of a class, although it is not necessary to pass in the corresponding parameter when calling
Self.threadid = ThreadID
Self.name = Name
Self.counter = Counter
def run (Self):
"""
Methods used to represent thread activity
"""
Print"Start Thread:" +Self.name)
Print_time (Self.name,Self.counter,5)
Print"Start Thread:" +Self.name)
def print_time (Threadname,delay,counter):
While counter:
If Exitflag:
Threadname.exit ()
Time.sleep (Delay)
Print"%s:%s"% (Threadname,time.ctime (Time.time ()))
Counter-=1
#创建新线程
Thread1 = Mythread (1,"Thread-1",1)
Thread2 = Mythread (2,"Thread-2",2)
#开启新线程
Thread1.start ()
Thread2.start ()
Thread1.join ()
Thread2.join ()#等待至线程中止.
Print"Exit main thread")
###################################################################
C:\python3.7\python.exe d:/python-test/Multithreading/multithreading. py
Start Thread: thread-1
Start Thread: thread-2
thread-1:tue Nov28 10:27:57 2017
thread-2:tue Nov28 10:27:58 2017
thread-1:tue Nov28 10:27:58 2017
thread-1:tue Nov28 10:27:59 2017
thread-2:tue Nov28 10:28:00 2017
thread-1:tue Nov28 10:28:00 2017
thread-1:tue: 01 2017
Start thread: thread-1
thread-2:tue: 02 2017
thread-2:tue: 04 2017
thread-2:tue: 06 2017
Start thread: thread-2
Exit main thread
###################################################################
Python Multithreading Exercises