Write a program that launches 1,000 threads. Each thread adds 1 to an integer variable sum which initialized as 0. Implement the addition program with and without synchronization to observe different results. (1)Define the main class sum to create a fixed thread pool with 1,000 threads; (2)Define the task class SumTaskWithoutSyn which implements addition without any synchronization; (3)Define the task class SumTaskWithSynMet to implement synchronized method; (4)Define the task class SumTaskWithSynStat to implement synchronized statement block; (5)Define the task class SumTaskWithLock to implement synchronization with lock.
时间: 2024-03-20 20:43:20 浏览: 84
thread pool
Sure, I can help you with that.
Here is the code for the main class `Sum`:
```java
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Sum {
public static void main(String[] args) {
int threadCount = 1000;
int sum = 0;
ExecutorService executor = Executors.newFixedThreadPool(threadCount);
// Create 1000 threads and submit SumTaskWithoutSyn tasks
for (int i = 0; i < threadCount; i++) {
executor.execute(new SumTaskWithoutSyn(sum));
}
// Shutdown the executor
executor.shutdown();
// Wait until all threads are finished
while (!executor.isTerminated()) {
}
System.out.println("Sum without synchronization: " + sum);
// Reset the sum variable
sum = 0;
// Create a new executor
executor = Executors.newFixedThreadPool(threadCount);
// Create 1000 threads and submit SumTaskWithSynMet tasks
for (int i = 0; i < threadCount; i++) {
executor.execute(new SumTaskWithSynMet(sum));
}
// Shutdown the executor
executor.shutdown();
// Wait until all threads are finished
while (!executor.isTerminated()) {
}
System.out.println("Sum with synchronized method: " + sum);
// Reset the sum variable
sum = 0;
// Create a new executor
executor = Executors.newFixedThreadPool(threadCount);
// Create 1000 threads and submit SumTaskWithSynStat tasks
for (int i = 0; i < threadCount; i++) {
executor.execute(new SumTaskWithSynStat(sum));
}
// Shutdown the executor
executor.shutdown();
// Wait until all threads are finished
while (!executor.isTerminated()) {
}
System.out.println("Sum with synchronized statement block: " + sum);
// Reset the sum variable
sum = 0;
// Create a new executor
executor = Executors.newFixedThreadPool(threadCount);
// Create 1000 threads and submit SumTaskWithLock tasks
for (int i = 0; i < threadCount; i++) {
executor.execute(new SumTaskWithLock(sum));
}
// Shutdown the executor
executor.shutdown();
// Wait until all threads are finished
while (!executor.isTerminated()) {
}
System.out.println("Sum with lock synchronization: " + sum);
}
}
```
The `Sum` class creates a fixed thread pool with 1,000 threads and submits tasks to the executor. It then waits for all threads to finish and prints the final sum.
Here is the code for the `SumTaskWithoutSyn` class:
```java
public class SumTaskWithoutSyn implements Runnable {
private int sum;
public SumTaskWithoutSyn(int sum) {
this.sum = sum;
}
@Override
public void run() {
sum += 1;
}
}
```
The `SumTaskWithoutSyn` class implements the addition without any synchronization.
Here is the code for the `SumTaskWithSynMet` class:
```java
public class SumTaskWithSynMet implements Runnable {
private int sum;
public SumTaskWithSynMet(int sum) {
this.sum = sum;
}
@Override
public void run() {
add();
}
private synchronized void add() {
sum += 1;
}
}
```
The `SumTaskWithSynMet` class implements the addition with synchronized method.
Here is the code for the `SumTaskWithSynStat` class:
```java
public class SumTaskWithSynStat implements Runnable {
private int sum;
public SumTaskWithSynStat(int sum) {
this.sum = sum;
}
@Override
public void run() {
synchronized (this) {
sum += 1;
}
}
}
```
The `SumTaskWithSynStat` class implements the addition with synchronized statement block.
Here is the code for the `SumTaskWithLock` class:
```java
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class SumTaskWithLock implements Runnable {
private int sum;
private Lock lock;
public SumTaskWithLock(int sum) {
this.sum = sum;
lock = new ReentrantLock();
}
@Override
public void run() {
lock.lock();
try {
sum += 1;
} finally {
lock.unlock();
}
}
}
```
The `SumTaskWithLock` class implements the addition with lock synchronization.
I hope this helps!
阅读全文