Skip to content

Instantly share code, notes, and snippets.

@magnusram05
magnusram05 / LambdaExpression.java
Last active November 5, 2019 04:23
Java 8: Lambda Expression, Functional Interface and Stream
public class ProductUtil {
private static final Comparator<Product> sortByRatingAscStrategy = (product1, product2) -> {
if (product1 == null || product2 == null)
return -1;
return Double.compare(product1.getRating(), product2.getRating());
};
public static void main(String [] args){
List<Product> productList = new ArrayList<>(2);
@magnusram05
magnusram05 / CustomThreadPoolExecutor.java
Created June 30, 2019 09:58
Custom ThreadPoolExecutor
static class CustomThreadPoolExecutor extends ThreadPoolExecutor {
CustomThreadPoolExecutor(int corePoolSize,
int maxPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue blockingQueue) {
super(corePoolSize, maxPoolSize, keepAliveTime, unit, blockingQueue,
SearchThreadFactory.newThreadFactory(),
new CallerRunsPolicy());
}
<div class="mxgraph" style="max-width:100%;border:1px solid transparent;" data-mxgraph="{&quot;highlight&quot;:&quot;#0000ff&quot;,&quot;nav&quot;:true,&quot;resize&quot;:true,&quot;toolbar&quot;:&quot;zoom layers lightbox&quot;,&quot;edit&quot;:&quot;_blank&quot;,&quot;xml&quot;:&quot;&lt;mxfile modified=\&quot;2019-03-08T04:41:07.950Z\&quot; host=\&quot;www.draw.io\&quot; agent=\&quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36\&quot; etag=\&quot;8si5KNKgbAvTft3Qepxk\&quot; version=\&quot;10.3.5\&quot;&gt;&lt;diagram id=\&quot;-hgmOIA6eGsgZ6nlONSW\&quot; name=\&quot;Page-1\&quot;&gt;7Vttc5s4EP41/ugOSIDxx8Tu5e6mnbtp2l77UQZh64KRj5fYzq8/SYB5k2xMDHbbJDMJXsQidh89u6zWIzhb7x5CtFl9pC72R0BzdyM4HwEw1SbsLxfsU4GhwVSwDImbivRC8EhecCbUMmlCXBxVBsaU+jHZVIUODQLsxBUZCkO6rQ7zqF+96wYtcUPw6CC/Kf2HuPEqldpgUsh/x2S5yu+sW9P0zBrlg7MniVbIpduSCL4fwVlIaZwerXcz7HPb5XZJr/tNcfYwsRAHcZsLnibJy5/ftqsv8w/gm77//sl/GI+NVMsz8pPsgbPJxvvcAmzeG37o+Xh3xy06gvc4cLPDueOjKCIOE67itc8E
@magnusram05
magnusram05 / RaceConditionDemo.java
Last active October 28, 2019 02:17
Java: Multithreading - Part 2 - RaceCondition
package org.practice.java.multithreading;
import java.util.function.*;
public class RaceConditionDemo {
public static void main(String [] args){
while(true){
handleUserSession();
}
}
static void handleUserSession(){
@magnusram05
magnusram05 / RaceConditionDemo.java
Created March 3, 2019 10:08
Java: Multithreading - RaceCondition
package org.practice.java.multithreading;
import java.util.function.*;
public class RaceConditionDemo {
public static void main(String [] args){
while(true){
handleUserSession();
}
}
static void handleUserSession(){
@magnusram05
magnusram05 / VisibilityTest.java
Last active February 19, 2019 02:53
Java: Happens-Before relationship testing using synchronized block
package org.practice.java.multithreading;
public class VisibilityTest {
public static void main(String [] args){
CustomLock lock = new CustomLock();
Thread thread1 = new Thread1(lock);
Thread thread2 = new Thread2(lock);
thread1.start();
thread2.start();
@magnusram05
magnusram05 / OddEvenVolatileFlavor.java
Last active February 18, 2019 03:26
Java: Inter-Thread communication
/*
Custom lock with boolean evenProceed=false
Odd thread prints, sets evenProceed to true, notifies and waits
Even thread waits in loop checking for the condition evenProceed to be true before proceeding,
prints, notifies and waits
*/
package org.practice.java.multithreading;
public class OddEvenVolatileFlavor {
public static void main(String [] args){
@magnusram05
magnusram05 / OddEvenCountDownLatchFlavor.java
Last active February 4, 2019 03:37
Java: Inter-thread communication
package org.practice.java.multithreading;
import java.util.concurrent.*;
public class OddEvenCountDownLatchFlavor {
public static void main(String [] args){
Object lock = new Object();
CountDownLatch latch = new CountDownLatch(1);
Thread oddThread = new OddThread(lock, latch);
Thread evenThread = new EvenThread(lock, latch);