Commit be56d377 authored by Trisha Gee's avatar Trisha Gee
Browse files

[Ch15] Book example for race condition/lost update

parent 0994ba56
Loading
Loading
Loading
Loading
+3 −7
Changes for src/main/java/ch15/LostUpdate.java: 3 added lines, 7 removed lines.
Original line number Diff line number Diff line
@@ -4,12 +4,12 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class TestSyncTest {
public class LostUpdate {
  public static void main(String[] args) throws InterruptedException {
    ExecutorService pool = Executors.newFixedThreadPool(6);
    ExecutorService pool = Executors.newFixedThreadPool(30);
    Balance balance = new Balance();
    for (int i = 0; i < 1000; i++) {
      pool.execute(() -> incrementBalance(balance));
      pool.execute(() -> balance.increment());
    }
    pool.shutdown();
    // make sure the pool has finished running all the updates before printing the output
@@ -17,10 +17,6 @@ public class TestSyncTest {
      System.out.println("balance = " + balance.balance);
    }
  }

  private static void incrementBalance(Balance balance) {
      balance.increment();
  }
}

class Balance {