This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="mxgraph" style="max-width:100%;border:1px solid transparent;" data-mxgraph="{"highlight":"#0000ff","nav":true,"resize":true,"toolbar":"zoom layers lightbox","edit":"_blank","xml":"<mxfile modified=\"2019-03-08T04:41:07.950Z\" host=\"www.draw.io\" agent=\"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36\" etag=\"8si5KNKgbAvTft3Qepxk\" version=\"10.3.5\"><diagram id=\"-hgmOIA6eGsgZ6nlONSW\" name=\"Page-1\">7Vttc5s4EP41/ugOSIDxx8Tu5e6mnbtp2l77UQZh64KRj5fYzq8/SYB5k2xMDHbbJDMJXsQidh89u6zWIzhb7x5CtFl9pC72R0BzdyM4HwEw1SbsLxfsU4GhwVSwDImbivRC8EhecCbUMmlCXBxVBsaU+jHZVIUODQLsxBUZCkO6rQ7zqF+96wYtcUPw6CC/Kf2HuPEqldpgUsh/x2S5yu+sW9P0zBrlg7MniVbIpduSCL4fwVlIaZwerXcz7HPb5XZJr/tNcfYwsRAHcZsLnibJy5/ftqsv8w/gm77//sl/GI+NVMsz8pPsgbPJxvvcAmzeG37o+Xh3xy06gvc4cLPDueOjKCIOE67itc8E |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.practice.java.multithreading; | |
import java.util.function.*; | |
public class RaceConditionDemo { | |
public static void main(String [] args){ | |
while(true){ | |
handleUserSession(); | |
} | |
} | |
static void handleUserSession(){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.practice.java.multithreading; | |
import java.util.function.*; | |
public class RaceConditionDemo { | |
public static void main(String [] args){ | |
while(true){ | |
handleUserSession(); | |
} | |
} | |
static void handleUserSession(){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |