Friday, 2 August 2013

Java/Core Java Interview question: - What are native threads, working thread and green thread?

Native Threads: -
  • Native threads use the operating system's native ability to manage multi-threaded processes - in particular, they use the pthread library
  • The kernel schedules and manages the various threads that make up the process

Working Thread: - 
  • In working thread scheme, allocates one thread to execute one task.
  • When the task is complete, the thread may return to a thread pool for later use. In this scheme a thread may execute arbitrary tasks, which are passed in the form of a Runnable method argument, typically execute()
  • The runnable tasks are usually stored in a queue until a thread host is available to run them.
  • The worker thread design pattern is usually used to handle many concurrent tasks where it is not important which finishes first and no single task needs to be coordinated with another. The task queue controls how many threads run concurrently to improve the overall performance of the system. However, a worker thread framework requires relatively complex programming to set up, so should not be used where simpler threading techniques can achieve similar results.

Green Thread: - 
  • A green thread refers to a mode of operation for the Java Virtual Machine (JVM) in which all code is executed in a single operating system thread. If the Java program has any concurrent threads, the JVM manages multi-threading internally rather than using other operating system threads
  • There is a significant processing overhead for the JVM to keep track of thread states and swap between them, so green thread mode has been deprecated and removed from more recent Java implementations. Current JVM implementations make more efficient use of native operating system threads

Also see the following Java video on Google Window Toolkit (GWT).

Wednesday, 24 April 2013

Java interview questions and answers: - We have return in the try block, will finally block execute?

Let us first try to understand this Java interview question. You can see below is a simple Java code which where in we have put a return statement in the try block. So the question is will finally block execute.

public static void main(String[] args)
                  int i=0;
                  // will the control come here
                  int z=0;

The answer is Yes Finally block will execute irrespective there is return or there are no return statements. That’s why finally block is the best place to put clean up code.

Friday, 14 September 2012

Java Training: -What do you mean by Bootstrap, Extension and System Class loader?

There three types of class loaders:-
  • Bootstrap Class loader also called as primordial class loader.
  • Extension Class loader.
  • System Class loader.
Let’s now try to get the fundamentals of these class loaders.

Bootstrap Class loader

Bootstrap class loader loads those classes those which are essential for JVM to function properly. Bootstrap class loader is responsible for loading all core java classes for instance java.lang.*,* etc. Bootstrap class loader finds these necessary classes from “jdk/jre/lib/rt.jar”. Bootstrap class loader cannot be instantiated from JAVA code and is implemented natively inside JVM.

Extension Class loader

The extension class loader also termed as the standard extensions class loader is a child of the bootstrap class loader. Its primary responsibility is to load classes from the extension directories, normally located the “jre/lib/ext” directory. This provides the ability to simply drop in new extensions, such as various security extensions, without requiring modification to the user's class path.

System Class loader

The system class loader also termed application class loader is the class loader responsible for loading code from the path specified by the CLASSPATH environment variable. It is also used to load an application’s entry point class that is the "static void main ()" method in a class.

See the following video on overview and working of Servlets in Java: -

Click to get Java Training


Get more Java Training from author’s blog

Saturday, 8 September 2012

Java Training: - Mention the concept of local interfaces?

One of the biggest issues of creating objects using home interface is performance. Below are the steps which follow when you call the EJB object:-
  • JAVA client calls the local stub.
  • Stub marshal the values in to some other form which the network understands and sends it to the skeleton.
  • Skeleton then de-marshals it back to a form which is suitable for JAVA.
  • Skeleton then calls the EJB object and methods.
  • EJB object then does object creation, connection pooling, transaction etc.
  • Once EJB object calls the bean and the bean completes its functionalities. All the above steps must again be repeated to reach back to the JAVA client.
So you can easily guess from the above step that its lot of work. But this has been improved in EJB 2.0 using Local objects. Local objects implement local interface rather than using remote interface. Just to have a comparison below are the steps how the local object works.
  • JAVA client calls the local object.
  • Local object does connection pooling, transactions and security.
  • It then passes calls the bean and when bean completes its work it returns the data to the Local object who then passes the same to the end client.
See the following video on Introduction to EJB in Java: -

Click to get Java Training


Get more Java Training from author’s blog

Wednesday, 5 September 2012

Java Training: - Show different types of resultset?

“ResultSet” is an object that contains the results of SQL query. That means it contains the rows that satisfy the conditions of the query.

There are three basic types of resultsets:-

Forward-only results set

These types of resultset are non-scrollable and moves only forward.

Scroll-insensitive set

Resultset is scrollable so the cursor can move forward, backward, to a particular row etc.
While the resultset is open it does not show any changes done to the underlying database.

Scroll-sensitive set
Resultset is scrollable so the cursor can move forward, backward, to a particular row etc.
Resultset is sensitive to changes when it is open or used. That means if any values are modified in the database it’s propagated to the resultset.

See the following video on Factory Pattern in Java: -

Click to get Java Training


Get more Java Training from author’s blog

Monday, 3 September 2012

Java Training: - Explain “ResultSet”, “RowSet”, “CachedRowset”, “JdbcRowset” and “WebRowSet” relation ship?

Below are the major points of difference:-
  • “ResultSet” is a connected architecture while “RowSet” is a disconnected architecture.
  • As “ResultSet” is a connected architecture we cannot serialize the object while “RowSet” can be serialized.
  • “RowSet” object is a Java bean while “ResultSet” is not. In the below diagram you can see “RowSet” interface also derives from “BaseRowSet” interface. “BaseRowSet” interface has all the ingredients to make it a Java bean.
Below diagram shows the complete relationship between all the interface and classes.

Figure: - Interface diagram for “Resultset” and “RowSet”
As "RowSet" is a disconnected from database it need to maintain Meta data for columns. "RowSet" can also provide scrollable resultsets or updatable resultsets even if the JDBC driver is not supporting the same. Java client can get a “RowSet” manipulate the same and finally send all the results to the database at one go. Sun has provided three implementation of “RowSet” as below:-

CachedRowSet: - Disconnected rowset always keeps the data in memory. It is scrollable and can also be serialized. It’s an ideal choice where we want to pass data between tiers or to do batch updates. "CachedRowSet" are disconnected so can be used for huge number of updates. Get the "CachedRowSet" object manipulate all your data and send the whole bunch of manipulated data in on go to the Database.

JDBCRowSet: - It’s opposite to "CachedRowSet". It maintains a connection to the database while the client has reference to it. So it’s a connected architecture as compared to CachedRowSet.

WebRowSet :- "WebRowSet" is a extension of "CachedRowSet". But the added feature is it can produce XML presentation of the data cached. If you are thinking of exposing your data through web services or to provide data to thin client which are written in different language other than JAVA. Best bet if you want to pass data in XML format over HTTP protocol.

See the following video on FlyWeight Pattern in Java: -

Click to get Java Training


Get more Java Training from author’s blog