Python--线程

# -*- coding: utf-8 -*-
# @Time    : 2018/1/2 0002 13:36
# @Author  : wangyafeng
# @Email   : 279949848@qq.com
 
###################threading###########################

import threading,time

 
def loop():
    print('thread %s is running...' % threading.current_thread().name)
    n = 0
    while n < 5:
        n = n + 1
        print('thread %s >>> %s' % (threading.current_thread().name, n))
        time.sleep(1)
    print('thread %s ended.' % threading.current_thread().name)
 
 
def loop2():
    n = 0
    while n < 5:
        n = n + 1
        print('thread %s >>> %s' % (threading.current_thread().name, n))
        time.sleep(1)
    print('thread %s ended.' % threading.current_thread().name)
 
 
threads = []
t1 = threading.Thread(target=loop, name='LoopThread')
threads.append(t1)
t2 = threading.Thread(target=loop, name='wyf')
threads.append(t2)
 
if __name__ == '__main__':
    for t in threads:
        t.setDaemon(True) #守护线程
        t.start()   #线程开始
    t.join()        #等待主进程:在子线程完成运行之前,这个子线程的父线程将一直被阻塞
    print("all over")

 #----------------------------运行结果------------------------------------------------------

 

D:\Python\Python36-32\python.exe D:/PycharmProjects/python实例/菜鸟升级/多线程.py
thread LoopThread is running...
thread LoopThread >>> 1
thread wyf is running...
thread wyf >>> 1
thread wyf >>> 2
thread LoopThread >>> 2
thread LoopThread >>> 3
thread wyf >>> 3
thread wyf >>> 4
thread LoopThread >>> 4
thread LoopThread >>> 5
thread wyf >>> 5
thread wyf ended.
all over
 
Process finished with exit code 0
 

###################Lock#################################

当把代码改为如下时:

def loop():
    print('thread %s is running...' % threading.current_thread().name)
 
    n = 0
    try:
        lock.acquire()
        while n < 5:
            n = n + 1
            print('thread %s >>> %s' % (threading.current_thread().name, n))
            time.sleep(1)
        print('thread %s ended.' % threading.current_thread().name)
    finally:
        lock.release()
 
 
def loop2():
    n = 0
    try:
        lock.acquire()
        while n < 5:
            n = n + 1
            print('thread %s >>> %s' % (threading.current_thread().name, n))
            time.sleep(1)
        print('thread %s ended.' % threading.current_thread().name)
    finally:
        lock.release()

 #----------------------------------------------------------------------执行结果如下

 

 

 

D:\Python\Python36-32\python.exe D:/PycharmProjects/python实例/菜鸟升级/多线程.py

 

thread LoopThread is running...
thread LoopThread >>> 1
thread wyf is running...
thread LoopThread >>> 2
thread LoopThread >>> 3
thread LoopThread >>> 4
thread LoopThread >>> 5
thread LoopThread ended.
thread wyf >>> 1
thread wyf >>> 2
thread wyf >>> 3
thread wyf >>> 4
thread wyf >>> 5
thread wyf ended.
all over

 #so,这还是我们想要的结果.....................为了好看,有些代码不太规整,将就着看吧.................................................................

 

 
import threading,time

 

 
# 创建全局ThreadLocal对象:
local_school = threading.local()
 
 
def process_student():
    # 获取当前线程关联的student:
    std = local_school.student
    print('Hello, %s (in %s)' % (std, threading.current_thread().name))
 
 
def process_thread(name):
    # 绑定ThreadLocal的student:
    local_school.student = name
    for i in range(2):
        print("i=",i)
        time.sleep(1)
        process_student()
 
 
t1 = threading.Thread(target= process_thread, args=('Alice',), name='Thread-A')
t2 = threading.Thread(target= process_thread, args=('Bob',), name='Thread-B')
t3 = threading.Thread(target= process_thread, args=('owen',), name='Thread-C')
 
t2.start();  t2.join();
t1.start();  t1.join();
t3.start();  t3.join();

 

#__________________运行结果_________________________________________

 

 
D:\Python\Python36-32\python.exe D:/PycharmProjects/python实例/菜鸟升级/多线程.py

 

i= 0
Hello, Bob (in Thread-B)
i= 1
Hello, Bob (in Thread-B)
i= 0
Hello, Alice (in Thread-A)
i= 1
Hello, Alice (in Thread-A)
i= 0
Hello, owen (in Thread-C)
i= 1
Hello, owen (in Thread-C)
 
Process finished with exit code 0

 

 #????????????????????????????未完待续........

posted @ 2018-01-02 14:18  王亚锋  阅读(203)  评论(0编辑  收藏  举报