Showing posts with label CORE JAVA. Show all posts
Showing posts with label CORE JAVA. Show all posts

Monday, 24 May 2021

Lambda Expression in Java 8

Lambda Expression













Lambda calculus was the big change in Mathematical world which came in 1930s.
Because of this programmers also started using it.

LISP is the 1st lang where lambda expressions came first.
Apart form LISP, C, CPP, PYTHON, SCALA, RUBBY, Objective C

Advantages

  1. To enable functional programming in java
  2. To write more readable, maintainable and concise code
  3. To use APIs very easily and effectively.
  4. To enable parallel processing
Lambda Expressionlambda expressions are a means to create anonymous classes of functional interfaces easily
     1. No Name
     2. No Return Type
     3. No Access Modifier

NOTE : Only use in case of functional interface.


Functional Interface : An interface having only one abstract method is known as Functional Interface.
Example : Runnable , Callable etc.

There are some rules to write lambda expression from which two main important rules are
1. If body is having only one statement then { } braces are optional
2. If passing parameter as an argument then argument type is optional

Example.

public void display(String str){
    System.out.println("Hello "+str);
}
Using lambda
(str)->System.out.println("Hello "+str);
or
str->System.out.println("Hello "+str);
int getValue(){
  return 5;
}
Using lambda
() -> 5
// concatenating two strings, it has 2 string parameters and they are concatenated in lambda body
(String s1, String s2) -> s1+s2;


Below are some example codes using lambda

1. Calculator program
























2. Threading



Scenarios

  1. (int x, int y) -> x+y; or (x, y) -> x + y; which one of these is a valid lambda expression?

    Both of them are valid lambda expressions if used in a correct context.


  2. (int x, y) -> x + y; is this a valid lambda expression?

    You can't have lambda expression where type for only one of the parameter is explicitly declared so this lambda expression is invalid.

Read More »

Monday, 17 May 2021

10.0 Collection Framework in Java

Collection Framework

Why there is need of collection framework ?

 Limitation of Array : 

1. Fixed in Size - If we give less size then we can't expand it at run time, If we give more space then unused space will be of no use.

2. They are Homogeneous in type, we can only store similar type of data into an Array.

3. There is underlying data structure behind this as a result of there is no additional supporting methods are available.

Collections are grow able in nature.

Note : Arrays are highly recommended  because of its high performance but memory wise collections are good.


What is Collection ?

Group of individual object as a  single entity.

Java provides several classes and interfaces to achieve this also know as Collection Framework.


There are 9 key Interface




Read More »

Wednesday, 21 February 2018

New features in Java 8

Java 8 New Features


  1. Lambda expressions,
  2. Method references,
  3. Functional interfaces,
  4. Stream API,
  5. Default methods,
  6. Base64 Encode Decode,
  7. Static methods in interface,
  8. Optional class,
  9. Collectors class,
  10. ForEach() method,
  11. Parallel array sorting,
  12. Nashorn JavaScript Engine,
  13. Parallel Array Sorting,
  14. Type and Repating Annotations,
  15. IO Enhancements,
  16. Concurrency Enhancements,
  17. JDBC Enhancements etc.
Read More »

Friday, 9 February 2018

9.0 JDBC




JDBC - Java Database Connectivity

JDBC is a technology use to communicate between application and database.























STEPS


1. Create an Application


2. You need a translator 

       Class.forName("oracle.jdbc.driver.OracleDriver");

ojdbc6.jar will require if i am using oracle 11g


3. Create the connection (Road)

Connection con=DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:xe","system","password");  

4. Vechile is required

Statement stmt=con.createStatement();  

5. SQL query as per requirement and the the resultset is required where database engine will give you the result


ResultSet rs=stmt.executeQuery("select * from emp");  

while(rs.next())
{  
   System.out.println(rs.getInt(1)+" "+rs.getString(2));  
}  

6. Close the Road/Connection

con.close();  




practice



1. Translator = Driver
Class.forName("oracle.jdbc.driver.OracleDriver");

2. Road = Connection
Connection con=Driver.getConnection("jdbc:oracle:thin:@localhost:1521:xe","username","password");

3. Vechicle = Statement object
Statement smt=con.createStatement();

4. Requirement = sql
Resultset rs = smt.executeQuery("select * from emp");

5. Package = Resultset
while(rs.next())
{
System.out.println(rs.getInt(1)+ " " + rs.getString(2));
}

6. Close

con.cose();



Example

1. Create a table in mysql










































import java.sql.*;


