Category: Java
-
Ch.04 – Balking Pattern
Unlike Guarded Suspension pattern that waits until guard condition meets, Balking pattern returns from executing without waiting. Balking Pattern Example Guarded Time Example – wait(n) Balking after waiting for some specific amount of time is called as guarded timed or timeout. In Java, as there is no way to distinguish…
-
Ch. 03 – Guarded Suspension
Guarded Suspension Format Add synchronized keyword on the method Inside the while condition, add the guard condition which needs to be guarded Add NOT(!) operator to the guard condition Add wait(); inside of the while statement Add notifyAll(); when the guardCondition meets with instance state change wait() method wait()and notifyAll()…
-
Ch. 02 – Immutable Pattern
Immutable Immutable class’s state doesn’t change after initialization. Class is set as final Field variables are set as final and private Cannot update the field variable’s value No setter methods No methods can update the values Getter method does not return the field variable reference If getter method returns a…
-
Ch.01 – Single Threaded Execution
Synchronized synchronized method ensures only one thread can execute the method at a time. All of the methods which access shared resources(critical section) should have a synchronized keyword to guard the state of shared resource. synchronized methods are slower It takes time to acquire a lock of the object Waits…
-
Ch.08 – Worker Thread pattern
Worker threads are sometimes called as background thread, who keeps executing jobs pulled from job queue. Example of Worker Thread pattern Take note on what resource needs to be protected requestQueue is the one that needs to be protected Add synchronized keyword on the method that can access the resource…
-
[Java Application Performance and Memory Management] Chapter 21 – Other Coding Choices
Primitive type vs Reference type Primitives are stored on stack while the other is on heap. Primitive types are mostly faster https://www.baeldung.com/java-primitives-vs-objects BigDecimal vs Double BigDecimal requires more precision which consumes more resources StringBuilder vs String literal StringBuilder always shows better performance If the String literals concatenation is in one…
-
[Java Application Performance and Memory Management] Chapter 20 – How Lists Work
ArrayList ArrayList is internally created as an Array As seen from the above screenshot, it is saved in this.elementData array Initially, the size of the ArrayList is 0 and the elementData length is 0 as well. However when the first element is added, the size of the ArrayList is 1…
-
[Java Application Performance and Memory Management] Chapter 15 – Garbage Collection Tuning
Monitoring/Tuning Garbage Collections Flags -verbose:gc Prints the Garbage Collector activities such as whether they performed Minor GC, Full GC, etc. -XX:-UseAdaptiveSizePolicy Young Generation space like Eden, S0 and S1 area is dynamically being resized by the virtual machine in runtime. This flag is to disable that feature. -XX:NewRatio=n The ratio…
-
[Java Application Performance and Memory Management] Chapter 14 – Garbage Collection
Mark and Sweep During the “Mark” process, the garbage collector looks through the memory space and mark the ones that are being referenced by either stack variables or static variables. During this “Mark” process, the application itself is being paused, which is called “Stop the world”. If there’s any other…
-
[Java Application Performance and Memory Management] Chapter 12, 13 – Monitoring the Heap, Analysing a heap dump
JVisualVM Under the installed jdk directory(e.g. /Library/Java/JavaVirtualMachines/jdk1.8.0_271.jdk/Contents/Home/bin) , there is an execution file called jvisualvm which should look like above screenshot when it’s opened. From the left panel of the applications list, you can select the java application running on your local and see the heap usage of that application…
