编写程序,创建并运行3个线程:第一个线程打印100次字母a,第二个线程打印100次字母b,第三个线程打印1~100的整数。
时间: 2023-09-19 08:10:49 浏览: 62
以下是Python实现的示例代码:
```python
import threading
def print_a():
for i in range(100):
print('a', end='')
def print_b():
for i in range(100):
print('b', end='')
def print_num():
for i in range(1, 101):
print(i, end=' ')
if __name__ == '__main__':
thread_a = threading.Thread(target=print_a)
thread_b = threading.Thread(target=print_b)
thread_num = threading.Thread(target=print_num)
thread_a.start()
thread_b.start()
thread_num.start()
thread_a.join()
thread_b.join()
thread_num.join()
print('All threads completed.')
```
首先定义了三个函数分别用于打印字母a,字母b和数字1~100。然后使用`threading.Thread()`方法创建三个线程,并分别将上述三个函数作为参数传入。接着使用`start()`方法启动线程,使用`join()`方法等待所有线程执行完毕。最后输出提示信息表示所有线程均已完成。
相关推荐
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)