Posts

Showing posts from June, 2017

Executor and Executor Service Framework

In Java , a task is a unit of work. To accomplish the work done we need to create a new Thread every time.  There is a performance overhead associated with starting a new thread, and each thread is also allocated some memory for its stack etc. In low level Java threading , we have to follow  task execution policy , in which the thread to which task has been submitted will only execute the task. How does Executor Framework come over the task execution policy and improve performance ? Executor framework provides a way to decouple the task execution policy stages from submition and execution . Instead of starting a new thread for every task to execute concurrently, the task can be passed to a thread pool . As soon as the pool has any idle threads the task is assigned to one of them and executed. Internally the tasks are inserted into a Blocking Queue which the threads in the pool are dequeuing from. When a new task is inserted into the queue one of the idle threads will deque

Different ways of getting a new object in Java

There are different ways of creating a new object in java . Few of them are explained as below : 1) new operator 2) reflection 3) deserialization 4) cloning Using new operator : It is the most common and regular way to create an object and a very simple one also. By using this method we can call whichever constructor we want to call (no-arg constructor as well as parameterized). MyClass mc = new MyClass(); Using Reflection:  We can also use the newInstance() method of a Class class to create an object. This newInstance() method calls the no-arg constructor to create the object. using Class.forName(“<fully-qualified-class-name> ”) Class c = Class.forName("com.codingfewer.MyClass"); c.newInstance(); Here code will compile without any issues, ie, with/without presence of MyClass.java in the classpath. But at runtime if the class is not found then it will throw run time exception. For this reason we are forced to handle ClassNotFoundException, it is a

Hashmap internal working

Image
Hashmap works on the principle of Hashing. Hashing works on the three terms : Hash Function, Hash value and Bucket. Now we will go through each of them one by one. Hash Function  : Hash Function in hashing is the function which converts an Object into integer form. In Java hashcode() method does this work. Hash Value : Hash Value in java is the integer value returned by the Hash Function by converting memory address into Integer to find the index in bucket. Bucket : Bucket is used to store key value pairs.  Bucket can store multiple key value pair. In hashmap bucket use linked list to store objects. As we have understand the Hash Function , Hash Value and Bucket concepts in Hashing. Now we will see what is hashCode() method in Java API and How does it gives integer value of memory address. hashCode() hashCode() method is used to get the hash Code of an object. hashCode() method of object class returns the memory reference of object in integer form. Definition of hashCode

Immutable Class

Immutable objects are instances whose state doesn’t change after it has been initialized. For example, String is an immutable class and once instantiated its value never changes.Everytime we try to append() or concatenate() something with String it creates a new String but it does not change the same String. Benefits of Immutable Class 1) Immutable class is good for caching purpose because you don’t need to worry about the value changes. 2) Other benefit of immutable class is that it is inherently thread-safe, so you don’t need to worry about thread safety in case of multi-threaded environment. How to make immutable class Declare the class as final so it can’t be extended. Make all fields private so that direct access is not allowed. Don’t provide setter methods for variables Make all mutable fields final so that it’s value can be assigned only once. Initialize all the fields via a constructor performing deep copy. Perform cloning of objects in the getter methods to ret

Deep Cloning and Shallow Cloning

Image
What is Cloning  ? Cloning is a process of creating an exact copy of an existing object in the memory. In java, clone() method of java.lang.Object class is used for cloning process. This method creates an exact copy of an object on which it is called through field-by-field assignment and returns the reference of that object. Not all the objects in java are eligible for cloning process. The objects which implement Cloneable interface are only eligible for cloning process. Cloneable interface is a marker interface which is used to provide the marker to cloning process. Both shallow copy and deep copy are related to this cloning process. The default version of clone() method creates the shallow copy of an object. To create the deep copy of an object, you have to override the clone() method . Let’s see how these shallow copy and deep copy work. Shallow Cloning The default version of clone() method creates the shallow copy of an object. The shallow copy of an object will have e

Factory Design Pattern

Image
Factory design pattern is a creational design pattern. In Factory design pattern we create a factory method which gives us object on the basis of the input we provide. Objects are being created without exposing the creation logic of instance from client. Factory design pattern is mainly used when we have one super class and multiple sub class and on the basis of the input we have to return one of the sub class . Super class in design pattern can be an abstract class, interface and normal class. In below diagram, we have interface as Computer, we have implemented it as "PC" in which we need normal RAM, CPU and Hard Disk and other we have implemented it as "Server" in which we have High RAM, High CPU and very large memory Hard Disk. Client class will ask to factory method to give the instance of one of them and   factory method will return the instance of PC or Server on the basis of input. Implementation : Computer.java PC.java : Se

System Design Concepts ( Part 2 : Load Balancer )

Image
Load balancing refers to efficiently distributing incoming network traffic across a group of backend servers, also known as a server farm or server pool.Load balancing improves responsiveness and increases availability of applications. A load balancer is a device that distributes network or application traffic across a cluster of servers.  What does load balancer exactly do ? A load balancer sits between the client and the server farm accepting incoming network and application traffic and distributing the traffic across multiple backend servers using various methods. By balancing application requests across multiple servers, a load balancer reduces individual server load and prevents any one application server from becoming a single point of failure, thus improving overall application availability and responsiveness. Why do we need Load Balancer ? Traffic volumes are increasing and applications are becoming more complex. Load balancers provide the bedrock for

System Design Concepts ( Part 1 : Horizontal Scaling vs Vertical Scaling )

Image
Horizontal scaling means that you scale by adding more machines into your pool of resources whereas Vertical scaling means that you scale by adding more power (CPU, RAM) to an existing machine. With horizontal-scaling it is often easier to scale dynamically by adding more machines into the existing pool - Vertical-scaling is often limited to the capacity of a single machine, scaling beyond that capacity often involves downtime and comes with an upper limit. Lets understand the above concepts with an example : Suppose if you have a normal bike and you are purchasing new sports bike because you want more horsepower that is vertical scaling because you have upgraded current bike with more better features. . And if you have one bike and you purchased few more similar kind of bikes then that is horizontal scaling because you have added more similar kind of vehicles to you garage. Advantage of vertical scaling is that it sports bike give more horsepower but it consumes extra petrol

Spring Boot and Docker

Docker allows you to package an application with its dependencies, into a light weight, portable container that can run on almost any environment.  You can think of a Docker container as a run time, a mini virtual machine that encapsulates your application and its dependencies. In order to run a container you need a Docker image. An image is like a template that defines everything that will exist within the container. You can almost think of an container as a run time instance of the image it was created from. In this post we'll define and build 3 slightly different Docker images that run a simple Java app.