JavaSpartans
Java J2ee Struts Hibernate Spring Interview Questions Java J2ee Struts Hibernate Spring Interview Questions
1 2 3    Next

1. What is synchronization

Synchronization is a technique to control the access of multiple threads to shared resources. Synchronization stops multithreading. With synchronization , at a time only one thread will be able to access a shared resource.
Synchronization is a process of controlling the access of shared resources by the multiple threads. In non synchronized multithreaded application, it is possible for one thread to modify a shared object while another thread is in the process of using or updating the object's value. The best example to learn this concept is to understand Producer Consumer problem.


2. How can we tell that Hashtable is Synchronized ?

Open the documentation of Hashtable and check the keyword ?synchronized? for its methods.


3. How do you implement synchronization ?

Synchronization is a process of restricting concurrent access to the shared resources. As synchronization is a performance issue, we should be very much careful fo decide at which part of the code the problem may occur. At that place provide a synchronized block or simply provide a synchronized method. just use the key word synchronized before the method or block. Please note that we can synchronize only blocks or methods. we can not synchronize variables or classes. By creating locks automatically by using wait(), notify() and notifyAll() methods  the Java Virtual machine taking care of inner implementation.


4. What is synchronization and why is it important?

With respect to multithreading, synchronization is the capability to control the access of multiple threads to shared resources. Without synchronization, it is possible for one thread to modify a shared object while another thread is in the process of using or updating that object?s value. This often leads to significant errors.


5. What is the default size of Vector, ArrayList, Hashtable, HashMap, and HashSet in Java?

Vector, ArrayList are all growable array of objects so the size is not specific but depends upon the heap area of JVM. Vector has a default size of 0. A vector has its default capacity which is 10 elements, here size is different from capacity, after 10 element if we enter one element the capacity of vector changes to 20 were as size is 11 only, for example if you have entered 21 elements in a vector, then if you print v.size it results 21 but v.capacity it results 30.
Vector default size - 0;           default capacity 10,
ArrayList default size - 0; default capacity 10, 
Hashtable - default size 0;  Hashmap - 0, HashSet ? 0;
The default initial capacity : for Hashtable 11, for HashMap 11, for HashSet 16.

Consider the output for Vector :
Vector v = new Vector();
System.out.println("vsize"+v.size());  0
System.out.println("vcapacity"+v.capacity()); 10
v.add("Hello");
System.out.println("vsize"+v.size()); 1
System.out.println("vcapacity"+v.capacity()); 10
v.add("Hello1");v.add("Hello2");v.add("Hello3");v.add("Hello4");v.add("Hello5");
v.add("Hello6");v.add("Hello7");v.add("Hello8");v.add("Hello9");v.add("Hello10");
System.out.println("vsize"+v.size()); 11
System.out.println("vcapacity"+v.capacity()); 20


6. Difference between Hashtable and HashMap

Hashtable does not store null value, while HashMap does Hashtable is synchronized, while HashMap is not


7. Can you tell me the code for implementing the connection pooling in WebLogic Server ?

If we want a database connection from the pool, we need to create it from the DataSource not from DriverManager.
So DriverManager.getConnection() method will give you normal connection Where as DataSource.getConnection() method will give you a database connection from the connection pool.

To get a DataSource reference we need a JNDI name, from that name we can get InitialContext reference.
The following is the Standard code to get a connection from DataSource. For more information refer to WebLogic Docs.

The JNDI name will be there in the Directory Service. The Initial Context Object will get the information from the Directory Service.

InitalContext ic = new InitialContext();
Hashtable ht = new Hashtable();
ht.put( Context.INITIAL_CONTEXT_FACTORY,weblogic.jndi.WLInitialContextFactory);
//You have to set weblogic properties first and the JNDI name that you are defining in Weblogic while creating the connection pool
//here t3 means TENGA Protocol
ht.put( Context.PROVIDER_URL t3://localhost:7001);
ht.put( Context.SECURITY_PRINCIPAL username);
ht.put( Context.SECURITY_CREDENTIALS password_of_ weblogic);

Whenever you need a connection in your Java Class you can use the following lines of code.

DataSource ds = (DataSource)ic.lookup("jndiname");
Connection con = ds.getConnection();
Statement stmt = con.createStatement();
//from here you can play normally


8. Will hashtable allow null keys ?

No


9. Synchronization will be done on what ?

We can synchronize only methods and blocks using the keyword 'synchronized'.
like
synchronized method m1(){
}
or
synchronized{
}


10. What is the main disadvantage of threads ?

The main disadvantage of thread is that we need to synchronize the object while using the thread-deadlocks, data races, starvation are the main issues we need to consider-performance will be low because of synchronization.


Java J2ee Struts Hibernate Spring Interview Questions
1 2 3    Next
Menu - javaspartans.com - Java J2ee Struts Spring Hibernate Interview Questions
Hostens.com - A home for your website
Ads By Google