wellnwill Marathahalli Bangalore

Thread Life cycle

  1. New
  2. Ready
  3. Running state
  4. Blocked / waiting / non-running mode
  5. Completed/Dead state

Synchronization

By default the execution of thread is not synchronized it means multiple threads can access the same resource at same time.

Programmatically multiple thread can execute same method concurrently. Sometimes we need proper synchronization between thread.

In java, synchronization is achieved by using the concept of locking.

Java provides the following two concepts to implement locking:

  1. synchronized method
  2. synchronized block

In java, each object has an implicit lock associated with it. In java each class also have an implicit lock associated with it.


Synchronized method

In case of static synchronized method in thread need to acquire the lock on the current class.

In case of non-static synchronized method in thread need to acquire the lock on the current object.

Synchronized block

Synchronized block is useful if we need to synchronized only a set of statement inside the method.

Syntax-

synchronized(classname.class){}
synchronized(object reference having object id){}
class A
{
 public synchronized static void show() 
 {
   System.out.println("How are you");
   System.out.println("i am");
   System.out.println("good");
 }
}
class Thread1 extends Thread
{
public void run()
{
A.show();
}
}
class SynTest
{
public static void main(String args[])
{
Thread1 t1=new Thread1();
Thread1 t2=new Thread1();
t1.start();
t2.start();
}
}




class A
{
 public static void show()
 {
  System.out.println("How are you");
  synchronized(A.class)
  {
   System.out.println("i am");
   System.out.println("good");
  }
 }
}
class Thread1 extends Thread
{
public void run()
{
A.show();
}
}
class SynTest1{
public static void main(String args[])
{
Thread1 t1=new Thread1();
Thread1 t2=new Thread1();
t1.start();
t2.start();
}
}


class A
{
 public synchronized void show()
 {
  System.out.println("How are you");
  System.out.println("i am");
  System.out.println("good");
 }
}
class Thread1 extends Thread
{
public void run()
{
A a1=new A();
a1.show();
}
}
class SynTest2
{
public static void main(String args[])
{
Thread1 t1=new Thread1();
Thread1 t2=new Thread1();
t1.start();
t2.start();
}
}


class A
{
 public void show()
 {
  System.out.println("How are you");
  synchronized(this)
  {
   System.out.println("i am");
   System.out.println("good");
  }
 }
 }
class Thread1 extends Thread{
public void run()
{
A a1=new A();
a1.show();
}
}
class SynTest3
{
public static void main(String args[])
{
Thread1 t1=new Thread1();
Thread1 t2=new Thread1();
t1.start();
t2.start();
}
}


Deadlock

When a thread enter into synchronized method, object will be locked, other threads cannot enter into that method until lock will be released.

Deadlock is a condition in which two threads are waiting to acquire the lock of each other.

To solve the deadlock problem, we have to establish inter thread communication.

InterThread communication

Inter thread communication is a mechanism where threads can communicate to each others. To establish communication wait() and notify() methods are used.

wait()

This method blocks the current thread execution as well as release all the locks associated with it and enter into waiting pool.

notify()

when a thread executes notify() method, that thread notify the other thread which is in waiting pool so that the thread which is in waiting pool can continue the execution.

notify() method, notify only one thread.

notifyAll() method, notify all the thread.

class Bank
{  
double amount=20000;  
synchronized void withdraw(double amount){  
System.out.println("withdrawing...");  
if(this.amount < amount)
{  
System.out.println("Less balance;waiting for deposit..");  
try{
wait();
}
catch(Exception e){}  
}  
this.amount-=amount;  
System.out.println("withdraw processed...");  
} 
synchronized void deposit(double amount){  
System.out.println("depositing...");  
this.amount+=amount;  
System.out.println("deposit processed... ");  
notify();  
}
}
class ITC1 extends Thread{ 
Bank b1;
ITC1(Bank b1)
{
	this.b1=b1;
}
public void run()
{
 b1.withdraw(25000);
}
}
class ITC2 extends Thread
{ 
Bank b1;
ITC2(Bank b1)
{
	this.b1=b1;
}
public void run()
{
 b1.deposit(25000);
}
}
class ITCTest{
public static void main(String args[]){ 
Bank b1=new Bank(); 
ITC1 t1=new ITC1(b1);  
t1.start();  
ITC2 t2=new ITC2(b1);  
t2.start();  
}
}  













wait() sleep()
wait() suspend the thread execution by releasing object lock temporary. sleep() suspend the thread execution without releasing the object lock.
Owait() should be notified by notify() or notifyAll() methods. sleep() method causes the current thread to sleep for a specified amount of time.
non-static method of Object class. static method of Thread class.

Daemon threads

The threads which are executed at background is called daemon threads. Ex: - garbage collector, default exception handler etc.

garbage collector

protected void finalize(){} – Object class method. public static void gc(){} ---- System and Runtime classes

Footer with Map