Skip to main content

Q) How is synchronization implemented in multithreaded java application?




A)
->in java, synchronization is implemented by using the modifier “synchronized”. We have two levels of synchronization.
1) method level
2) block level
->if we apply synchronized modifier to the method of an object, it is known as method level synchronization.
Ex:
Synchronized void withdraw(0 // method level synchronization
{
     ---
     ---  
}
->when there is no source code available with us we can’t ass synchronized to that method.
->therefore at method level w can’t do.
->Therefore go for process level.

 Class acccountthread
{
     run()
    {
         Synchronized(acc)
           {
                  acc.withdraw();
            }
}
}
->instead of declining the method as synchronized, we can create a block of statements by using “Synchronized” modifier. It is known as block level synchronization.
->Synchronized block has the fallowing syntax:

Synchronized(object)
{
   // block of statements.
}

Ex:
public void run()
{
   Synchronized(acc)
    {
      acc.withdraw();
    }
}

Ex:
Public void withdraw(float amt)
{
   ---
   ---
   Synchronized(this)
   {
      //vital code that should not be executed parllelly by more than one thread on the same    
      //object.
   }
    ----
    ----
}
->in the first example we may or may not have the source code.
->second example is mostly used.
->when acc object one same, stop parallelism that is when x threads are acting on same object.
Public void run()
{
   acc.withdraw();
}//error
->here there may be a chance of data inconsistency in previous program code. So use synchronized.
->method level synchronization on is not powerful.
->block level Synchronization is powerful.
->2nd example written only if we know withdrawn. i.e. developed by us.
Therefore synchronized block causes serial processing which reduces performance.
Therefore keep only less code in synchronized block.
->EJB container provides multithreading code to program implicitly.
->web container provides multithreading code to servlets, JSP.
->synchronization is the background uses the concept of mutex (mutually exclusive block), monitor.
->in the background synchronized keyword is given to JVM. JVM gives to os. Os implements this mutex, monitor.
->monitor:
           Critical reason of code is created by os.
           Where only one thread is allowed at a time to access (all synchronized methods of that   
           object is monitor size. The code that is kept in monitor can’t be parllelly executed).
->mutex:
          Only one of threads, will get lock, all will not get lock. Other threads can’t enter  
           monitor. When lock is released, i.e. executes the code only then it can enter monitor.

Comments

Popular posts from this blog

Q) What is JDBC driver? What is its role in java database connection?

A) ->A translation software written in java according o JDBC specification is nothing but “JDBC driver”. -> JDBC driver implements JDBC API. Use single API. JDBC driver role: They are 4 roles   1)       Establishing connection between JDBC client and database server. 2)       Receiving JDBC method calls made by JDBC client while performing database (CRUD) operations. 3)       Translating java method calls into DBMS understandable calls and forwarding them to database server. 4)       Receiving the results from database server. Translating them into java format and handling over the same to the JDBC client.

Q) What is ODBC?

A) ->open database connectivity is a technology from MS that enables any language program (other than java) to communication with database Management System. ->in java program ODBC is directly not allowed for the fallowing reasons 1) OS dependency 2) Procedural oriented approach 3) Security thread

Q) Explain about nested classes?

A) ->defining a class with in another class is known as nesting of the class. Ex: class A {        class B        {                      } //nested class, and inner class.because it is not static class. } //enclosing class or outer class. ->static class is declared in inside an another class then it does not become an inner class. Note: non-static nested class is known as “inner class”. In the above example class B is nested but not an inner class. Ex: class D {        static c        {                      } //nested class, but not an inner class , because of static class. } //outer class. ->we have three different kinds of inner classes. 1) Member inner class 2) Meth...