Note || Pyhton3 advanced as much as a thread principle

# Multithreading

# A process equivalent to one or more threads

# When no multi-threaded programming, a process is a main thread

# But multithreaded programming, a process contains multiple threads, including the main thread

# Using threads can achieve concurrent programs

# Python3 in a lot of thread support is threading module

# import threading

# In python3, the threads can be created in two ways

'' '
In two ways:

     ① use threading.Thread run function (call the constructor of the Thread class to create threads) directly in the thread.

         target parameters: pass a function object (function name);

         args parameter: pass a tuple, as the objective function parameter;

         Proceed as follows:

             1 - Call the Thread class constructor to create a thread object, when you create an object, target parameters specified function as a thread of execution

             2 - calling thread object star () method to start the thread

     ② inheritance Thread class to create a thread class.

         Proceed as follows:

             1-- subclass definition of the Thread class, the class and override run () method, the method body run () method represents the thread needs to complete the task, so the run () method is called thread of execution

             2 - Create an instance of a subclass of Thread, the thread object is created

             3 - calling thread object star () method to start a thread

     The first approach is recommended to create a thread, this programming simple way, the thread packaged directly target function, the logical structure having a sharper
'' '

# How to get the name of the main thread

# threading.current_thread().getName()

# Thread scheduling

# Start method to start a thread

# Join method waiting thread

import threading

import time


def thread_run(name):

time.sleep(2)

print("%s first thread!!!" % name)


t1 = threading.Thread(target=thread_run, args=('peiyanan', ))

t2 = threading.Thread(target=thread_run, args=('tanyongling', ))

t1.start()

t2.start()

t1.setName ( 'child thread 1') # Set the name of the thread name

print (t1.name) # print thread name

print (t1.getName ()) # Get the Thread name

print (t1.is_alive ()) # isalive () method for determining whether a running thread is not running is displayed False

T1.join # ()
# t2.join ()  
# Print ( 'the main thread running !!')


# -------------------------------------------------------


# How elegant use multithreading

# Number can be used for loop to control the creation thread

# Menu = [ 'mathematics', 'English', 'Language', 'geography', 'History']

# for m in menu:

#         t = threading.Thread(target=thread_run, args=(m, 2))

#         t.start()


# Thread scheduling

# Operating system will not allow one thread has been occupied by the CPU

# Thread life cycle
'' '
four states:
① new status means that the thread has been initialized, but did not start
② running state refers to the normal execution thread
③ blocking state means the thread is being executed, but did not get the CPU to perform time, while waiting at the external CPU threads executing state
④ death state refers to the end of the thread execution
'' '

# Using threads need to be aware of the point
'' '
thread unique resources
function local variable called, the thread will have a copy of itself
does not conflict
thread will sometimes cause trouble on development: such as access to shared data, such as Global variable
'' '

Guess you like

Origin www.cnblogs.com/peipei-Study/p/12095400.html