Spring does not manage the complete lifecycle of a prototype bean. We have to clean up prototype-scoped objects and release expensive resources that the prototype beans hold. With the help of BeanPostProcessor and DisposalBean interface we have to implmemt and write the clean up code. If someone need I can post the code here to show how to do that?
@@codeAtoZ In the last question I think he was trying highlight below scenerio- If we are working on with the collection! We should pay attention to not lose the data List data = new ArrayList(); Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8).parallelStream().map(i -> { data.add(i); // // AVOID STATEFUL LAMBDA EXPRESSIONS! return i; }).forEachOrdered(i -> System.out.print(i + " ")); // 1 2 3 4 5 6 7 8 // [1, 3, 4, 5, 2] ->missing ! System.out.println(); System.out.println(data); For an ArrayList object, the JVM internally manages a primitive array of the same type. As the size of the dynamic ArrayList grows, a new, larger primitive array is periodically required. If two threads both trigger the array to be resized at the same time, a result can be lost, producing the unexpected value shown here. Solution : we can make use concurrent stream like ConcurrentHashMap , CopyOnWriteArrayList example- Stream stream1 = Stream.of("Debjeet", "Roy", "Code With Roy").parallel(); ConcurrentMap map = stream1 .collect(Collectors.toConcurrentMap(String::length, k -> k, (s1, s2) -> s1 + "," + s2));
System.out.println(map); // {3=Roy, 7=Debjeet, 13=Code With Roy} System.out.println(map.getClass()); // java.util.concurrent.ConcurrentHashMap
@@codeAtoZ In the last question I think he was trying highlight below scenerio- If we are working on with the collection! We should pay attention to not lose the data List data = new ArrayList(); Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8).parallelStream().map(i -> { data.add(i); // // AVOID STATEFUL LAMBDA EXPRESSIONS! return i; }).forEachOrdered(i -> System.out.print(i + " ")); // 1 2 3 4 5 6 7 8 // [1, 3, 4, 5, 2] ->missing ! System.out.println(); System.out.println(data); For an ArrayList object, the JVM internally manages a primitive array of the same type. As the size of the dynamic ArrayList grows, a new, larger primitive array is periodically required. If two threads both trigger the array to be resized at the same time, a result can be lost, producing the unexpected value shown here. Solution : we can make use concurrent stream like ConcurrentHashMap , CopyOnWriteArrayList example- Stream stream1 = Stream.of("Debjeet", "Roy", "Code With Roy").parallel(); ConcurrentMap map = stream1 .collect(Collectors.toConcurrentMap(String::length, k -> k, (s1, s2) -> s1 + "," + s2));
System.out.println(map); // {3=Roy, 7=Debjeet, 13=Code With Roy} System.out.println(map.getClass()); // java.util.concurrent.ConcurrentHashMap
Yes I do at times... But I try to back it up with as much nearby answers as possible. If I don't know it completely then I tell clearly.. Sorry I am not aware of it.
Second round was good.
The design question about corona was good, it wil take some time to think in all perspective.
Yes... That question was very new for me
Spring does not manage the complete lifecycle of a prototype bean. We have to clean up prototype-scoped objects and release expensive resources that the prototype beans hold.
With the help of BeanPostProcessor and DisposalBean interface we have to implmemt and write the clean up code.
If someone need I can post the code here to show how to do that?
Yes sure... We all learn here from each other
@@codeAtoZ In the last question I think he was trying highlight below scenerio-
If we are working on with the collection! We should pay attention to not lose the data
List data = new ArrayList();
Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8).parallelStream().map(i -> {
data.add(i); // // AVOID STATEFUL LAMBDA EXPRESSIONS!
return i;
}).forEachOrdered(i -> System.out.print(i + " "));
// 1 2 3 4 5 6 7 8
// [1, 3, 4, 5, 2] ->missing !
System.out.println();
System.out.println(data);
For an ArrayList object, the JVM internally manages a primitive array
of the same type. As the size of the dynamic ArrayList grows, a new, larger primitive array is periodically required. If two threads both trigger the array to be resized at the same time, a result can be lost, producing the unexpected value shown here.
Solution :
we can make use concurrent stream like ConcurrentHashMap , CopyOnWriteArrayList
example-
Stream stream1 = Stream.of("Debjeet", "Roy", "Code With Roy").parallel();
ConcurrentMap map = stream1
.collect(Collectors.toConcurrentMap(String::length, k -> k, (s1, s2) -> s1 + "," + s2));
System.out.println(map); // {3=Roy, 7=Debjeet, 13=Code With Roy}
System.out.println(map.getClass()); // java.util.concurrent.ConcurrentHashMap
@@codeAtoZ In the last question I think he was trying highlight below scenerio-
If we are working on with the collection! We should pay attention to not lose the data
List data = new ArrayList();
Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8).parallelStream().map(i -> {
data.add(i); // // AVOID STATEFUL LAMBDA EXPRESSIONS!
return i;
}).forEachOrdered(i -> System.out.print(i + " "));
// 1 2 3 4 5 6 7 8
// [1, 3, 4, 5, 2] ->missing !
System.out.println();
System.out.println(data);
For an ArrayList object, the JVM internally manages a primitive array
of the same type. As the size of the dynamic ArrayList grows, a new, larger primitive array is periodically required. If two threads both trigger the array to be resized at the same time, a result can be lost, producing the unexpected value shown here.
Solution :
we can make use concurrent stream like ConcurrentHashMap , CopyOnWriteArrayList
example-
Stream stream1 = Stream.of("Debjeet", "Roy", "Code With Roy").parallel();
ConcurrentMap map = stream1
.collect(Collectors.toConcurrentMap(String::length, k -> k, (s1, s2) -> s1 + "," + s2));
System.out.println(map); // {3=Roy, 7=Debjeet, 13=Code With Roy}
System.out.println(map.getClass()); // java.util.concurrent.ConcurrentHashMap
@@codeAtoZ Here is the code
public class PrototypeBeanPostProcessor implements BeanPostProcessor, DisposableBean, ApplicationContextAware {
private ApplicationContext context;
List prototypeBeans = new LinkedList();
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (context.isPrototype(beanName)) {
synchronized (prototypeBeans) {
prototypeBeans.add(bean);
}
}
return bean;
}
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.context = applicationContext;
}
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
public void destroy() throws Exception {
synchronized (prototypeBeans) {
for (Object bean : prototypeBeans) {
if (bean instanceof DisposableBean) {
DisposableBean disposable = (DisposableBean)bean;
try {
disposable.destroy();
} catch (Exception e) {
e.printStackTrace();
}
}
}
prototypeBeans.clear();
}
}
}
that corona question blew my mind
Yes.. Me too
You don't get nervous before/ during interviews?... have you ever got blank during any interview?
Yes I do at times... But I try to back it up with as much nearby answers as possible. If I don't know it completely then I tell clearly.. Sorry I am not aware of it.
Did you get any offer from Morgan Stanley ? What was the position you were interviewed for ?
@@MithuMuni what do you mean ?
Yes
Bad audio quality
Hi, was this the final round and have u got the offer?
Yes
Approx how much are they offering?
In the range of 25 - 31
Did you get selected
Yes
Here is the code
public class PrototypeBeanPostProcessor implements BeanPostProcessor, DisposableBean, ApplicationContextAware {
private ApplicationContext context;
List prototypeBeans = new LinkedList();
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (context.isPrototype(beanName)) {
synchronized (prototypeBeans) {
prototypeBeans.add(bean);
}
}
return bean;
}
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.context = applicationContext;
}
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
public void destroy() throws Exception {
synchronized (prototypeBeans) {
for (Object bean : prototypeBeans) {
if (bean instanceof DisposableBean) {
DisposableBean disposable = (DisposableBean)bean;
try {
disposable.destroy();
} catch (Exception e) {
e.printStackTrace();
}
}
}
prototypeBeans.clear();
}
}
}
What was asked in Round 1 ?
I have already uploaded video... See in playlist