There are five states of thread
New, ready, running, blocked, dead.
There are three cases of blocking:
Synchronous blocking refers to a state in a competitive lock, which enters this state when a thread requests a lock, and resumes to a running state once the lock is successfully acquired;
Waiting for blocking is the status of waiting for other threads to notify, after the thread obtains the conditional lock, the call "Wait" will enter this state, once the other thread notifies, the thread will enter the synchronization blocking state, the competition condition lock again;
Other blocking means blocking when calling Time.sleep (), Anotherthread.join (), or waiting for IO, which does not release the acquired lock.
Python provides two ways to use threads, one is functional and one is class-wrapped.
* Thread
* Threading
1. Thread:
>>> Import thread>>> dir (thread) [' LockType ', ' __doc__ ', ' __name__ ', ' __package__ ', ' _count ', ' _ Local ', ' allocate ', ' allocate_lock ', ' Error ', ' exit ', ' exit_thread ', ' get_ident ', ' interrupt_main ', ' stack_size ', ' Start_new ', ' Start_new_thread ']
Thread.start_new_thread (function, args [, Kwargs])
Call the Start_new_thread () function in the thread module to generate a new thread.
2, Threading:
>>> Import threading>>> dir (threading) [' Boundedsemaphore ', ' Condition ', ' Event ', ' Lock ', ' rlock ', ' Semaphore ', ' Thread ', ' threaderror ', ' Timer ', ' _boundedsemaphore ', ' _condition ', ' _dummythread ', ' _event ', ' _ Mainthread ', ' _rlock ', ' _semaphore ', ' _timer ', ' _verbose ', ' _verbose ', ' __all__ ', ' __builtins__ ', ' __doc__ ', ' __file__ ' , ' __name__ ', ' __package__ ', ' _active ', ' _active_limbo_lock ', ' _after_fork ', ' _allocate_lock ', ' _counter ', ' _ Enumerate ', ' _format_exc ', ' _get_ident ', ' _limbo ', ' _newname ', ' _picksomenondaemonthread ', ' _profile_hook ', ' _ Shutdown ', ' _sleep ', ' _start_new_thread ', ' _sys ', ' _test ', ' _time ', ' _trace_hook ', ' activecount ', ' active_count ', ' CurrentThread ', ' current_thread ', ' deque ', ' Enumerate ', ' local ', ' setprofile ', ' settrace ', ' stack_size ', ' warnings '
>>> Dir (threading. Thread) [' _thread__bootstrap ', ' _thread__bootstrap_inner ', ' _thread__delete ', ' _thread__exc_clear ', ' _Thread__exc_ Info ', ' _thread__initialized ', ' _thread__stop ', ' __class__ ', ' __delattr__ ', ' __dict__ ', ' __doc__ ', ' __format__ ', ' __ getattribute__ ', ' __hash__ ', ' __init__ ', ' __module__ ', ' __new__ ', ' __reduce__ ', ' __reduce_ex__ ', ' __repr__ ', ' __ Setattr__ ', ' __sizeof__ ', ' __str__ ', ' __subclasshook__ ', ' __weakref__ ', ' _block ', ' _note ', ' _reset_internal_locks ', ' _ Set_daemon ', ' _set_ident ', ' daemon ', ' getName ', ' ident ', ' isAlive ', ' Isdaemon ', ' is_alive ', ' join ', ' name ', ' Run ', ' SETDA Emon ', ' setName ', ' Start ']
Common methods provided by the threading module:
Threading.currentthread (): Returns the current thread variable.
Threading.enumerate (): Returns a list that contains the running thread. Running refers to threads that do not include pre-and post-termination threads until after the thread has started and ends.
Threading.activecount (): Returns the number of running threads with the same result as Len (Threading.enumerate ()).
Inherit threading. The thread method, overriding the Run method.
Threading. Initialization function prototypes for the thread class: Def __init__ (self, group=none, Target=none, Name=none, args= (), kwargs={})
The parameter group is reserved for future expansion;
The parameter target is a callable object (also known as an active [activity]) that executes after the thread is started;
The parameter name is the name of the thread. The default value is "Thread-n" and N is a number.
The parameter args and Kwargs each represent the argument list and the keyword argument when the target is called.
Join () method, the thread that calls the method waits until the thread object finishes, and then resumes running.
Calling Thread.Join will cause the thread to block until the calling thread finishes running or times out. The parameter timeout is a numeric type that indicates the time-out period, and if the parameter is not supplied, the thread will block until the thread ends.
Threading. Lock object: Mutex, with Acquire () and release () methods
Rlock is allowed to be acquire multiple times in the same thread. But lock does not allow this situation. Note: If you use Rlock, then acquire and release must appear in pairs, that is, call n times acquire, must call N times release to really release the occupied locks.
Threading. Condition object: Condition variable, when the object is created, it contains a lock object (because condition variable is always used with the mutex). You can call the acquire () and release () methods on the condition object to control the potential lock object.
Condition.wait ([timeout]): The Wait method releases the internal footprint, and the thread is suspended until the notification is woken up or timed out (if the timeout parameter is provided). The program will continue to execute when the thread is awakened and re-occupied.
Condition.notify (): Wakes up a suspended thread (if there is a pending thread). Note: the Notify () method does not release the occupied locks.
Condition.notifyall () wakes up all pending threads (if there are pending threads). Note: These methods do not release the locks that are occupied.