Compare commits
2 Commits
dbd83223fe
...
c89a9b3a90
Author | SHA1 | Date | |
---|---|---|---|
c89a9b3a90 | |||
9be4e8a359 |
42
src/exercises/ex_s2/TestingAtomicInteger.java
Normal file
42
src/exercises/ex_s2/TestingAtomicInteger.java
Normal file
@ -0,0 +1,42 @@
|
||||
package exercises.ex_s2;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicIntegerArray;
|
||||
|
||||
public class TestingAtomicInteger {
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
int[] nonAtomic = new int[5];
|
||||
AtomicIntegerArray atomic = new AtomicIntegerArray(5);
|
||||
|
||||
for (int i = 0; i < 5; i++) {
|
||||
nonAtomic[i] = i;
|
||||
atomic.set(i, i);
|
||||
}
|
||||
|
||||
System.out.println("Starting values atomic array: " + atomic);
|
||||
System.out.println("Starting values of non atomic array: " + Arrays.toString(nonAtomic));
|
||||
System.out.println("Creating 3 threads to increment the values within those two arrays");
|
||||
|
||||
Thread[] threads = new Thread[3];
|
||||
for (int i = 0; i < 3; i++) {
|
||||
threads[i] = new Thread(() -> {
|
||||
for (int j = 0; j < 100000; j++) {
|
||||
for (int k = 0; k < 5; k++) {
|
||||
nonAtomic[k] += 1;
|
||||
atomic.incrementAndGet(k);
|
||||
}
|
||||
}
|
||||
});
|
||||
threads[i].start();
|
||||
}
|
||||
|
||||
for (int i = 0; i < 3; i++) {
|
||||
threads[i].join();
|
||||
}
|
||||
|
||||
System.out.println("Final values atomic array: " + atomic);
|
||||
System.out.println("Final values of non atomic array: " + Arrays.toString(nonAtomic));
|
||||
}
|
||||
}
|
20
src/exercises/ex_s3/AbstractTweet.java
Normal file
20
src/exercises/ex_s3/AbstractTweet.java
Normal file
@ -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)";
|
||||
}
|
||||
}
|
27
src/exercises/ex_s3/AtomicTweet.java
Normal file
27
src/exercises/ex_s3/AtomicTweet.java
Normal file
@ -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();
|
||||
}
|
||||
}
|
32
src/exercises/ex_s3/TestingAtomicLong.java
Normal file
32
src/exercises/ex_s3/TestingAtomicLong.java
Normal file
@ -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<AbstractTweet> 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);
|
||||
}
|
||||
}
|
21
src/exercises/ex_s3/Tweet.java
Normal file
21
src/exercises/ex_s3/Tweet.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
24
src/exercises/ex_s3/User.java
Normal file
24
src/exercises/ex_s3/User.java
Normal file
@ -0,0 +1,24 @@
|
||||
package exercises.ex_s3;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class User implements Runnable {
|
||||
private final String name;
|
||||
private final List<AbstractTweet> tweets;
|
||||
|
||||
public User(String name, List<AbstractTweet> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user