Deep understanding of the JOIN () method in the Python THREADING module, pythonthreading

Source: Internet
Author: User
Tags exit in

Deep understanding of the JOIN () method in the Python THREADING module, pythonthreading

I have read two codes on oschina and have benefited a lot. I do not understand the join () method. Refer to the python documentation on the official website:
Join ([timeout]): Wait until the process ends. This will block the thread being called until the end of the thread that is calling the join () method. (This is what it means)

Haha, this is easy to understand.
Join method. If a thread or function needs to call another thread during execution and can be executed only after it is completed, the join method of the called thread can be used when this thread is called.
Copy codeThe Code is as follows:
#-*-Encoding: gb2312 -*-
Import string, threading, time
 
Def thread_main ():
Global count, mutex
# Obtain the thread name
Threadname = threading. currentThread (). getName ()
 
For x in xrange (0, int ()):
# Getting a lock
Mutex. acquire ()
Count = count + 1
# Release a lock
Mutex. release ()
Print threadname, x, count
Time. sleep (1)
 
Def main (num ):
Global count, mutex
Threads = []
 
Count = 1
# Create a lock
Mutex = threading. Lock ()
# Create a thread object first
For x in xrange (0, num ):
Threads. append (threading. Thread (target = thread_main, args = (10 ,)))
# Start all threads
For t in threads:
T. start ()
# Wait for all sub-threads to exit in the main thread
For t in threads:
T. join ()
 
If _ name _ = '_ main __':
Num = 4
# Create 4 threads
Main (4)
######################################## ###########################
#-*-Encoding: gb2312 -*-
Import threading
Import time
 
Class Test (threading. Thread ):
Def _ init _ (self, num ):
Threading. Thread. _ init _ (self)
Self. _ run_num = num
 
Def run (self ):
Global count, mutex
Threadname = threading. currentThread (). getName ()
 
For x in xrange (0, int (self. _ run_num )):
Mutex. acquire ()
Count = count + 1
Mutex. release ()
Print threadname, x, count
Time. sleep (1)
 
If _ name _ = '_ main __':
Global count, mutex
Threads = []
Num = 4
Count = 1
# Create a lock
Mutex = threading. Lock ()
# Create a thread object
For x in xrange (0, num ):
Threads. append (Test (10 ))
# Start a thread
For t in threads:
T. start ()
# Wait until the sub-thread ends
For t in threads:
T. join ()

In the program, the call to the join () method is clear. The main process calls the join () method of the subthread one by one. After all the four threads are executed, the main thread will execute the following code and exit here.
Another method that corresponds to the one found online:
3. Daemon

SetDaemon ()

This method is basically the opposite of join. When we are running the program, we will execute a main thread. If the main thread creates another sub-thread, and the main thread and sub-thread run separately, when the main thread completes and wants to exit, checks whether the Sub-thread is complete. If the sub-thread is not completed, the main thread will wait for the sub-thread to complete and then exit. But sometimes we need to exit with the main thread as long as the main thread is completed and the thread is not completed, the setDaemon method can be used.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.