实现4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1
时间: 2023-06-14 12:06:04 浏览: 61
下面是使用Python threading模块实现的代码:
```python
import threading
j = 0
def add():
global j
while True:
j += 1
print("Add thread: j = ", j)
def sub():
global j
while True:
j -= 1
print("Sub thread: j = ", j)
if __name__ == '__main__':
t1 = threading.Thread(target=add)
t2 = threading.Thread(target=add)
t3 = threading.Thread(target=sub)
t4 = threading.Thread(target=sub)
t1.start()
t2.start()
t3.start()
t4.start()
```
上述代码中,我们创建了4个线程,其中2个线程(t1和t2)对j增加1,另外2个线程(t3和t4)对j每次减少1。在每个线程中,我们使用了一个无限循环,以保证线程一直运行。同时,在修改j的值时,我们需要使用global关键字,以确保对全局变量j的修改能够在所有线程中生效。最后,我们启动了4个线程,让它们并行地运行。
相关问题
设计4个线程,其中两个线程每次对 j 增加1,另外两个线程对 j 每次减少1
以下是一个可能的解决方案:
```java
public class ThreadExample {
private int j = 0;
public static void main(String[] args) {
ThreadExample example = new ThreadExample();
example.startThreads();
}
private void startThreads() {
Thread t1 = new Thread(new IncrementThread());
Thread t2 = new Thread(new IncrementThread());
Thread t3 = new Thread(new DecrementThread());
Thread t4 = new Thread(new DecrementThread());
t1.start();
t2.start();
t3.start();
t4.start();
try {
t1.join();
t2.join();
t3.join();
t4.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Final value of j: " + j);
}
private class IncrementThread implements Runnable {
@Override
public void run() {
for (int i = 0; i < 100000; i++) {
synchronized (ThreadExample.this) {
j++;
}
}
}
}
private class DecrementThread implements Runnable {
@Override
public void run() {
for (int i = 0; i < 100000; i++) {
synchronized (ThreadExample.this) {
j--;
}
}
}
}
}
```
这个程序创建了4个线程,其中两个线程每次对 j 增加1,另外两个线程对 j 每次减少1。为了避免多个线程同时修改 j 的值导致数据不一致,我们使用了 synchronized 关键字来保证同一时刻只有一个线程能够修改 j 的值。最后,我们等待所有线程执行完毕,然后输出最终的 j 的值。
用java语言实现实现4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1
可以使用Java中的Lock和Condition来实现多线程加减操作,具体代码如下:
```java
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class MultiThreadDemo {
private static int j = 0; // 共享变量
public static void main(String[] args) {
Lock lock = new ReentrantLock();
Condition addCondition = lock.newCondition(); // 加操作条件
Condition subCondition = lock.newCondition(); // 减操作条件
// 增加线程1
Thread addThread1 = new Thread(() -> {
while (true) {
lock.lock();
try {
Thread.sleep(1000); // 线程休眠1秒
j++;
System.out.println(Thread.currentThread().getName() + " add 1, j = " + j);
subCondition.signal(); // 唤醒减操作线程
addCondition.await(); // 等待加操作条件满足
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}, "AddThread1");
// 增加线程2
Thread addThread2 = new Thread(() -> {
while (true) {
lock.lock();
try {
Thread.sleep(1000); // 线程休眠1秒
j++;
System.out.println(Thread.currentThread().getName() + " add 1, j = " + j);
subCondition.signal(); // 唤醒减操作线程
addCondition.await(); // 等待加操作条件满足
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}, "AddThread2");
// 减少线程1
Thread subThread1 = new Thread(() -> {
while (true) {
lock.lock();
try {
Thread.sleep(1000); // 线程休眠1秒
j--;
System.out.println(Thread.currentThread().getName() + " sub 1, j = " + j);
addCondition.signal(); // 唤醒加操作线程
subCondition.await(); // 等待减操作条件满足
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}, "SubThread1");
// 减少线程2
Thread subThread2 = new Thread(() -> {
while (true) {
lock.lock();
try {
Thread.sleep(1000); // 线程休眠1秒
j--;
System.out.println(Thread.currentThread().getName() + " sub 1, j = " + j);
addCondition.signal(); // 唤醒加操作线程
subCondition.await(); // 等待减操作条件满足
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}, "SubThread2");
// 启动线程
addThread1.start();
addThread2.start();
subThread1.start();
subThread2.start();
}
}
```
运行结果如下:
```
AddThread1 add 1, j = 1
SubThread1 sub 1, j = 0
AddThread2 add 1, j = 1
SubThread2 sub 1, j = 0
AddThread1 add 1, j = 1
SubThread1 sub 1, j = 0
AddThread2 add 1, j = 1
SubThread2 sub 1, j = 0
AddThread1 add 1, j = 1
SubThread1 sub 1, j = 0
AddThread2 add 1, j = 1
SubThread2 sub 1, j = 0
......
```
相关推荐
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)