public class FirstProgram {

public static void main(String args[])
{
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/bikesh?autoReconnect=true&useSSL=false";
String user="root";
String password="root";
try{
System.out.println("Database connection started....");
Class.forName(driver);
System.out.println("Driver Loaded");
Connection con=DriverManager.getConnection(url,user,password);
System.out.println("Connection Established");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from emp");
System.out.println("Query executed");
System.out.println("Below is the output ...");
while(rs.next())
{
System.out.println("Id = "+rs.getString(1));
System.out.println("Name = "+rs.getString(2));
System.out.println("Age = "+rs.getString(3));
}
con.close();
System.out.println("Database connection successfully closed");
}
catch(Exception e){
e.printStackTrace();
}
}

}

NOTE :

WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.

I was getting above warning - to fix this used below url.
String url="jdbc:mysql://localhost:3306/bikesh?autoReconnect=true&useSSL=false";



OUTPUT
Database connection started....
Driver Loaded
Conenction Established
Query executed
Below is the output ...
Id = 1
Name = Bikesh
Age = 27
Database connection successfully closed


1. INSERT Example using PreparedStatement

import java.sql.*;

public class Insert {
public static void main(String args[])
{
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/bikesh?useSSL=false";
String username="root";
String password="root";
Connection con;
PreparedStatement ps;
ResultSet rs;

try{
Class.forName(driver);
con=DriverManager.getConnection(url,username,password);
String sqlinsert="insert into emp values(?,?,?)";
ps=con.prepareStatement(sqlinsert);
ps.setInt(1, 3);
ps.setString(2, "Maurya");
ps.setInt(3, 29);
ps.executeUpdate();

String sql="select * from emp";
ps=con.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next())
{
System.out.println(
"Id = "+rs.getString(1)+" "+
"Name = "+rs.getString(2)+ " "+
"Age = "+rs.getString(3))
;
}
con.close();
}
catch(Exception e){
e.printStackTrace();
}
}
}


OUTPUT
Id = 1 Name = Bikesh Age = 27
Id = 2 Name = Amites Age = 29
Id = 3 Name = Maurya Age = 29

2. Update Example


import java.sql.*;

public class Insert {
public static void main(String args[])
{
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/bikesh?useSSL=false";
String username="root";
String password="root";
Connection con;
PreparedStatement ps;
ResultSet rs;

try{
Class.forName(driver);
con=DriverManager.getConnection(url,username,password);

String sqlupdate="update emp set name=? where id=?";
ps=con.prepareStatement(sqlupdate);
ps.setString(1, "Amitesh");
ps.setInt(2, 2);
ps.executeUpdate();

String sql="select * from emp";
ps=con.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next())
{
System.out.println(
"Id = "+rs.getString(1)+" "+
"Name = "+rs.getString(2)+ " "+
"Age = "+rs.getString(3))
;
}
con.close();
}
catch(Exception e){
e.printStackTrace();
}
}
}


Id = 1 Name = Bikesh Age = 27
Id = 2 Name = Amitesh Age = 29
Id = 3 Name = Maurya Age = 29


3. Delete example


table data before deletion
Id = 1 Name = Bikesh Age = 27
Id = 2 Name = Amitesh Age = 29
Id = 3 Name = Maurya Age = 29

import java.sql.*;

public class Insert {
public static void main(String args[])
{
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/bikesh?useSSL=false";
String username="root";
String password="root";
Connection con;
PreparedStatement ps;
ResultSet rs;
try{
Class.forName(driver);
con=DriverManager.getConnection(url,username,password);
String sqlupdate="delete from emp where id=?";
ps=con.prepareStatement(sqlupdate);
ps.setInt(1, 3);
ps.executeUpdate();
String sql="select * from emp";
ps=con.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next())
{
System.out.println(
"Id = "+rs.getString(1)+" "+
"Name = "+rs.getString(2)+ " "+
"Age = "+rs.getString(3))
;
}
con.close();
}
catch(Exception e){
e.printStackTrace();
}
}
}
 

OUTPUT
Id = 1 Name = Bikesh Age = 27
Id = 2 Name = Amitesh Age = 29




Read More »

Monday, 29 January 2018

7.0 MultiThreading in Java

Multitasking - executing multiple tasks simultaneously.

  • process based multitasking
Executing multiple task simultaneously where each task is separate process
  • Thread based multitasking
Executing multiple task simultaneously where each task is independent but part of same program.

main advantage is to increase performance and to reduce response time of system.


MULTITHREADING - Multiple threads executing simultaneously

