Saturday, 9 July 2016

What does persistence object means in Hibernate architecture?

Persistent object is nothing but an instance of POJO class that we create to represent rows in the table in the database.

According to hibernate-doc an instance of POJO class representing table in database goes through 3 states.

Transient
Persistent
Detached

When a POJO instance is in session scope, it is said to be persistent i.e hibernate detects any changes made to that object and synchronizes it with database when we close or flush the session.

Friday, 8 July 2016

What is database deadlock? How can we avoid them?

Database deadlock
When multiple external resources are trying to access the DB locks and runs into cyclic wait, it may make the DB unresponsive.

Avoid Database deadlock
1. Can make a queue wherein we can verify and order the request to DB (To run the operations in FIFO order).
2. Less use of cursors as they lock the tables for long time (slow down the DML executions).
3. Keeping the transaction smaller (process query relation operation before start the transactions).

What algorithm is used in modern day elevators?

Knuth's Elevator algorithm
The elevator algorithm, a simple algorithm by which a single elevator can decide where to stop, is summarised as follows:

1. Continue travelling in the same direction while there are remaining requests in that same direction.
2. If there are no further requests in that direction, then stop and become idle, or change direction if there are requests in the opposite direction.

The elevator algorithm has found an application in computer operating systems as an algorithm for scheduling hard disk requests.

Modern elevators use more complex heuristic algorithms to decide which request to service next.

Java Virtual Machine (JVM) architecture

Java Virtual Machine (JVM) architecture
Classloader:
JVM's class loader sub system performs 3 tasks
1. It loads .class file into memory.
2. It verifies byte code instructions.
3. It allots memory required for the program.
           
Run time data area:
This is the memory resource used by JVM and it is divided into 5 parts
1. Method area: Method area stores class code and method code such as runtime constant pool, field and method data, the code for methods.

2. Heap: Objects are created on heap.

3. Java stacks: Java Stack stores frames. It holds local variables and partial results, and plays a part in method invocation and return. Each thread has a private JVM stack, created at the same time as thread. A new frame is created each time a method is invoked. A frame is destroyed when its method invocation completes.

4. Program counter registers: It contains the address of the Java virtual machine instruction currently being executed by microprocessor.

5. Native method stacks: The native method stacks are places where native methods (for example, C language programs) are executed.

Execution engine:
The byte code that is assigned to the runtime data areas in the JVM via class loader is executed by the execution engine. The execution engine reads the Java Byte code in the unit of instruction. It is like a CPU executing the machine command one by one.

Execution engine contains interpreter and JIT compiler, which covert byte code into machine code. JVM uses optimization technique to decide which part to be interpreted and which part to be used with JIT compiler. The HotSpot represents the block of code executed by JIT compiler.

Native method interface:
Native method interface is a program that connects native methods libraries (C header files) with JVM for executing native methods.

Native method library:
It holds the native libraries information.

Why is main() in java void?

Java was designed from the beginning to be a multithreaded environment.

Reason of void return of main method:

1.The reason for the main method having void as return type is that once main finishes, it doesn't necessarily mean that the entire program finished. If main spawns new threads, then these threads can keep program running. The return type of main doesn't make much sense at this point.

Example:
This is very common in Swing applications, where the main method typically starts a GUI on the Swing thread, and then main finishes however the program is still running.

2.The program may exit before or after the main method finishes; a return value from main would therefore be meaningless. If you want the program to return a status code, call one of the following methods (note that all three methods never return normally):

System.exit(int status) - Equivalent to Runtime.getRuntime().exit(status).

Runtime.exit(int status) - Terminates the currently running JVM by initiating its shutdown sequence (run all registered shutdown hooks, and uninvoked finalizers, if necessary). Once this is done the JVM halts.

Runtime.halt(int status) - Forcibly terminates the currently running JVM.

Why C and C++ allowed int return type? 
C and C++ were both designed as languages before multithreading was a widely known technique, and both had threads grafted onto them at a later date (main can return int in C and C++).


Parallelism vs Concurrency

