Relationship of program \ Process \ Thread:
Programs (program)
A static description of a set of feature sets, with at least one process in the program
Processes (Process)
Processes are resources allocated and dispatched by the system, they have their own separate space, and the process has at least one thread
Threads (thread)
Threads are the basic unit of processor scheduling, their space is shared, and there can be multiple threads in a process
Advantages of Multithreading:
Using threads can put the tasks of a long-time program into the background to handle it, making the user experience even better. For example, a user clicked a button to trigger
Some time processing, you can pop up a progress bar to show the progress of processing
" "The main purpose of this code: Understanding multithreaded running logic familiar syntax and methods" "ImportThreadingImportTime#used for pauses.classMyThread (Threading. Thread):#with multi-threading, you must inherit the parent class threading. Thread def __init__(Self,threadid,name,counter): Threading. Thread.__init__(self)#fixed format, equivalent to initialization of threadsSelf.threadid =ThreadID Self.name=name Self.counter=counterPrint('initialization Complete') defRun (self):#you cannot determine the order in which run () executes between different threads, which is determined by CPU processing . Print("Start"+self.name) print_time (Self.name,self.counter,5) Print("End"+self.name)defPrint_time (threadname,counter,delay): whileCounter:time.sleep (Delay)#Pause 5s Print("%s:%s"% (Threadname,time.ctime (Time.time ())))#print thread name and timeCounter = counter-1#Create a thread, generate two objectsThread1 = MyThread (1,"Thread-1", 1) Thread2= MyThread (2,"Thread-2", 2)#Open ThreadThread1.start ()#starting the thread, each thread object must call the function at least once, and he will automatically call the run () methodThread2.start ()
Different results may appear, different in the order in which they appear
Initialization complete
Initialization complete
Start Thread-1
Start Thread-2
Thread-1:sat June 17 00:39:50 2017
End Thread-1
Thread-2:sat June 17 00:39:50 2017
Thread-2:sat June 17 00:39:55 2017
End Thread-2
python-Multithreading 1