Use of threading.Thread.join()

后端 未结 3 1605
迷失自我
迷失自我 2020-12-08 07:22

I am new to multithreading in python and trying to learn multithreading using threading module. I have made a very simple program of multi threading and i am having trouble

3条回答
  •  猫巷女王i
    2020-12-08 08:21

    I modified the code so that you will understand how exactly join works. so run this code with comments and without comments and observe the output for both.

    val = 0
    
    def increment(msg,sleep_time):
       global val 
       print "Inside increment"
       for x in range(10):
           val += 1
           print "%s : %d\n" % (msg,val)
           time.sleep(sleep_time)
    
    thread1 = threading.Thread(target=increment, args=("thread_01",0.5))
    thread2 = threading.Thread(target=increment, args=("thread_02",1))
    thread1.start()
    #thread1.join()
    thread2.start()
    #thread2.join()
    

提交回复
热议问题