创建两个线程,一个打印1-1000的奇数,一个打印1-1000的偶数
时间: 2024-02-25 16:55:36 浏览: 55
好的,我来写一个示例代码:
```python
import threading
# 打印奇数的线程函数
def print_odd():
for i in range(1, 1001, 2):
print(i)
# 打印偶数的线程函数
def print_even():
for i in range(2, 1001, 2):
print(i)
# 创建两个线程并启动
t1 = threading.Thread(target=print_odd)
t2 = threading.Thread(target=print_even)
t1.start()
t2.start()
```
这个代码创建了两个线程,一个打印奇数,一个打印偶数,它们同时运行并打印出结果。注意,这里用到了Python内置的`threading`模块来实现多线程。
相关问题
java语言实现:创建两个线程,线程A打印1-10之间的偶数。线程B打印1-10之间的奇数。两个操作交替执行
代码如下:
```
public class PrintNumber {
private static Object lock = new Object(); // 定义一个锁对象
private static int count = 1; // 初始值为1
public static void main(String[] args) {
Thread thread1 = new Thread(() -> {
while (count <= 10) {
synchronized (lock) { // 加锁
if (count % 2 == 0) {
System.out.println(Thread.currentThread().getName() + ": " + count);
count++;
}
}
}
}, "Thread-A");
Thread thread2 = new Thread(() -> {
while (count <= 10) {
synchronized (lock) { // 加锁
if (count % 2 != 0) {
System.out.println(Thread.currentThread().getName() + ": " + count);
count++;
}
}
}
}, "Thread-B");
thread1.start(); // 启动线程1
thread2.start(); // 启动线程2
}
}
```
在该代码中,我们首先定义了一个锁对象lock和一个计数器count,然后创建了两个线程thread1和thread2。线程thread1负责打印偶数,线程thread2负责打印奇数。
在每个线程中,我们使用while循环保证打印的数字不超过10。然后使用synchronized关键字对锁对象进行加锁,确保两个线程交替执行。在加锁的代码块中,我们对计数器进行判断,打印偶数或奇数,并将计数器加1。
最后,我们启动两个线程。由于两个线程使用了同一个锁对象,因此它们会交替执行,打印出1-10之间的奇数和偶数。
创建两个线程 一个输出1-100的奇数,一个输出1-100的偶数 交替输出
可以使用线程和锁来实现交替输出奇偶数的功能。下面是一个示例代码:
```python
import threading
# 创建一个锁
lock = threading.Lock()
# 奇数线程
def odd_thread():
for i in range(1, 101, 2):
lock.acquire() # 获取锁
print(i)
lock.release() # 释放锁
# 偶数线程
def even_thread():
for i in range(2, 101, 2):
lock.acquire() # 获取锁
print(i)
lock.release() # 释放锁
# 创建并启动线程
t1 = threading.Thread(target=odd_thread)
t2 = threading.Thread(target=even_thread)
t1.start()
t2.start()
t1.join()
t2.join()
```
这个代码中,我们首先创建了一个锁对象 `lock`,然后定义了奇数线程函数 `odd_thread` 和偶数线程函数 `even_thread`。在每个线程的循环中,首先通过 `lock.acquire()` 获取锁,然后输出对应的数字,最后通过 `lock.release()` 释放锁。
最后,我们创建并启动两个线程 `t1` 和 `t2`,并使用 `t1.join()` 和 `t2.join()` 来等待两个线程的结束。这样,两个线程就会交替输出奇偶数。
相关推荐
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)