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 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.

3. What are different ways of achieving multi-threading?

There are two ways of achieving multi-threading : By implementing Runnable interface. By extending Thread class.

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. Synchronization will be done on what ?

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

6. 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.

7. At what situation you will go to implement your application as threads ?

In all situations try to utilize multithreading.
if you have single core server you will not get multithreading advantage.
If you have 4 core / 8 core processors then,
Identify all the sequential and concurrent computations.
based on number of cores, create those many threads and run.

mostly in heavy batch processing implement multi-threading.

8. What is a green thread ?

Native threads can switch between threads preemptively, switching control from a running thread to a non-running thread at any time. Green threads only switch when control is explicitly given up by a thread (Thread.yield(), Object.wait(), etc.) or a thread performs a blocking operation (read(), etc.). On multi-CPU machines, native threads can run more than one thread simultaneously by assigning different threads to different CPUs. Green threads run on only one CPU. Native threads create the appearance that many Java processes are running: each thread takes up its own entry in the process table. One clue that these are all threads of the same process is that the memory size is identical for all the threads - they are all using the same memory. Unfortunately, this behavior limits the scalability of Java on Linux. The process table is not infinitely large, and processes can only create a limited number of threads before running out of system resources or hitting configured limits.

9. What are the differences between sleep() and wait() methods in multi threading ?

  1. sleep() method is defined in class Thread and wait() method is in class Object.
  2. sleep() is a static method where as wait() is an instance method.
  3. sleep(1000) will wait exactly for 1 second wait(1000) wait up-to 1 second.
  4. sleep() object will keeps its locks. wait() object releases all its locks.
  5. wait(), notify(), notifyAll() methods should be called only from with in synchronized context or code.
  6. A thread could stop waiting earlier if it receives the notify() or notifyAll() call.

10. Is the JDBC-ODBC Bridge multi-threaded ?

No. The JDBC-ODBC Bridge does not support concurrent access from different threads. The JDBC-ODBC Bridge uses synchronized methods to serialize all of the calls that it makes to ODBC. Multi-threaded Java programs may use the Bridge, but they won't get the advantages of multithreading.
In addition, deadlocks can occur between locks held in the database and the semaphore used by the Bridge. We are thinking about removing the synchronized methods in the future. They were added originally to make things simple for folks writing Java programs that use a single-threaded ODBC driver.

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