Skip to content

Commit 7d0aee1

Browse files
committed
Day 3 starting point for Collections.synchronized... lab
1 parent 4d7664e commit 7d0aee1

12 files changed

Lines changed: 25 additions & 7 deletions

File tree

Capture 21.png

3.19 MB
Loading

Capture 22.png

3.19 MB
Loading

Capture 23.png

3.3 MB
Loading

Capture 24.png

3.2 MB
Loading

Capture 25.png

3.54 MB
Loading

Capture 26.png

3.27 MB
Loading

Capture 27.png

3.47 MB
Loading

Capture 28.png

3.26 MB
Loading

Capture 29.png

3.36 MB
Loading
Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
package runnables;
22

3+
import java.util.concurrent.atomic.AtomicInteger;
4+
import java.util.concurrent.atomic.AtomicLong;
5+
36
public class Counter {
47
public static long count = 0;
8+
// public static AtomicLong count = new AtomicLong();
59

610
public static void main(String[] args) throws InterruptedException {
711
Runnable r = () -> {
8-
for (int i = 0; i < 100_000; i++) {
9-
count++;
12+
for (int i = 0; i < 1_000_000_000L; i++) {
13+
synchronized (Counter.class) {
14+
count++;
15+
}
16+
// count.incrementAndGet();
1017
}
1118
};
1219

1320
System.out.println("count is " + count);
21+
long start = System.nanoTime();
1422
Thread t = new Thread(r);
1523
t.start();
1624
Thread t2 = new Thread(r);
@@ -21,6 +29,8 @@ public static void main(String[] args) throws InterruptedException {
2129
// to the continuation of the calling thread
2230
t.join();
2331
t2.join();
24-
System.out.println("count is " + count);
32+
long time = System.nanoTime() - start;
33+
System.out.println("count is " + count + ". Time was " + (time / 1_000_000_000.0));
34+
2535
}
2636
}

0 commit comments

Comments
 (0)