1.线程的三种创建方式

1.线程的创建方式


#第一种多线程方式 借助模块threading
"""
import win32api
import threading


def show(i):
    win32api.MessageBox(0,"你的账户很危险"+str(i),"来自支付宝的问候",6)

#target=show 线程函数,args=()参数 类的构造实现多线程,不考虑通信冲突
threading.Thread(target=show,args=(1,)).start()
threading.Thread(target=show,args=(1,)).start()#target关键字参数传递函数名,args关键字参数参数传递函数的参数
threading.Thread(target=show,args=(1,)).start()
threading.Thread(target=show,args=(1,)).start()
"""

#第二种多线程简单创建方式
"""
import win32api#引用系统函数,不考虑冲突,通信
import _thread #多线程

def show(i):
    win32api.MessageBox(0,"你的账户很危险","来自支付宝的问候"+str(i))


for i in range(5):#创建五个线程
    _thread.start_new_thread(show,(i,))#(i)元组,用于传递参数,函数实现多线程

show(99)#主线程
"""

#第三种用类表现多线程
import threading
import time
import win32api

class Mythread(threading.Thread):
    def __init__(self,num):
        threading.Thread.__init__(self)
        self.num=num

    def run(self):
        win32api.MessageBox(0,"你的账户有危险","来自支付宝的问候",16)


for i in range(5):
    t=Mythread(i)
    t.start()#线程的顺序结构
    t.join()




D:\PycharmProjects\untitled11\venv\Scripts\python.exe D:/PycharmProjects/untitled11/多线程/线程中阶/1.创建多线程的方式.py

Process finished with exit code 0

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值