Posts

Showing posts with the label interview

Your own Thread Pool

"Create your own thread pool" - A typical interview question, asked to check if you have a basic understanding of Thread Pool, Runnable, and the disadvantages of creating a new Thread for each Task. ThreadPool is made up of a number of threads, identified by the core thread pool size. Once you start the ThreadPool, all the threads start running, waiting for the task. Each task or Runnable that you submit to the ThreadPool is executed by the existing threads of the ThreadPool. Creating a thread is expensive, so it is advisable to have a ThreadPool which manages the tasks. Here is a program which creates its own ThreadPool - MyThreadPool. import java.util.concurrent.ArrayBlockingQueue ; import java.util.concurrent.BlockingQueue ; class MyThreadPool { private int corePoolSize ; private MyThread tPool []; private BlockingQueue < Runnable > queue = new ArrayBlockingQueue <>(1024); public MyThreadPool ( int corePoolSize ) { thi...

Different ways to create an Object in Java

In this post, we will discuss the different ways to create a new Object Consider we have a class, say CustomClass and we are going to see different ways in which we can create a new object of CustomClass. Using new keyword/the constructor: new CustomClass (); Clone You already have an object and you using Cloning to create a copy of that object CustomClass myObject = new CustomClass (); CustomClassobject = ( CustomClass ) myObject . clone (); Class.forName: The reflection API gives us the ability to create objects without calling the constructor. Spring and Hibernate use reflection to create objects. Class . forname ( CustomClass ); Deserialization is another way of creating the object. First you serialize and then deserialize to get the object.

Revise Servlets in 10 mins

Revising Servlets in 10 mins, useful only if you have already read on servlets and just need to some guide points to recollect. Servlet is used to create dynamic web applications. Resides at server side. Implement Servlet interface to make a Servlet. Or you can extend the HTTPServlet class. Capable to serving and responding to requests. Better performance (creates new thread for each request), robust, secure and portable(since in java) Uses the HTTP Protocol to transfer data between the web server and the web browser. Stateless by default. HTTP Request methods: GET POST HEAD PUT DELETE OPTIONS TRACE Container provides runtime environment for j2ee applications Life cycle management Multithreading support object pooling Security Content Types: text/html, text/plain, images/jpeg etc Server types: Web server and application server Servlet Life Cycle: Servlet class is loaded Servlet instance is created  init method is invoked service method is invoked, ev...