Concurrent Programming [first post] thread

 A Both invocation thread

- Direct the called function

import threading
import time
def myThreading():
    time.sleep(2)
    print('子线程正在运行',time.ctime())

def myThreading1():
    time.sleep(4)
    print('子线程1正在运行',time.ctime())


if __name__ == '__main__':
    q = threading.Thread(target=myThreading)
    q1 = threading.Thread(target=myThreading1)

    # q.setDaemon(True)
    #q1.setDaemon (True) 

    q.start () 
    q1.start () 

    # q.join () 
    # q1.join () 

    Print ( ' the main thread running ' , time.ctime ())

 

 

- inherited from the parent class Threading, run by rewriting function implementation (execution start () function, the function will be called to run)

Import Threading
 Import Time 


class myThreading (of the threading.Thread):
     # the time.sleep (2) 
    # Print ( 'child thread running') 
    DEF  the __init__ (Self, name): 
        . Super () the __init__ () 
        the self.name = name
     DEF rUN (Self): 
        the time.sleep ( 2 )
         Print ( ' child thread running {} ' .format (the self.name)) 

class myThreading1 (of the threading.Thread):
     # the time.sleep (. 4) 
    # Print ( 'sub-thread 1 running ') 
    DEF  __init__(self,name):
        super().__init__()
        self.name = name
    def run(self):
        time.sleep(4)
        print('子线程{}正在运行'.format(self.name))

if __name__ == '__main__':
    q = myThreading(1)
    q1 = myThreading1(2)

    # q.setDaemon(True)
    # q1.setDaemon(True)

    q.start()
    q1.start()

    # q.join()
    # q1.join()

    Print ( ' the main thread running ' )

 

Resource same process multiple threads share the process of

Import Threading 
n- = 100 # should be defined in a global variable in the windows system '__main__' on it if __name__ == 
DEF Work ():
     Global n- 
    n- = 0
     Print ( ' the sub-process: ' , n-) 

# thread direct memory space is shared 
IF  the __name__ == ' __main__ ' : 
    P = of the threading.Thread (target = Work) 
    p.start () 
    Print ( ' the main process: ' , n-)

 

 

To be continued. . . . . . . Synchronization locks, deadlocks, thread queue

Guess you like

Origin www.cnblogs.com/xxp1624/p/11256714.html