Parallelism
Parallelism means that an application splits its tasks up into smaller subtasks which can be processed in parallel, for instance on multiple CPUs at the exact same time.



Example:If a person is listening to music while writing an assignment then work done is in parallel way.

Concurrency
A condition that exists when at least two threads are making progress. A more generalized form of parallelism that can include time-slicing as a form of virtual parallelism.

If the computer only has one CPU the application may not make progress on more than one task at exactly the same time, but more than one task is being processed at a time inside the application. It does not completely finish one task before it begins the next.




Example:If a person is watching TV while writing an assignment then work done is in concurrent way.

Pizza Factory design in Java

Design patterns used:

1.     Abstract Factory,
2.     Template method pattern (to define the preparation sequence).

VeggiePizza.java
package com.pizza.factory;
public class VeggiePizza extends Pizza {

     public VeggiePizza() {
           name = "Veggie Pizza";
           dough = "Crust";
           sauce = "Marinara sauce";
           toppings.add("Shredded mozzarella");
           toppings.add("Grated parmesan");
           toppings.add("Diced onion");
           toppings.add("Sliced mushrooms");
           toppings.add("Sliced red pepper");
           toppings.add("Sliced black olives");
     }

     @Override
     public voidprepare() {
           System.out.println("Add Veggie !!");

     }

     @Override
     public voidbake() {
           System.out.println("Bake Veggie Pizza !!");

     }

     @Override
     public voidcut() {
           System.out.println("Cut Veggie Pizza !!");

     }

     @Override
     public voidpack() {
           System.out.println("Pack Veggie Pizza !!");

     }
}

CheesePizza.java
package com.pizza.factory;
public class CheesePizza extends Pizza {
    
     public CheesePizza() {
           name = "Cheese Pizza";
           dough = "Regular Crust";
           sauce = "Marinara Pizza Sauce";
           toppings.add("Fresh Mozzarella");
           toppings.add("Parmesan");
     }

     @Override
     public voidprepare() {
           System.out.println("Add Cheese !!");
          
     }

     @Override
     public voidbake() {
           System.out.println("Bake Cheese Pizza !!");
          
     }

     @Override
     public voidcut() {
           System.out.println("Cut Cheese Pizza !!");
          
     }

     @Override
     public voidpack() {
           System.out.println("Pack Cheese Pizza !!");
     }
}

Pizza.java
package com.pizza.factory;
import java.util.ArrayList;

public abstract class Pizza {
     
      String name;
      String dough;
      String sauce;
      ArrayList<String> toppings = new ArrayList<String>();
     
     
    /**
     * Template method Pattern - Sequence of processes.
     * To define the sequence to prepare Pizza.
     */
    public finalvoidtemplatePizzaPrepare() {
     
      /*Step#1. prepare Pizza integrants */
      prepare();
     
      /*Step#2. bake the Pizza. */
      bake();
     
      /*Step#3. cut the slice of Pizza. */
      cut();
     
      /*Step#4. pack the Pizza. */
      pack();
    }

      public abstract void prepare();
     
      public abstract void bake();
     
      public abstract void cut();
     
      public abstract void pack();
}

SimplePizzaFactory.java
package com.pizza.factory;
public class SimplePizzaFactory {
    
     public static Pizza orderPizza(String type) {
          
           Pizza pizza = null;
          
           if("cheese".equals(type)) {
                pizza = new CheesePizza();
           } else if("veggie".equals(type)) {
                pizza = new VeggiePizza();
           }
          
           pizza.templatePizzaPrepare();
           return pizza;
     }
}

PizzaStore.java

package com.pizza.factory;
public class PizzaStore {

     public static void main(String[] args) {

           Pizza pizza  = SimplePizzaFactory.orderPizza("cheese");
          
           System.out.println("\nPizza ready : "+ pizza.name);
          
           /*pizza  = SimplePizzaFactory.createPizza("veggie");
           System.out.println("Pizza type : "+ pizza);*/
     }
}
Related Posts Plugin for WordPress, Blogger...