Tuesday, December 29, 2009

Java




Synchronization of Threads in Java


By Abir Atarthy


Today I will discuss a topic in java called Synchronization of Threads.

When two threads need to share the data, you must ensure that one thread does not change the data used by the another thread. On such occation they might compete themselves for the same resources and create conflicts. For example one thread may try to read a file while other thread is still writing to the same file.

Java enables you to coordinate the actions of multiple threads by using synchronized methods or synchronized statements.

An object for which access is to be coordinated is accessed by using methods declared with the synchronized keyword. For exmp. the method that will read information from a file and the method that will update the same file may be declared as synchronized. You can invoke only one synchronized method for an object at any given point of time.

This prevents conflicts between the synchronized methods in multiple threads.

The addition of the synchronized modifier to a method declaration ensures that only one thread is allowed inside the method at a time. This can be useful in keeping out other threads.

Lock term refers to the access granted to a particular thread that can access the shared resources. At any given time, only one thread can hold the lock and thereby have access to the shared resource. Every object in Java has build-in lock that only comes in action when the object has synchronized method code. By associating a shared resource with a Java object and its lock, the object can act as a guard, ensuring synchronized access to the resource. Only one thread at a time can access the shared resource guarded by the object lock.

Remember the following points related to lock and synchronization:

  • Only methods (or blocks) can be synchronized, Classes and variable cannot be synchronized.

· Each object has just one lock.

· If a class has both synchronized and non-synchronized methods, multiple threads can still access the class's non-synchronized methods.

· If a thread goes to sleep, it holds any locks it has—it doesn't release them.

· Constructors cannot be synchronized

The following code shows how a synchronized

Method can be used.

Consider the following program:-

It has been compiled in JDK 1.6 compiler.


public class account {

public int balance;

public int accountno;

void display(){

System.out.println("Account no"+accountno +"Balance"+balance);

}

synchronized public void deposite(int amount){

//Method to diposite

balance=balance+amount;

System.out.println("Amount is deposited");

display();

}

synchronized public void withdraw(int amount) {

//Method to withdraw

balance=balance-amount;

System.out.println("Amount withdrawn");

display();

}

}

//implementing a thread to diposite

class transactiondiposite implements Runnable{

public int amount;

account accountx=new account();

transactiondiposite(account x,int amount){

accountx= x;

this.amount=amount;

new Thread(this).start();

}

public void run(){

accountx.deposite(amount);

}

}

//implementing a thread to withdraw

class transactionwithdraw implements Runnable{

account accounty=new account();

int amount;

transactionwithdraw(account y,int amount){

accounty= y;

this.amount=amount;

new Thread(this).start();

}

public void run(){

accounty.withdraw(amount);

}

}

class transaction{

public static void main(String args[]){

account obj=new account();//creates an account

obj.balance=1000;//initialize the account

obj.accountno=345;

transactiondiposite t1; //A thread for diposite

transactionwithdraw t2;//Another thread for withdraw

t1=new transactiondiposite(obj,500);

t2=new transactionwithdraw(obj,900);

}

}

OUTPUT:- Amount is deposited

Account no345Balance1500

Amount withdrawn

Account no345Balance600


No comments:

Post a Comment