Skip to content

Commit 11b6174

Browse files
Merge remote-tracking branch 'origin/master'
2 parents 858649c + 21e5850 commit 11b6174

File tree

6 files changed

+167
-3
lines changed

6 files changed

+167
-3
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.howtodoinjava.concurrency;
2+
3+
import java.util.concurrent.*;
4+
5+
public class BlockingThreadPoolExecutor extends ThreadPoolExecutor {
6+
private final Semaphore semaphore;
7+
8+
public BlockingThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime,
9+
TimeUnit unit, BlockingQueue<Runnable> workQueue) {
10+
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
11+
semaphore = new Semaphore(maximumPoolSize);
12+
}
13+
14+
@Override
15+
protected void beforeExecute(Thread t, Runnable r) {
16+
super.beforeExecute(t, r);
17+
}
18+
19+
20+
@Override
21+
public void execute(final Runnable task) {
22+
23+
boolean acquired = false;
24+
25+
do {
26+
try {
27+
semaphore.acquire();
28+
acquired = true;
29+
} catch (final InterruptedException e) {
30+
e.printStackTrace();
31+
}
32+
} while (!acquired);
33+
34+
try {
35+
super.execute(task);
36+
} catch (final RejectedExecutionException e) {
37+
System.out.println("Task Rejected");
38+
semaphore.release();
39+
return;
40+
}
41+
semaphore.release();
42+
}
43+
44+
@Override
45+
protected void afterExecute(Runnable r, Throwable t) {
46+
super.afterExecute(r, t);
47+
if (t != null) {
48+
t.printStackTrace();
49+
}
50+
}
51+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.howtodoinjava.concurrency;
2+
3+
import java.util.concurrent.ArrayBlockingQueue;
4+
import java.util.concurrent.BlockingQueue;
5+
import java.util.concurrent.TimeUnit;
6+
7+
public class BlockingThreadPoolExecutorDemo {
8+
public static void main(String[] args) throws InterruptedException {
9+
BlockingQueue<Runnable> blockingQueue = new ArrayBlockingQueue<>(1);
10+
BlockingThreadPoolExecutor executor = new BlockingThreadPoolExecutor(1, 1, 5000, TimeUnit.MILLISECONDS, blockingQueue);
11+
executor.setRejectedExecutionHandler(new CustomRejectedExecutionHandler());
12+
13+
executor.prestartAllCoreThreads();
14+
15+
int threadCounter = 0;
16+
while (true) {
17+
threadCounter++;
18+
// Adding threads one by one
19+
System.out.println("Adding DemoTask : " + threadCounter);
20+
blockingQueue.offer(new DemoTask(Integer.toString(threadCounter)));
21+
if (threadCounter == 100)
22+
break;
23+
}
24+
25+
Thread.sleep(1000000);
26+
}
27+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.howtodoinjava.concurrency;
2+
3+
import java.util.concurrent.RejectedExecutionHandler;
4+
import java.util.concurrent.ThreadPoolExecutor;
5+
6+
public class CustomRejectedExecutionHandler implements RejectedExecutionHandler {
7+
@Override
8+
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
9+
try
10+
{
11+
Thread.sleep(1000);
12+
System.out.println("Try again : " + ((DemoTask) r).getName());
13+
executor.execute(r);
14+
} catch (Exception e) {
15+
e.printStackTrace();
16+
}
17+
}
18+
}

src/main/java/com/howtodoinjava/concurrency/DemoTask.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ public String getName() {
1313

1414
@Override
1515
public void run() {
16+
System.out.println("Executing : " + name);
1617
try {
17-
Thread.sleep(500);
18+
Thread.sleep(1000);
1819
} catch (InterruptedException e) {
1920
e.printStackTrace();
2021
}
21-
System.out.println("Executing : " + name);
2222
}
2323
}

src/main/java/com/howtodoinjava/core/collections/map/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
* [Java WeakHashMap](https://howtodoinjava.com/java/collections/java-weakhashmap/)
55
* [Java IdentityHashMap](https://howtodoinjava.com/java/collections/java-identityhashmap/)
66
* [Java Nested Map](https://howtodoinjava.com/java/collections/java-nested-map/)
7-
* [Java EnumMap](https://howtodoinjava.com/java/collections/java-enummap/)
7+
* [Java EnumMap](https://howtodoinjava.com/java/collections/java-enummap/)
8+
* [Java SubMap](https://howtodoinjava.com/java/collections/java-submap/)
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.howtodoinjava.core.collections.map;
2+
3+
import com.google.common.base.Predicates;
4+
import com.google.common.collect.Maps;
5+
6+
import java.util.*;
7+
import java.util.stream.Collectors;
8+
9+
public class SubMaps {
10+
public static void main(String[] args) {
11+
//1
12+
Map<String, String> map = new HashMap<>();
13+
map.put("key1", "Value1");
14+
map.put("key2", "Value2");
15+
map.put("key3", "Value3");
16+
17+
List<String> keysToRetain = List.of("key1", "key2");
18+
map.keySet().retainAll(keysToRetain);
19+
20+
System.out.println(map); //{key1=Value1, key2=Value2}
21+
22+
//2
23+
TreeMap<String, String> treeMap = new TreeMap<>();
24+
25+
treeMap.put("key1", "Value1");
26+
treeMap.put("key2", "Value2");
27+
treeMap.put("key3", "Value3");
28+
29+
SortedMap<String, String> subMap = treeMap.subMap("key2", "key3");
30+
SortedMap<String, String> headMap = treeMap.headMap("key2");
31+
SortedMap<String, String> tailMap = treeMap.tailMap("key2");
32+
33+
System.out.println(subMap);
34+
System.out.println(headMap);
35+
System.out.println(tailMap);
36+
37+
subMap.put("key2", "NEW_VALUE_2");
38+
39+
System.out.println(subMap); //{key2=NEW_VALUE_2}
40+
System.out.println(treeMap); //{key1=Value1, key2=NEW_VALUE_2, key3=Value3}
41+
42+
treeMap.put("key4", "value4");
43+
44+
System.out.println(tailMap); //{key2=NEW_VALUE_2, key3=Value3, key4=value4}
45+
System.out.println(treeMap); //{key1=Value1, key2=NEW_VALUE_2, key3=Value3, key4=value4}
46+
47+
//3
48+
Map<Integer, String> hashmap = new HashMap<>();
49+
hashmap.put(1, "Value1");
50+
hashmap.put(2, "Value2");
51+
hashmap.put(3, "Value3");
52+
53+
List<Integer> keysList = Arrays.asList(1, 2);
54+
55+
Map<Integer, String> subHashmap = hashmap.entrySet()
56+
.stream()
57+
.filter(x -> keysList.contains(x.getKey()))
58+
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
59+
60+
System.out.println(subHashmap); //{1=Value1, 2=Value2}
61+
System.out.println(hashmap);
62+
63+
//4
64+
Map<Integer, String> subMap1 = Maps.filterKeys(hashmap, Predicates.in(keysList));
65+
System.out.print(subMap1);
66+
}
67+
}

0 commit comments

Comments
 (0)