File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Threads and Synchronization and the fly likes Synchronization Issue With Threads. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Synchronization Issue With Threads." Watch "Synchronization Issue With Threads." New topic

Synchronization Issue With Threads.

sandeepg gadhvig

Joined: Jul 22, 2008
Posts: 10
I have one small sample code for synchronization test.
public class Account
private int balance=50;

public int getBalance(){
return balance;

public void withdraw(int amt){

public class AccountJob implements Runnable
private Account account;

public static final int MIN_BALANCE=10;
public static final int WID_BALANCE=10;

public AccountJob(Account account){

public void run(){
for(int i=0;i<5;i++){

private synchronized void withdraw(){
//------check for balance if >= 10 go on.
if(account.getBalance() >= MIN_BALANCE){System.out.println(Thread.currentThread().getName()+" is going to withdraw ");
}catch(InterruptedException e){}

System.out.println(Thread.currentThread().getName()+" completes withdraw.");
System.out.println("Not enough balance in "+Thread.currentThread().getName()+"'s account, balance is"+account.getBalance());

public class TestClass{

public static void main(String[] args){

Account account=new Account();
// both runnable objects refer to same Account object
// but here two threads have different runnable object.
Thread thread1=new Thread(new AccountJob(account));
Thread thread2=new Thread(new AccountJob(account));



in TestAccount class this code

Thread thread1=new Thread(new AccountJob(account));
Thread thread2=new Thread(new AccountJob(account));

i am passing to different objects to two different threads.
and also the method in AccountJob class

private synchronized void withdraw(){

is synchronized.
BUT im not getting the synchronized behavior.
two threads are withdrawing balance below 0.while it should not because the method for withdrawal is SYNCHRONIZED.

but if instead of that i try this thing in TestAccount class

code: AccountJob accountJob=new AccountJob(account);
// same object reference in two threads
Thread thread1=new Thread(accountJob);
Thread thread2=new Thread(accountJob);

Output: synchronization works

instead of current code which is
//different runnable objects.
Thread thread1=new Thread(new AccountJob(account));
Thread thread2=new Thread(new AccountJob(account));

Output: Account overdrawn synchronization doesn't work.

Can any one tell me
1)is this problem due to two different objects of Runnable
2)if yes then WHY.

waiting for answer.
Thaks for reply.
Nitesh Kant

Joined: Feb 25, 2007
Posts: 1638

Firstly, this is not an advanced question. Please CarefullyChooseOneForum for your post.

Answering your question,

sandeepg: is this problem due to two different objects of Runnable


sandeepg: if yes then WHY.

You have made the method withdraw() in AccountJob synchronized. This means that the synchronization will be on the AccountJob instance which is different for the two threads.

You must make the method withdraw() in Account as synchronized to get the correct synchronization behavior. Needless to say that you would have to make the check for the available balance inside the Account class.
[ August 05, 2008: Message edited by: Nitesh Kant ]

apigee, a better way to API!
I agree. Here's the link:
subject: Synchronization Issue With Threads.
jQuery in Action, 3rd edition