Compare commits
No commits in common. "c89a9b3a90a60de87a8f12c6645009006d00a6c4" and "dbd83223fed0ff6ac0eeff6c4a8b19be24ec766f" have entirely different histories.
c89a9b3a90
...
dbd83223fe
@ -1,42 +0,0 @@
|
|||||||
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));
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,20 +0,0 @@
|
|||||||
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)";
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,27 +0,0 @@
|
|||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,32 +0,0 @@
|
|||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,21 +0,0 @@
|
|||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,24 +0,0 @@
|
|||||||
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