Commit 4e9becc9 authored by Trisha Gee's avatar Trisha Gee
Browse files

[Ch15] Moving code magnets examples into their own package so I can create...

[Ch15] Moving code magnets examples into their own package so I can create more examples on the same topic
parent 4cbde337
Loading
Loading
Loading
Loading
+23 −0
Changes for src/main/java/ch15/exercise1/AddLowerCaseJob.java: 23 added lines, 0 removed lines.
Original line number Diff line number Diff line
package ch15.exercise1;

class AddLowerCaseJob implements Runnable {
  private final Data data;
  AddLowerCaseJob(Data data) {
    this.data = data;
  }

  public void run() {
    char letter = 'a';

    for (int i = 0; i < 26; i++) {
      data.addLetter(letter++);
      try {
        Thread.sleep(50);
      } catch (InterruptedException ignored) {
      }
    }
    System.out.println(Thread.currentThread().getName() + data.getLetters());
    System.out.println(Thread.currentThread().getName() + "getLetters().size() = " + data.getLetters().size());
  }

}
+21 −0
Changes for src/main/java/ch15/exercise1/AddUpperCaseJob.java: 21 added lines, 0 removed lines.
Original line number Diff line number Diff line
package ch15.exercise1;

class AddUpperCaseJob implements Runnable {
  private final Data data;
  AddUpperCaseJob(Data data) {
    this.data = data;
  }
  public void run() {
    char letter = 'A';
    for (int i = 0; i < 26; i++) {
      data.addLetter(letter++);
      try {
        Thread.sleep(50);
      } catch (InterruptedException ignored) {
      }
    }
    System.out.println(Thread.currentThread().getName() + data.getLetters());
    System.out.println(Thread.currentThread().getName() + "getLetters().size() = " + data.getLetters().size());
  }

}
+10 −0
Changes for src/main/java/ch15/exercise1/Data.java: 10 added lines, 0 removed lines.
Original line number Diff line number Diff line
package ch15.exercise1;

import java.util.Collection;
import java.util.List;

public interface Data {
  void addLetter(char c);

  List<String> getLetters();
}
+5 −46
Changes for src/main/java/ch15/exercise1/NotThreadSafeCollection.java: 5 added lines, 46 removed lines.
Original line number Diff line number Diff line
package ch15;
package ch15.exercise1;

import java.util.ArrayList;
import java.util.List;
@@ -8,22 +8,15 @@ import java.util.concurrent.Executors;
public class NotThreadSafeCollection {
  public static void main(String[] args) {
    ExecutorService threadPool = Executors.newFixedThreadPool(2);
    threadPool.execute(new JobOne());
    threadPool.execute(new JobTwo());
    DataImpl data = new DataImpl();
    threadPool.execute(new AddLowerCaseJob(data));
    threadPool.execute(new AddUpperCaseJob(data));
    threadPool.shutdown();
  }
}

class Data {
class DataImpl implements Data {
  private final List<String> letters = new ArrayList<>();
  private final static Data instance = new Data();

  private Data() {
  }

  public static Data getInstance() {
    return instance;
  }

  public List<String> getLetters() {
    return letters;
@@ -33,37 +26,3 @@ class Data {
    letters.add(String.valueOf(letter));
  }
}

class JobOne implements Runnable {
  Data a = Data.getInstance();
  char letter = 'A';

  public void run() {
    for (int x = 0; x < 26; x++) {
      a.addLetter(letter++);
      try {
        Thread.sleep(50);
      } catch (InterruptedException ignored) {
      }
    }
    System.out.println("one " + a.getLetters());
    System.out.println("a.getLetters().size() = " + a.getLetters().size());
  }
}

class JobTwo implements Runnable {
  Data a = Data.getInstance();
  char letter = 'a';

  public void run() {
    for (int x = 0; x < 26; x++) {
      a.addLetter(letter++);
      try {
        Thread.sleep(50);
      } catch (InterruptedException ignored) {
      }
    }
    System.out.println("two " + a.getLetters());
    System.out.println("a.getLetters().size() = " + a.getLetters().size());
  }
}
+1 −1
Changes for src/main/java/ch15/exercise1/TestThreads.java: 1 added line, 1 removed line.
Original line number Diff line number Diff line
package ch15;
package ch15.exercise1;

public class TestThreads {
  public static void main(String[] args) {
Loading