From 16fdd49a8c1367a917671196119d260a975146c9 Mon Sep 17 00:00:00 2001 From: Hammy Date: Sun, 27 Dec 2020 15:12:13 +0000 Subject: [PATCH] Solve Concurrency Issues (Synchronized Block Statements) --- .../with/synchronised/blocks/BankAccount.java | 17 ++++++++++++++ .../issues/with/synchronised/blocks/Main.java | 23 +++++++++++++++++++ .../with/synchronised/blocks/Worker.java | 19 +++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/src/multi/threading/solving/concurrency/issues/with/synchronised/blocks/BankAccount.java create mode 100644 Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/src/multi/threading/solving/concurrency/issues/with/synchronised/blocks/Main.java create mode 100644 Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/src/multi/threading/solving/concurrency/issues/with/synchronised/blocks/Worker.java diff --git a/Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/src/multi/threading/solving/concurrency/issues/with/synchronised/blocks/BankAccount.java b/Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/src/multi/threading/solving/concurrency/issues/with/synchronised/blocks/BankAccount.java new file mode 100644 index 0000000..1fc2050 --- /dev/null +++ b/Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/src/multi/threading/solving/concurrency/issues/with/synchronised/blocks/BankAccount.java @@ -0,0 +1,17 @@ +package multi.threading.solving.concurrency.issues.with.synchronised.blocks; + +public class BankAccount { + private int balance; + + public BankAccount(int balance) { + this.balance = balance; + } + + public int getBalance() { + return balance; + } + + public void deposit(int amount) { + balance += amount; + } +} diff --git a/Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/src/multi/threading/solving/concurrency/issues/with/synchronised/blocks/Main.java b/Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/src/multi/threading/solving/concurrency/issues/with/synchronised/blocks/Main.java new file mode 100644 index 0000000..6e8494b --- /dev/null +++ b/Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/src/multi/threading/solving/concurrency/issues/with/synchronised/blocks/Main.java @@ -0,0 +1,23 @@ +package multi.threading.solving.concurrency.issues.with.synchronised.blocks; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; + +public class Main { + public static void main(String[] args) throws InterruptedException { + + ExecutorService executorService = Executors.newFixedThreadPool(5); + BankAccount account = new BankAccount(100); + + for (int i = 0; i <= 10; i++) { + Worker worker = new Worker(account); + executorService.submit(worker); + } + + executorService.shutdown(); + if (executorService.awaitTermination(60, TimeUnit.SECONDS)) { + System.out.println("Operations Executed Successfully!"); + } + } +} diff --git a/Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/src/multi/threading/solving/concurrency/issues/with/synchronised/blocks/Worker.java b/Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/src/multi/threading/solving/concurrency/issues/with/synchronised/blocks/Worker.java new file mode 100644 index 0000000..a817e24 --- /dev/null +++ b/Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/src/multi/threading/solving/concurrency/issues/with/synchronised/blocks/Worker.java @@ -0,0 +1,19 @@ +package multi.threading.solving.concurrency.issues.with.synchronised.blocks; + +public class Worker implements Runnable { + private final BankAccount account; + + public Worker(BankAccount account) { + this.account = account; + } + + @Override + public void run() { + for (int i = 0; i < 10; i++) { + synchronized(account) { + account.deposit(10); + } + System.out.println("End Balance: " + account.getBalance()); + } + } +}