Thread : A light weight process.
A separate flow of execution.

Defining Thread -  
  1. By extending Thread class
  2. By implementing Runnable interface



class ThreadDemo
{
public static void main(String args[])
{
MyThread obj=new MyThread();
obj.start();
for(int i=1;i<=10;i++)
{
System.out.println("Main Thread : " +i);
}
}
}
class MyThread extends Thread
{
public void run()
{
for(int i=1;i<=10;i++)
{
System.out.println("Child Thread : " +i);
}
}
}


C:\Users\Aarchi\Desktop\Java Development>java ThreadDemo
Main Thread : 1
Main Thread : 2
Main Thread : 3
Child Thread : 1
Main Thread : 4
Main Thread : 5
Child Thread : 2
Main Thread : 6
Child Thread : 3
Main Thread : 7
Child Thread : 4
Main Thread : 8
Child Thread : 5
Main Thread : 9
Child Thread : 6
Main Thread : 10
Child Thread : 7
Child Thread : 8
Child Thread : 9
Child Thread : 10




Thread scheduler - It is a part of JVM and responsible for threads execution.
In which order threads will execute is decide by thread scheduler. 

Overloading of run() - is always possible but thread class start() can invoke no-arg run ().
The other overloaded method can be invoked explicitly like a normal method call.

If you'll not override run() in you class then Thread class run() will get executed which has empty implementation hence we'll not get any output.

If we override start() then our start() will be executed just like a normal method call and new thread won't be created.

THREAD LIFE CYCLE









After starting a thread if we are trying to restart the same thread then we'll get run time exception.
saying IllegalThreadStateException.

Thread t=new Thread();
t.start();
t.start();


Defining a thread by implementing Runnable Interface.

Runnable interface present in java.lang package and only contain one method run().



import java.lang.*;
class ThreadDemo1
{
public static void main(String args[])
{
MyRunnable obj=new MyRunnable();
Thread t=new Thread(obj);
t.start();
for(int i=1;i<=10;i++)
{
System.out.println("Main Thread : "+i);
}
}
}

class MyRunnable implements Runnable
{
public  void run()
{
for(int i=1;i<=10;i++)
{
System.out.println("Child Thread : "+i);
}
}
}

C:\Users\Aarchi\Desktop\Java Development>java ThreadDemo1
Main Thread : 1
Main Thread : 2
Main Thread : 3
Main Thread : 4
Main Thread : 5
Child Thread : 1
Child Thread : 2
Main Thread : 6
Child Thread : 3
Main Thread : 7
Child Thread : 4
Main Thread : 8
Child Thread : 5
Main Thread : 9
Main Thread : 10
Child Thread : 6
Child Thread : 7
Child Thread : 8
Child Thread : 9
Child Thread : 10


CASE STUDY

NOTE : implementation of Runnable interface is recommended
If we'll extend Thread class then we can't extend any other class.


THREAD CLASS CONSTRUCTORS
  1. Thread t = new Thread();
  2. Thread t = new Thread(Runnable r);
  3. Thread t = new Thread(String name);
  4. Thread t = new Thread(Runnable r, String name);
  5. Thread t = new Thread(ThreadGroup g, String name);
  6. Thread t = new Thread(ThreadGroup g, Runnable r);
  7. Thread t = new Thread(ThreadGroup g, Runnable r, String name);
  8. Thread t = new Thread(ThreadGroup g, Runnable r, String name, Long stacksize);
class MyThread extends Thread

MyThread obj=newMyThread();
Thread t=new Thread(obj);
t.start();

Thi is valid
MyThread ------>Thread------->Runnable

  1. Thread t = new Thread(Runnable r);
we can pass MyThread object here.



To get and set Thread names

class ThreadDemo2
{
public static void main(String args[])
{
System.out.println(Thread.currentThread().getName());
MyThreada obj=new MyThreada();
System.out.println(obj.getName());
Thread.currentThread().setName("Bikesh");
System.out.println(Thread.currentThread().getName());
System.out.println(10/0);
}
}
class MyThreada extends Thread
{
}


C:\Users\Aarchi\Desktop\Java Development>java ThreadDemo2
main
Thread-0
Bikesh
Exception in thread "Bikesh" java.lang.ArithmeticException: / by zero
        at ThreadDemo2.main(ThreadDemo2.java:10)


Thread Priorities
Every thread in java have some priority.
Range of Priorities - 1(min) to 10(max)
Thread.MIN_PRIORITY=1
Thread.NORM_PRIORITY=5
Thread.MAX_PRIORITY=10
Thread.LOW_PRIORITY - Invalid

