Skip to content

Commit f87e731

Browse files
author
Dhananjay Nagargoje
committed
lamda streams threading
1 parent e6fb58f commit f87e731

15 files changed

+353
-179
lines changed

Diff for: .idea/workspace.xml

+166-175
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
2.59 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
1.48 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package javarecipies.concurrency.creatingthreads;
2+
3+
import java.util.concurrent.ExecutorService;
4+
import java.util.concurrent.Executors;
5+
import java.util.concurrent.TimeUnit;
6+
import java.util.concurrent.locks.Lock;
7+
import java.util.concurrent.locks.ReentrantLock;
8+
import java.util.stream.IntStream;
9+
10+
public class OneSynchronizedUse {
11+
12+
static Object lock = new Object();
13+
static Lock reentrantLock = new ReentrantLock();
14+
static class Counter {
15+
private int count ;
16+
17+
public void inc() {
18+
count++;
19+
}
20+
21+
public synchronized void inc1() {
22+
count++;
23+
}
24+
25+
public void inc2() {
26+
synchronized (lock) {
27+
count++;
28+
lock.notifyAll();
29+
}
30+
}
31+
32+
public synchronized void inc3() {
33+
reentrantLock.lock();
34+
count++;
35+
reentrantLock.unlock();
36+
}
37+
38+
public int getCount() {
39+
return count;
40+
}
41+
}
42+
43+
public static void process() {
44+
45+
Counter c = new Counter();
46+
47+
Thread t1 = new Thread(new Runnable() {
48+
@Override
49+
public void run() {
50+
for (int i = 0; i < 99; i++) c.inc();
51+
}
52+
});
53+
54+
55+
Thread t2 = new Thread(new Runnable() {
56+
@Override
57+
public void run() {
58+
for (int i = 0; i < 98; i++) c.inc();
59+
}
60+
});
61+
62+
63+
Thread t3 = new Thread(new Runnable() {
64+
@Override
65+
public void run() {
66+
for (int i = 0; i < 97; i++) c.inc();
67+
}
68+
});
69+
70+
t1.start();
71+
t2.start();
72+
t3.start();
73+
74+
try {
75+
t1.join();
76+
t2.join();
77+
t3.join();
78+
} catch (InterruptedException e) {
79+
e.printStackTrace();
80+
}
81+
ExecutorService service = Executors.newFixedThreadPool(5);
82+
Counter summation = new Counter();
83+
84+
IntStream.range(0, 963)
85+
.forEach(count -> service.submit(summation::inc));
86+
try {
87+
service.awaitTermination(1000, TimeUnit.MILLISECONDS);
88+
} catch (InterruptedException e) {
89+
e.printStackTrace();
90+
}
91+
92+
System.out.println("Final Counter Value : " + summation.getCount());
93+
}
94+
95+
public static void main(String[] args) {
96+
97+
process();
98+
99+
}
100+
}

Diff for: src/main/java/javarecipies/java8/MyReducer.java

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package javarecipies.java8;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.function.BinaryOperator;
6+
7+
public class MyReducer {
8+
static <T> T reduce(List<T> values, T identity, BinaryOperator<T> binaryOperator) {
9+
T result = identity;
10+
for (T value : values) {
11+
result = binaryOperator.apply(result, value);
12+
}
13+
return result;
14+
}
15+
16+
public static void main(String[] args) {
17+
BinaryOperator<Integer> adder = (integer, integer2) -> integer + integer2;
18+
List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
19+
20+
System.out.println(reduce(integers, 0, adder));
21+
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package javarecipies.java8;
2+
3+
import java.util.*;
4+
import java.util.function.Consumer;
5+
6+
public class NewCollectionMethods {
7+
8+
public static void main(String[] args) {
9+
Map<Integer, ArrayList<Integer>> map1 = new HashMap<>();
10+
Map<Integer, ArrayList<Integer>> map2 = new HashMap<>();
11+
ArrayList<Integer> l1 = new ArrayList<>();
12+
ArrayList<Integer> l2 = new ArrayList<>();
13+
l1.add(1);
14+
l1.add(2);
15+
l1.add(3);
16+
17+
l2.add(4);
18+
l2.add(5);
19+
l2.add(6);
20+
21+
map1.putIfAbsent(1, l1);
22+
map2.putIfAbsent(1, l2);
23+
24+
Consumer<Object> cout = System.out::println;
25+
cout.accept(map1);
26+
cout.accept(map2);
27+
28+
cout.accept("Merging map2 into map1");
29+
30+
map2.forEach((key2, val2) -> {
31+
map1.merge(key2, val2, (oldval, newvalue) -> {
32+
oldval.addAll(newvalue);
33+
return oldval;
34+
});
35+
});
36+
37+
cout.accept(map1);
38+
39+
40+
}
41+
42+
}

Diff for: src/main/java/javarecipies/java8/NewFunctionalInterfaces.java

-4
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ public static void main(String[] args) {
2727
Predicate<Integer> isGreaterThan10 = x-> x>10;
2828
printer.accept(isGreaterThan10.test(9));
2929

30-
31-
32-
33-
3430
}
3531

3632
}

Diff for: src/main/java/javarecipies/java8/StreamsAnatomy.java

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package javarecipies.java8;
2+
3+
import edu.princeton.cs.algs4.In;
4+
5+
import java.util.Arrays;
6+
import java.util.Comparator;
7+
import java.util.List;
8+
import java.util.stream.Collectors;
9+
10+
public class StreamsAnatomy {
11+
public static void main(String[] args) {
12+
List<Integer> profits = Arrays.asList(4,5,2,6,4,7,8,3,45,6,345,3324,576,2342,33);
13+
14+
//find first three max profits
15+
List<Integer> maxThree = profits.stream()//stream source is collection
16+
.sorted(Comparator.reverseOrder()) //intermidiate stateful operation
17+
.limit(3)
18+
.collect(Collectors.toList()); //termainal operation
19+
20+
System.out.println(maxThree);
21+
}
22+
}

0 commit comments

Comments
 (0)