Skip to content

Commit 34e86c2

Browse files
committed
Merge branch 'master' of https://github.com/eugenp/tutorials into task/BAEL-9567
2 parents 4058bfc + a797be2 commit 34e86c2

File tree

109 files changed

+4042
-761
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

109 files changed

+4042
-761
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.baeldung.java.list;
2+
3+
import java.util.Arrays;
4+
import java.util.Iterator;
5+
import java.util.List;
6+
import java.util.ListIterator;
7+
8+
/**
9+
* Demonstrates the different ways to loop over
10+
* the elements of a list.
11+
*/
12+
public class WaysToIterate {
13+
14+
List<String> countries = Arrays.asList("Germany", "Panama", "Australia");
15+
16+
/**
17+
* Iterate over a list using a basic for loop
18+
*/
19+
public void iterateWithForLoop() {
20+
for (int i = 0; i < countries.size(); i++) {
21+
System.out.println(countries.get(i));
22+
}
23+
}
24+
25+
/**
26+
* Iterate over a list using the enhanced for loop
27+
*/
28+
public void iterateWithEnhancedForLoop() {
29+
for (String country : countries) {
30+
System.out.println(country);
31+
}
32+
}
33+
34+
/**
35+
* Iterate over a list using an Iterator
36+
*/
37+
public void iterateWithIterator() {
38+
Iterator<String> countriesIterator = countries.iterator();
39+
while(countriesIterator.hasNext()) {
40+
System.out.println(countriesIterator.next());
41+
}
42+
}
43+
44+
/**
45+
* Iterate over a list using a ListIterator
46+
*/
47+
public void iterateWithListIterator() {
48+
ListIterator<String> listIterator = countries.listIterator();
49+
while(listIterator.hasNext()) {
50+
System.out.println(listIterator.next());
51+
}
52+
}
53+
54+
/**
55+
* Iterate over a list using the Iterable.forEach() method
56+
*/
57+
public void iterateWithForEach() {
58+
countries.forEach(System.out::println);
59+
}
60+
61+
/**
62+
* Iterate over a list using the Stream.forEach() method
63+
*/
64+
public void iterateWithStreamForEach() {
65+
countries.stream().forEach((c) -> System.out.println(c));
66+
}
67+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.baeldung.java.list;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.Iterator;
8+
import java.util.List;
9+
import java.util.ListIterator;
10+
11+
import org.junit.Test;
12+
13+
public class WaysToIterateUnitTest {
14+
15+
List<String> globalCountries = new ArrayList<String>();
16+
List<String> europeanCountries = Arrays.asList("Germany", "Panama", "Australia");
17+
18+
@Test
19+
public void whenIteratingUsingForLoop_thenReturnThreeAsSizeOfList() {
20+
for (int i = 0; i < europeanCountries.size(); i++) {
21+
globalCountries.add(europeanCountries.get(i));
22+
}
23+
assertEquals(globalCountries.size(), 3);
24+
globalCountries.clear();
25+
}
26+
27+
@Test
28+
public void whenIteratingUsingEnhancedForLoop_thenReturnThreeAsSizeOfList() {
29+
for (String country : europeanCountries) {
30+
globalCountries.add(country);
31+
}
32+
assertEquals(globalCountries.size(), 3);
33+
globalCountries.clear();
34+
}
35+
36+
@Test
37+
public void whenIteratingUsingIterator_thenReturnThreeAsSizeOfList() {
38+
Iterator<String> countriesIterator = europeanCountries.iterator();
39+
while (countriesIterator.hasNext()) {
40+
globalCountries.add(countriesIterator.next());
41+
}
42+
43+
assertEquals(globalCountries.size(), 3);
44+
globalCountries.clear();
45+
}
46+
47+
@Test
48+
public void whenIteratingUsingListIterator_thenReturnThreeAsSizeOfList() {
49+
ListIterator<String> countriesIterator = europeanCountries.listIterator();
50+
while (countriesIterator.hasNext()) {
51+
globalCountries.add(countriesIterator.next());
52+
}
53+
54+
assertEquals(globalCountries.size(), 3);
55+
globalCountries.clear();
56+
}
57+
58+
@Test
59+
public void whenIteratingUsingForEach_thenReturnThreeAsSizeOfList() {
60+
europeanCountries.forEach(country -> globalCountries.add(country));
61+
assertEquals(globalCountries.size(), 3);
62+
globalCountries.clear();
63+
}
64+
65+
@Test
66+
public void whenIteratingUsingStreamForEach_thenReturnThreeAsSizeOfList() {
67+
europeanCountries.stream().forEach((country) -> globalCountries.add(country));
68+
assertEquals(globalCountries.size(), 3);
69+
globalCountries.clear();
70+
}
71+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.concurrent.countdownlatch;
2+
3+
import java.util.concurrent.CountDownLatch;
4+
5+
public class CountdownLatchCountExample {
6+
7+
private int count;
8+
9+
public CountdownLatchCountExample(int count) {
10+
this.count = count;
11+
}
12+
13+
public boolean callTwiceInSameThread() {
14+
CountDownLatch countDownLatch = new CountDownLatch(count);
15+
Thread t = new Thread(() -> {
16+
countDownLatch.countDown();
17+
countDownLatch.countDown();
18+
});
19+
t.start();
20+
21+
try {
22+
countDownLatch.await();
23+
} catch (InterruptedException e) {
24+
e.printStackTrace();
25+
}
26+
return countDownLatch.getCount() == 0;
27+
}
28+
29+
public static void main(String[] args) {
30+
CountdownLatchCountExample ex = new CountdownLatchCountExample(2);
31+
System.out.println("Is CountDown Completed : " + ex.callTwiceInSameThread());
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.concurrent.countdownlatch;
2+
3+
import java.util.concurrent.CountDownLatch;
4+
import java.util.concurrent.ExecutorService;
5+
import java.util.concurrent.Executors;
6+
import java.util.concurrent.atomic.AtomicInteger;
7+
8+
public class CountdownLatchResetExample {
9+
10+
private int count;
11+
private int threadCount;
12+
private final AtomicInteger updateCount;
13+
14+
CountdownLatchResetExample(int count, int threadCount) {
15+
updateCount = new AtomicInteger(0);
16+
this.count = count;
17+
this.threadCount = threadCount;
18+
}
19+
20+
public int countWaits() {
21+
CountDownLatch countDownLatch = new CountDownLatch(count);
22+
ExecutorService es = Executors.newFixedThreadPool(threadCount);
23+
for (int i = 0; i < threadCount; i++) {
24+
es.execute(() -> {
25+
long prevValue = countDownLatch.getCount();
26+
countDownLatch.countDown();
27+
if (countDownLatch.getCount() != prevValue) {
28+
updateCount.incrementAndGet();
29+
}
30+
});
31+
}
32+
33+
es.shutdown();
34+
return updateCount.get();
35+
}
36+
37+
public static void main(String[] args) {
38+
CountdownLatchResetExample ex = new CountdownLatchResetExample(5, 20);
39+
System.out.println("Count : " + ex.countWaits());
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.baeldung.concurrent.cyclicbarrier;
2+
3+
import java.util.concurrent.BrokenBarrierException;
4+
import java.util.concurrent.CyclicBarrier;
5+
import java.util.concurrent.ExecutorService;
6+
import java.util.concurrent.Executors;
7+
import java.util.concurrent.atomic.AtomicInteger;
8+
9+
public class CyclicBarrierCompletionMethodExample {
10+
11+
private int count;
12+
private int threadCount;
13+
private final AtomicInteger updateCount;
14+
15+
CyclicBarrierCompletionMethodExample(int count, int threadCount) {
16+
updateCount = new AtomicInteger(0);
17+
this.count = count;
18+
this.threadCount = threadCount;
19+
}
20+
21+
public int countTrips() {
22+
23+
CyclicBarrier cyclicBarrier = new CyclicBarrier(count, () -> {
24+
updateCount.incrementAndGet();
25+
});
26+
27+
ExecutorService es = Executors.newFixedThreadPool(threadCount);
28+
for (int i = 0; i < threadCount; i++) {
29+
es.execute(() -> {
30+
try {
31+
cyclicBarrier.await();
32+
} catch (InterruptedException | BrokenBarrierException e) {
33+
e.printStackTrace();
34+
}
35+
});
36+
}
37+
es.shutdown();
38+
return updateCount.get();
39+
}
40+
41+
public static void main(String[] args) {
42+
CyclicBarrierCompletionMethodExample ex = new CyclicBarrierCompletionMethodExample(5, 20);
43+
System.out.println("Count : " + ex.countTrips());
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.concurrent.cyclicbarrier;
2+
3+
import java.util.concurrent.BrokenBarrierException;
4+
import java.util.concurrent.CyclicBarrier;
5+
6+
public class CyclicBarrierCountExample {
7+
8+
private int count;
9+
10+
public CyclicBarrierCountExample(int count) {
11+
this.count = count;
12+
}
13+
14+
public boolean callTwiceInSameThread() {
15+
CyclicBarrier cyclicBarrier = new CyclicBarrier(count);
16+
Thread t = new Thread(() -> {
17+
try {
18+
cyclicBarrier.await();
19+
cyclicBarrier.await();
20+
} catch (InterruptedException | BrokenBarrierException e) {
21+
e.printStackTrace();
22+
}
23+
});
24+
t.start();
25+
return cyclicBarrier.isBroken();
26+
}
27+
28+
public static void main(String[] args) {
29+
CyclicBarrierCountExample ex = new CyclicBarrierCountExample(7);
30+
System.out.println("Count : " + ex.callTwiceInSameThread());
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.baeldung.concurrent.cyclicbarrier;
2+
3+
import java.util.concurrent.BrokenBarrierException;
4+
import java.util.concurrent.CyclicBarrier;
5+
import java.util.concurrent.ExecutorService;
6+
import java.util.concurrent.Executors;
7+
import java.util.concurrent.atomic.AtomicInteger;
8+
9+
public class CyclicBarrierResetExample {
10+
11+
private int count;
12+
private int threadCount;
13+
private final AtomicInteger updateCount;
14+
15+
CyclicBarrierResetExample(int count, int threadCount) {
16+
updateCount = new AtomicInteger(0);
17+
this.count = count;
18+
this.threadCount = threadCount;
19+
}
20+
21+
public int countWaits() {
22+
23+
CyclicBarrier cyclicBarrier = new CyclicBarrier(count);
24+
25+
ExecutorService es = Executors.newFixedThreadPool(threadCount);
26+
for (int i = 0; i < threadCount; i++) {
27+
es.execute(() -> {
28+
try {
29+
if (cyclicBarrier.getNumberWaiting() > 0) {
30+
updateCount.incrementAndGet();
31+
}
32+
cyclicBarrier.await();
33+
} catch (InterruptedException | BrokenBarrierException e) {
34+
e.printStackTrace();
35+
}
36+
});
37+
}
38+
es.shutdown();
39+
return updateCount.get();
40+
}
41+
42+
public static void main(String[] args) {
43+
CyclicBarrierResetExample ex = new CyclicBarrierResetExample(7, 20);
44+
System.out.println("Count : " + ex.countWaits());
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.concurrent.countdownlatch;
2+
3+
import static org.junit.Assert.assertTrue;
4+
5+
import org.junit.Test;
6+
7+
public class CountdownLatchCountExampleUnitTest {
8+
9+
@Test
10+
public void whenCountDownLatch_completed() {
11+
CountdownLatchCountExample ex = new CountdownLatchCountExample(2);
12+
boolean isCompleted = ex.callTwiceInSameThread();
13+
assertTrue(isCompleted);
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.concurrent.countdownlatch;
2+
3+
import static org.junit.Assert.assertTrue;
4+
5+
import org.junit.Test;
6+
7+
public class CountdownLatchResetExampleUnitTest {
8+
9+
@Test
10+
public void whenCountDownLatch_noReset() {
11+
CountdownLatchResetExample ex = new CountdownLatchResetExample(7,20);
12+
int lineCount = ex.countWaits();
13+
assertTrue(lineCount <= 7);
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.concurrent.cyclicbarrier;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.Test;
6+
7+
public class CyclicBarrierCompletionMethodExampleUnitTest {
8+
9+
@Test
10+
public void whenCyclicBarrier_countTrips() {
11+
CyclicBarrierCompletionMethodExample ex = new CyclicBarrierCompletionMethodExample(7,20);
12+
int lineCount = ex.countTrips();
13+
assertEquals(2, lineCount);
14+
}
15+
}

0 commit comments

Comments
 (0)