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) Explain repeated deletion of records.

A) //RepeatedDeletion.java import java.util.Scanner; import java.sql.*; class RepeatedDeletion {        public static void main(String[] args) {               Connection con= DriverManager. getConnection ( "jdbc:odbc:prince" , "scott" , "tiger" );               PreparedStatement ps=con.PreparedStatement( "DELETE FROM ACCOUNT WHERE ACCNO=?" );               Scanner s= new Scanner(System. in );               while ( true )               {                      System. out .println( "Enter accno : " );      ...

Introduction

1) What is not JDBC? 2) What is the purpose of JDBC? 3) What is ODBC? 4) What is JDBC? 5) What is JDBC architecture? 6) What is JDBC client? What are its responsibilities? 7) What is JDBC API?  8) What is driver manager? 9) What is JDBC driver? What is its role in java database connection? 10) What is database server?

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