Thread scheduler will set the priority while allocating processor.
Thread which is having highest priority will get the chance first.
If two threads have same priority then we can't expect exact execution order. It depends on Thread scheduler.

Thread class defines the following methods to get and set priority of the thread.
public final int getPriority();
public final void setPriority(int p);
where p ranges from 1 to 10.
if it is not then we'll get IllegalArgumentException.


default priority for every thread is 5. (InCorrect) default priority of all thread inherited from parent to child. i.e whatever priority parent thread has , the same priority will be there for the child thread.
OR
default priority for only main thread is 5. (Correct)


class ThreadDemo2
{
public static void main(String args[])
{
System.out.println("Priority of "+Thread.currentThread().getName() +" = "+Thread.currentThread().getPriority());
Thread.currentThread().setPriority(8);
MyThreada obj=new MyThreada();
System.out.println("Priority of "+Thread.currentThread().getName() +" = "+Thread.currentThread().getPriority());
System.out.println("Priority of "+obj.getName() +" = "+obj.getPriority());
}
}
class MyThreada extends Thread
{
public void run(){}
}


C:\Users\Aarchi\Desktop\Java Development>javac ThreadDemo2.java

C:\Users\Aarchi\Desktop\Java Development>java ThreadDemo2
Priority of main = 5
Priority of main = 8
Priority of Thread-0 = 8

Thread.currentThread().setPriority(18);

Exception in thread "main" java.lang.IllegalArgumentException
        at java.lang.Thread.setPriority(Thread.java:1089)
        at ThreadDemo2.main(ThreadDemo2.java:10)


Some platforms won't provide support for thread priorities.
In Spite of setting highest priority to a thread , you may not get the desired output.


PREVENT THREAD EXECUTION
  1. yield()
  2. join()
  3. sleep()
yield() - causes to pass current executing thread to give the chance for waiting thread of same priority.
if there is no waiting thread or all waiting threads have low priority then same thread can continue its execution.
The thread which is yielded, when it will get the chance once again depends upon thread scheduler.
We can't expect exact immediate regaining the execution engine.

join() -
If a thread wants to wait until completing some other thread then we should go for join().
for ex. if a thread t1 wants to wait until completing t2 then t1 has to call t2.join()
If t1 executes t2.join() then  immediately t1 will be entered into waiting state until t2 completes.
Once t2 completes then t1 can continue its execution.

public void final join() throws InterruptedException;
public void final join(long ms) throws InterruptedException;

public void final join(long ms , in ns) throws InterruptedException;

while a thread waiting until completing some other thread then there may be a chance interrupted exception. Therefore every join method throws InterruptedException which is checked exception exception compulsory we should handle this exception either by using try catch or by throws keyword.

Example to get main thread to apply join

class MyThreadDemo extends Thread
{
static Thread mt;
public void run()
{
try{
mt.join();
}
catch(InterruptedException ie)
{
System.out.println(ie.getMessage());
}
for(int i=1;i<=10;i++)
{
System.out.println("Child Thread : "+i);
}
}
}

class ThreadDemo3
{
public static void main(String args[])
{
MyThreadDemo.mt=Thread.currentThread();
MyThreadDemo obj=new MyThreadDemo();
obj.start();
for(int i=1;i<=10;i++)
{
System.out.println("Main Thread : "+i);
}
}
}

If main thread also put obj.join() then a deadlock situation will arise.
If main thread put join on main thread itself then also deadlock situation will arise.
Thread.currentThread().join();

sleep() - If a thread don't want to perform any operation for a particular amount of time then we should go for sleep().
public static native void sleep(long ms) throws InterruptedException;
public static void sleep(long ms, int ns) throws InterruptedException;
Every sleep method throws IE Exception, which is checked exception hence whenever we are using sleep method , compulsory we should handle InterruptedException either by try catch or by throws keyword.

class ThreadDemo4
{
public static void main(String args[]) throws InterruptedException
{
for(int i=1;i<=10;i++)
{
System.out.println("Slider = "+i);
Thread.sleep(5000);
}
}
}


interrupt() - example of how a thread can interrupt another thread

class ThreadDemoInterrupt extends Thread
{
public void run()
{
try{
for(int i=1;i<=10;i++)
{
System.out.println("Lazy Thread = "+i);
Thread.sleep(2000);
}
}
catch(InterruptedException ie)
{
System.out.println("I got interrupted");
}
}
}

