Join functions in multi-threaded programming and join functions in multi-threaded programming

Source: Internet
Author: User

Join functions in multi-threaded programming and join functions in multi-threaded programming

# Coding: UTF-8 # test the multi-thread join Function import threading, time def doWaiting (): print 'start waiting1: '+ time. strftime ('% H: % M: % s') + "\ n" time. sleep (3) print 'Stop waiting1: '+ time. strftime ('% H: % M: % s') + "\ n" def doWaiting1 (): print 'start waiting2:' + time. strftime ('% H: % M: % s') + "\ n" time. sleep (8) print 'Stop waiting2: ', time. strftime ('% H: % M: % s') + "\ n" tsk = [] thread1 = threading. thread (target = doWaiting) thread1.start () tsk. append (thread1) thread2 = threading. thread (target = doWaiting1) thread2.start () tsk. append (thread2) print 'start join: '+ time. strftime ('% H: % M: % s') + "\ n" for tt in tsk: tt. join () print 'end join: '+ time. strftime ('% H: % M: % s') + "\ n"

 

The role of Join is well known, blocking processes until thread execution is complete

 

This applet uses two threads, thread1 and thread2. The actions executed by the thread are respectively doWaiting () and doWaiting1 (), the function body prints start + sleep for 3 seconds + prints end, and attaches the time to view the program execution process. The start () method is used to synchronize the execution of two threads. Then start to call the join () method of the two threads cyclically, and mark the start and end with the print function before and after this. We mainly observe for tt in tsk: tt. join ().

If join () does not contain parameters, run the following command:

start waiting1: 22:54:09start waiting2: 22:54:09start join: 22:54:09stop waiting1: 22:54:12stop waiting2:  22:54:17end join: 22:54:17Process finished with exit code 0

 

We can see that two threads run in parallel, process 1 ends after 3 s, process 2 ends after 8 s, and then return to the main process and execute the print "end join 」.

The following parameter is set to timeout 2 s, that is, tt. join (2). the execution is as follows:

start waiting1: 22:54:57start waiting2: 22:54:57start join: 22:54:57stop waiting1: 22:55:00end join: 22:55:01stop waiting2:  22:55:05Process finished with exit code 0

The two threads start concurrent execution, and then execute join (2) of thread 1. After thread 1 is executed for 2 seconds, the join (2) of thread 2 is executed ), after thread 2 is executed for 2 seconds, it does not matter (in this process, thread 1 is executed and the end information of thread 1 is printed). The main process is executed and "end join" is printed 」. Thread 2 is finished after 4s.

Summary:

1. The function of the join method is to block the main process (blocking, unable to execute statements after join) and focus on executing multithreading.

2. In the case of multi-threaded and multi-join, the join methods of each thread are executed in sequence, and the first one is finished before the execution is followed.

3. If no parameter exists, the join of the next thread starts to be executed until the end of the thread.

4. After the parameter is set, wait for the thread to ignore it for so long (and the thread has not ended ). No matter what it means, you can execute the following main process.

Finally, the program execution flow table with the parameter 2 is attached, and the self-drawn orz is better understood.

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.