373 Task class

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
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!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黄健华Yeah

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值