class ThreadDemo4
{
public static void main(String args[])
{
ThreadDemoInterrupt obj=new ThreadDemoInterrupt();
obj.start();
obj.interrupt();
System.out.println("End of main THread");
}
}

Note : Whenever we are calling interrupt() if the target thread not in sleeping state or waiting state then there is no impact of interrupt call immediately. Interrupt call will wait until target thread enter into sleeping or waiting state. If the target thread enter into sleeping or waiting state , immediately interrupt call will interrupt the target thread. 
If the target thread never entered into sleep or waiting state in its lifetime then there is no impact of interrupt call. This is the only case where interrupt call will be wasted.
























Synchronization - It is a modifier .It is only applicable for methods and block but not for classes and variables

If multiple threads try to operate simultaneously on a single java object then this type of problem is called object inconsistency problem.

To resolve data inconsistency we can use synchronized keyword.

if a method or block declared as synchronized then at a time only one thread is allowed to execute that method or block on the given object.

The main advantage of synchronized keyword is , we can resolve data inconsistency problems but the main disadvantage is , it increases waiting time of threads and creates performance problems. Hence if there is no specific requirement then it is not recommended to use synchronized keyword.

Internally synchronization concept is implemented by using lock.
Every object in java has a unique lock. Whenever we are using synchronized keyword then only lock concept will come into picture.
If a thread wants to execute a synchronized method on the given object , first it has to get lock of that object.
Once thread got the lock then it is allowed to execute any synchronized method on that object. 
Once method execution completes , automatically thread releases lock. So acquiring and releasing lock internally takes care by JVM.



















t1 got the lock and start execution of m1()
while t1 was executing t2 came but it will go into waiting state.
t3 came to access m2 method. But to execute m2 object lock is required which is currently with t1 therefore t3 will also go into waiting state.
t4 came to access m3, it will directly able to execute because no lock is required to execute a non-synchronized method.


NON-SYNCHRONIZED EXAMPLE

class NonSync
{
public static void main(String args[])
{
Display obj=new Display();
MyThr t1=new MyThr(obj,"Bikesh");
MyThr t2=new MyThr(obj,"Amitesh");
MyThr t3=new MyThr(obj,"Maurya");
t1.start();
t2.start();
t3.start();
}
}

class Display
{
public void wish(String name)
{
for(int i=1;i<=10;i++)
{
System.out.println("Good Morning : " + name);
try{
Thread.sleep(1000);
}
catch(InterruptedException ie)
{}
}
}
}

class MyThr extends Thread
{
Display d;
String name;
public MyThr(Display d,String name)
{
this.d=d;
this.name=name;
}
public void run()
{
d.wish(name);
}
}

C:\Users\Aarchi\Desktop\Java Development>javac NonSync.java

C:\Users\Aarchi\Desktop\Java Development>java NonSync
Good Morning : Amitesh
Good Morning : Maurya
Good Morning : Bikesh
Good Morning : Amitesh
Good Morning : Bikesh
Good Morning : Maurya
Good Morning : Amitesh
Good Morning : Maurya
Good Morning : Bikesh
Good Morning : Amitesh
Good Morning : Maurya
Good Morning : Bikesh
Good Morning : Maurya
Good Morning : Bikesh
Good Morning : Amitesh
Good Morning : Maurya
Good Morning : Bikesh
Good Morning : Amitesh
Good Morning : Maurya
Good Morning : Amitesh
Good Morning : Bikesh
Good Morning : Maurya
Good Morning : Amitesh
Good Morning : Bikesh
Good Morning : Maurya
Good Morning : Amitesh
Good Morning : Bikesh
Good Morning : Maurya
Good Morning : Amitesh
Good Morning : Bikesh


SYNCHRONIZED EXAMPLE

just add the synchronized modifier in the method
public synchronized void wish(String name)

C:\Users\Aarchi\Desktop\Java Development>javac NonSync.java

C:\Users\Aarchi\Desktop\Java Development>java NonSync
Good Morning : Maurya
Good Morning : Maurya
Good Morning : Maurya
Good Morning : Maurya
Good Morning : Maurya
Good Morning : Maurya
Good Morning : Maurya
Good Morning : Maurya
Good Morning : Maurya
Good Morning : Maurya
Good Morning : Bikesh
Good Morning : Bikesh
Good Morning : Bikesh
Good Morning : Bikesh
Good Morning : Bikesh
Good Morning : Bikesh
Good Morning : Bikesh
Good Morning : Bikesh
Good Morning : Bikesh
Good Morning : Bikesh
Good Morning : Amitesh
Good Morning : Amitesh
Good Morning : Amitesh
Good Morning : Amitesh
Good Morning : Amitesh
Good Morning : Amitesh
Good Morning : Amitesh
Good Morning : Amitesh
Good Morning : Amitesh
Good Morning : Amitesh

