diff --git a/src/exercises/ex_s3/AbstractTweet.java b/src/exercises/ex_s3/AbstractTweet.java new file mode 100644 index 0000000..7481348 --- /dev/null +++ b/src/exercises/ex_s3/AbstractTweet.java @@ -0,0 +1,20 @@ +package exercises.ex_s3; + +public abstract class AbstractTweet { + private String author; + private String content; + + public AbstractTweet(String author, String content) { + this.author = author; + this.content = content; + } + + public abstract void addLike(); + public abstract void removeLike(); + public abstract long getLikes(); + + @Override + public String toString() { + return "@" + author + " tweeted \"" + content + "\" (" + getLikes() + " likes)"; + } +} diff --git a/src/exercises/ex_s3/AtomicTweet.java b/src/exercises/ex_s3/AtomicTweet.java new file mode 100644 index 0000000..ffc8802 --- /dev/null +++ b/src/exercises/ex_s3/AtomicTweet.java @@ -0,0 +1,27 @@ +package exercises.ex_s3; + +import java.util.concurrent.atomic.AtomicLong; + +public class AtomicTweet extends AbstractTweet { + + private AtomicLong likes = new AtomicLong(0); + + public AtomicTweet(String author, String content) { + super(author, content); + } + + @Override + public void addLike() { + likes.incrementAndGet(); + } + + @Override + public void removeLike() { + likes.updateAndGet(i -> i > 0 ? i - 1 : i); + } + + @Override + public long getLikes() { + return likes.get(); + } +} diff --git a/src/exercises/ex_s3/TestingAtomicLong.java b/src/exercises/ex_s3/TestingAtomicLong.java new file mode 100644 index 0000000..dad2c05 --- /dev/null +++ b/src/exercises/ex_s3/TestingAtomicLong.java @@ -0,0 +1,32 @@ +package exercises.ex_s3; + +import java.util.ArrayList; +import java.util.List; + +public class TestingAtomicLong { + public static String[] names = { + "Alice", "Bob", "Charlie", "Derek", "Emily", + "Fionna", "Greg", "Harry", "Isabella", "Julia" + }; + public static void main(String[] args) throws InterruptedException { + List tweets = new ArrayList<>(); + AbstractTweet tweet1 = new Tweet("Alice", "Java is cool !"); + AbstractTweet tweet2 = new AtomicTweet("Bob", "ISC is the best !"); + tweets.add(tweet1); + tweets.add(tweet2); + + Thread[] users = new Thread[10]; + + for (int i = 0; i < 10; i++) { + users[i] = new Thread(new User(names[i], tweets)); + users[i].start(); + } + + for (int i = 0; i < 10; i++) { + users[i].join(); + } + + System.out.println(tweet1); + System.out.println(tweet2); + } +} diff --git a/src/exercises/ex_s3/Tweet.java b/src/exercises/ex_s3/Tweet.java new file mode 100644 index 0000000..c2fe9c5 --- /dev/null +++ b/src/exercises/ex_s3/Tweet.java @@ -0,0 +1,21 @@ +package exercises.ex_s3; + +public class Tweet extends AbstractTweet { + private long likes = 0; + + public Tweet(String author, String content) { + super(author, content); + } + + public void addLike() { + likes += 1; + } + + public void removeLike() { + likes -= 1; + } + + public long getLikes() { + return likes; + } +} diff --git a/src/exercises/ex_s3/User.java b/src/exercises/ex_s3/User.java new file mode 100644 index 0000000..2cdc9da --- /dev/null +++ b/src/exercises/ex_s3/User.java @@ -0,0 +1,24 @@ +package exercises.ex_s3; + +import java.util.List; + +public class User implements Runnable { + private final String name; + private final List tweets; + + public User(String name, List tweets) { + this.name = name; + this.tweets = tweets; + } + + @Override + public void run() { + for (int i = 0; i < 100000; i++) { + if (i % 10 == 9) { + tweets.forEach(AbstractTweet::removeLike); + } else { + tweets.forEach(AbstractTweet::addLike); + } + } + } +}