线程与进程

1进程与线程

进程:

狭义定义:一段程序的执行顺序

广义定义:进程就是具有一定独立功能的程序关于某个数据集合上的一次运动活动

进程是进行资源分配和调度的独立单位

线程是进程的一个实体,是cpu调度和分派的基本单位 他是比进程还小的能独立运行的基本单位

线程基本上不自己拥有系统资源只拥有一点在运行中必不可少的资源(程序计数器  寄存器和栈)但是他可以同属于一个进程的其他线程

共享进程的其他资源

进程和线程的主要区别是他们是不同操作系统管理的方式,一个进程崩溃后 在保护模式之后不会对其他进程产生影响,而线程

只是一个进程之后的不同执行路径.线程有自己的堆栈和自己的不同操作空间 但是线程之间没有单独的地址空间

进程负责各个线程的运行 而任务的执行顺序需要执行的时间  可以使用分线程

当界面有大量的任务需要更新的时候  需要放入分线程

import threading
print('位置1',threading.current_thread().name)
print('位置2',threading.current_thread().name)
print('位置3',threading.current_thread().name)
print('位置4',threading.current_thread().name)
sub_thread = threading.Thread(target=myThread,name='newThread')
sub_thread.start()
sub_thread.join()
rint('outside1',threading.current_thread().name)
print('outside2',threading.current_thread().name)

threading.Thread 开辟一个新的线程  tarent 目标  name是分线程的名字

确保任务的执行顺序  自己的任务先完成再执行其他的线程

当程序运行的时候会先在主线程中执行(因为在程序刚开始的时候只有主线程,没有分线程)

然后会根据情况进入了分线程主线程和分线程的任务是交叉进行的  因为两个线程是两条路

自己线程的执行情况不会影响其他的线程

分线程的代码结束后会回归主线程

2 线程锁

线程锁:当有一个数据有多个线程可以对其修改的时候  任何一个线程的改变都会对其他线程造成影响,如果我们

想要某个程序在使用完之前,其他线程不能对其进行修改么就需要对其增加一个线程锁

import threding 
import time'
import random

count = 0
def get_money(money):
    global count 
    count +=money
    count +=money
    count -= money
lock = therading.Lock()
def lock_thread(money):
    lock.acquire()
    time.sleep(random.randomint(1,3))
    print('当前线程为',threading.current_thread().name))
    get_money(money)
    time.sleep(random.randint(1, 3))
    print('当前线程为', threading.current_thread().name)
    lock.release()
thread1 = threading.Thread(target=lock_thread ,name='thread1',args=(1000,))
thread2 = threading.Thread(target=lock_thread ,name='thread2',args=(2000,))
thread1.start()    
thread2.start()
thread1.join()
thread2.join()

thread1.join()  join 注重的是整体  线程1 没有执行完之后线程2 不能执行

lock注重的是局部  某一个人变量没有用完,其他的线程不能够执行

3 线程卖票系统

import threading
import time
import random
class buyer(object):
    def __init__(self,name = '',number = 1):
        self.name = name
        self.number = number
lock = threading.Lock()
class computer(object):
    def __init__(self,count = 0):
        self.count = count
    def query_with_buyer_info(self,other):
        print('正在为{}查询剩余票数'.format(other.name))
        time.sleep(random.randint(1,3))
        lock.acquire()
        if self.count > other.number:
            print('有票,{}可以买'.format(other.name))
            time.sleep(random.randint(1,3))
            self.count -= other.number
        else:
            print('没票')
        lock.release()

hanmeimei = buyer('韩梅梅',2)
houzi = buyer('猴子',5)
con = computer(6)

thread1 = threading.Thread(target=con.query_with_buyer_info,name='thread1',args=(hanmeimei,))
thread2 = threading.Thread(target=con.query_with_buyer_info,name='thread2',args=(houzi,))
thread1.start()
thread2.start()

4 线程队列

import queue

引入队列包

 

import queue
# 队列:fifo  first  in first out
q = queue.Queue()

for i in range(5):
    q.put(i)
    # 将内容放置线程队列中
while  not q.empty():
    print(q.get())

    # last in first out
p = queue.LifoQueue()
for i in range(5):
    p.put(i)
while not p.empty():
    print(p.get())

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值