If multiple thread operating on same object then only synchronization required.
In below case we have created two different object and t1 and t2 will lock obj1 and obj2, therefore synchronization is not required here. In spite of having synchronize method we'll get irregular output.

class NonSync
{
public static void main(String args[])
{
Display obj1=new Display();
                Display obj2=new Display();
MyThr t1=new MyThr(obj1,"Bikesh");
MyThr t2=new MyThr(obj2,"Amitesh");
t1.start();
t2.start();
}
}

static synchronized method will resolve this issue
public static synchronized void wish(String name)

Class level lock - every class in java has a unique lock which is also known as class level lock. 

If a thread wants to execute any static synchronized method then it require class level lock is required instead of object lock required.
























class bank
{
public static void main(String args[])
{
locker l1=new locker();
locker l2=new locker();
MyThreadDemoBank1 t1=new MyThreadDemoBank1(l1,"Bikesh");
MyThreadDemoBank2 t2=new MyThreadDemoBank2(l2,"Amitesh");
t1.start();
t2.start();
}
}

class locker
{
static int amount=1000;
static int translimit=0;
public static synchronized void account(String name)
{
while (amount > 0)
{
translimit++;
int total=amount;
amount=amount-100;
try
{
Thread.sleep(1000);
}
catch (InterruptedException ie)
{
}
System.out.println("Transaction : "+total+" - 100 ( "+name+" withdraw) ="+amount);
if(translimit==5)
{translimit=0;break; }
}
}
}

class MyThreadDemoBank1 extends Thread
{
String name;
locker l;
public MyThreadDemoBank1(locker l,String name)
{
this.l=l;
this.name=name;
}
public void run()
{
l.account(name);
}
}

class MyThreadDemoBank2 extends Thread
{
String name;
locker l;
public MyThreadDemoBank2(locker l,String name)
{
this.l=l;
this.name=name;
}
public void run()
{
l.account(name);
}
}

C:\Users\Aarchi\Desktop\Java Development>javac bank.java

C:\Users\Aarchi\Desktop\Java Development>java bank
Transaction : 1000 - 100 ( Amitesh withdraw) =900
Transaction : 900 - 100 ( Amitesh withdraw) =800
Transaction : 800 - 100 ( Amitesh withdraw) =700
Transaction : 700 - 100 ( Amitesh withdraw) =600
Transaction : 600 - 100 ( Amitesh withdraw) =500
Transaction : 500 - 100 ( Bikesh withdraw) =400
Transaction : 400 - 100 ( Bikesh withdraw) =300
Transaction : 300 - 100 ( Bikesh withdraw) =200
Transaction : 200 - 100 ( Bikesh withdraw) =100
Transaction : 100 - 100 ( Bikesh withdraw) =0


SYNCHRONIZED BLOCK
If very few lines of the code required synchronization then it is not recommended to declare entire method as synchronized. We have to enclose those few lines of the code by using synchronized block. 

The main advantage of synchronized block over synchronized method is it reduces waiting time of threads and improves performance of the system/application.

synchronized(this)
{
..............
...............
..............
}
this is use to get the lock of current object

to get lock of particular project
synchronized(b)
{
.............
.............
.............
}
if a thread got the lock of object b then only it is allowed to execute this block.

to get class level lock
synchronized(Display.class)
{
...............
..............
..............
}
if a thread want to get the class level lock of class Display.

Lock concept only applicable for object types and class types but not for primitives.Hence we can't pass primitive type as argument to synchronized block otherwise we'll get compile time error saying "unexpected type fount int required ref."

int x=1;
ex: 
synchronize(x)
{
..............
...........
...............
}


QUICK REVISION:
  1. What is synchronized keyword and where we can apply ?
  2. Explain advantage of synchronized keyword.
  3. Explain disadvantage of synchronized keyword.
  4. What is race condition ? (data inconsistency problem)
  5. What is object lock and when it is required ?
  6. What is class level lock and when it is required ?
  7. Difference between class level and object level lock ?
  8. What is synchronized block ?
  9. How to declare synchronized block to get lock of current object, class level lock ?
  10. what is the advantage of synchronize block over synchronized method.
  11. is a thread can acquire multiple locks simultaneously ? (Yes)  



Read More »