[Python] Multithreading-1

# Exercise: create a thread 
from threading import Thread
 import time

def run(a = None, b = None) :
  print a, b 
  time.sleep(1)

t = Thread(target = run, name= " wangjing " ,args = ( " this is a " , " thread " ))
 #The thread is in a new state at this time

print t.getName() #Get the thread object name 
print t.isAlive() #Determine whether the thread is still alive, after start 
, call isAlive() before the execution is completed to return True 
t.start() #Start the thread # print t.isAlive() 
t.join(   ) #The main thread waits for the execution of the sub-thread t to end print " done! "
 


#Exercise : create a thread using the inherited method 
from threading import Thread
 import time

class MyThread(Thread) :
   def  __init__ (self, a) :
     #Call the constructor of the parent class 
    super(MyThread, self). __init__ ()
    self.a = a

  def run(self) :
    time.sleep(self.a)
    print "sleep :", self.a
 
t1 = MyThread(2)
t2 = MyThread(4 )
 #The execution of multiple threads has no order. This example mainly increases the waiting time for a long time, so it seems that there is an execution order 
t1.start()
t2.start()
t1.join()
t2.join()
print "done!"



#练习:线程
import threading  
import time  
class timer(threading.Thread): 
    #The timer class is derived from the class threading.Thread
    def __init__(self, num, interval):  
        threading.Thread.__init__(self)  
        self.thread_num = num  
        self.interval = interval  
        self.thread_stop = False  

    def run(self): 
        #Overwrite run() method, put what you want the thread do here  
        while not self.thread_stop:  
            print 'Thread Object(%d), Time:%s\n' %(self.thread_num, time.ctime())  
            time.sleep(self.interval)  
    def stop(self):  
        self.thread_stop = True  
 
def test():  
    thread1 = timer(1, 1)  
    thread2 = timer(2, 2)  
    thread1.start() #Execute   the run method in the class 
    thread2.start()  
    time.sleep( 10)   #Here is the main thread waiting for 10 seconds 
    thread1.stop()  
    thread2.stop() #Stop   the thread # The stop here is called by the main thread 
if __name__ == ' __main__ ' :   
 
    test()


#Exercise : start another thread in the thread 
import threading
 import time
 def context(tJoin):
     print  ' in threadContext. '
    tJoin.start()
    # will block tContext until threadJoin terminates 
    tJoin.join()
     # continue to execute 
    print  ' out threadContext. ' after tJoin terminates

def join1():
    print 'in threadJoin.'
    time.sleep(1)
    print 'out threadJoin.'

if __name__=="__main__":
    tJoin = threading.Thread(target = join1) #Pass
     the thread object as a parameter to another thread 
    tContext = threading.Thread(target = context, args = (tJoin,))
    tContext.start()


#Exercise : set the child thread as a daemon thread 
import threading  
 import time  
 class MyThread(threading.Thread):  
   def  __init__ (self, id):  
    threading.Thread.__init__(self)  

  def run(self):  
    time.sleep(5)  
    print "This is " + self.getName()  

if __name__ == "__main__":  
  t1 = MyThread(999 )  
   # t1.setDaemon(True) #Set 
the child thread as a daemon thread. After setting it as a daemon thread, the main thread exits the child thread and then exits to prevent the child thread from hanging 
  t1.start()  
   print  " I am the father thread. "

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324385880&siteId=291194637