What is the difference between StringBuilder and StringBuffer classes.

Both the StringBuffer and StringBuilder classes are used to create mutable strings. But StringBuffer are synchronized, which means that only one task is allowed to execute the methods. Consider the following program which appends a “-” mark to a StringBuilder object by 100 threads. import java.util.concurrent.Executors; import java.util.concurrent.ThreadPoolExecutor; public class MutableStringDemo{ static StringBuilder stringBuilder =…… Continue reading What is the difference between StringBuilder and StringBuffer classes.

Java Thread synchronization

To enhance the speed of a program, we use multiple threads to access the same resource at the same time. But, this kind of shared resources can produce unexpected results due to this simultaneous access made by multiple threads. The following example demonstrates the problem. Here, we have a sample bank system that contains 4…… Continue reading Java Thread synchronization