day10-04_ Multithreading Common Attribute method

Source: Internet
Author: User

First, need to understand the method

Methods for thread instance objects

# isAlive (): Determine if this thread is alive
# getName (): Get thread Name
# SetName (): Set thread name
#enumerate (): View Active Process objects
#activeCount (): View the number of active threads

From threading Import Thread,activecount,enumerate,current_threadimport timedef Task ():    print ('%s is running '% Current_thread (). GetName ())    Time.sleep (2) if __name__ = = ' __main__ ':    t=thread (Target=task,)    T.start ()    t.join ()    print (T.is_alive ()) #判断这个线程是否是存活的    print (T.getname ()) #获取线程名    print (Enumerate ()) # View Active Process Object    print (' master ')    print (Activecount ()) #查看活跃的线程数
Second, the use of current_thread (* * * * *)

You can use the Current_thread method to call some other methods, the test is often used to
To see what the open thread names are.

From threading import Thread, Activecount, enumerate, current_threadfrom multiprocessing import processimport timedef tas K ():    print ('%s is running '% current_thread (). GetName ()) #可以用current_thread方法去调用一些其它方法, testing is often used to    time.sleep ( 2) If __name__ = = ' __main__ ':    p = Process (target=task)    p.start ()    print (Current_thread ())
Third, based on the current_thread to verify that a process is open, the default is a main thread

Emphasis: The main thread represents the execution process of the process from the execution level.

From threading import Thread, Activecount, enumerate, current_threadfrom multiprocessing import processimport timedef tas K ():    print ('%s is running '% current_thread (). GetName ())    Time.sleep (2) if __name__ = = ' __main__ ':    t1 = Thread (target=task)    t2 = thread (target=task)    t3 = Thread (target=task)    T1.start ()    T2.start ()    T3.start ()    print (Current_thread (). GetName ()) #在一个进程里边, the main thread is only one, the others are child threads

  

  

 

day10-04_ Multithreading Common Attribute method

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.