1 Star 0 Fork 0

unite小伟 / pythontest

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
Thread_py.py 1.12 KB
一键复制 编辑 原始数据 按行查看 历史
unite小伟 提交于 2018-02-26 14:02 . test python
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
"""
@author:shengwei
@file:Thread_py.py
@time:2018/2/23 002317:00
"""
import time, threading
# 由于任何进程默认就会启动一个线程,我们把该线程称为主线程,
# 主线程又可以启动新的线程,Python的threading模块有个current_thread()函数,
# 它永远返回当前线程的实例。主线程实例的名字叫MainThread,
# 子线程的名字在创建时指定,我们用LoopThread命名子线程。
# 名字仅仅在打印时用来显示,完全没有其他意义,如果不起名字Python就自动给线程命名为Thread-1,Thread-2
# 新线程执行的代码:
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)
print('thread %s is running...' % threading.current_thread().name)
t = threading.Thread(target=loop, name='LoopThread')
t.start()
t.join()
print('thread %s ended.' % threading.current_thread().name)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/unite/pythontest.git
git@gitee.com:unite/pythontest.git
unite
pythontest
pythontest
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891