1. 一般多线程
- import threading
- def func(arg):
- # 获取当前执行该函数的线程的对象
- t = threading.current_thread()
- # 根据当前线程对象获取当前线程名称
- name = t.getName()
- print(name, arg)
- for i in range(5):
- t1 = threading.Thread(target=func, args=(i,))
- t1.setName('线程:%s-->' % i)
- t1.start()
- print('end')
2. 面向对象版多线程
- class MyThread(threading.Thread):
- def run(self):
- # 获取当前执行该函数的线程的对象
- t = threading.current_thread()
- # 根据当前线程对象获取当前线程名称
- name = t.getName()
- print(name, self._args, self._kwargs)
- for i in range(5):
- t = MyThread(args=(i,))
- t.start()
- print('end')
来源: http://www.bubuko.com/infodetail-2944226.html