Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Synchronization Issue With Threads.

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have one small sample code for synchronization test.
**************************Account.java***************
public class Account
{
private int balance=50;

public int getBalance(){
return balance;
}

public void withdraw(int amt){
balance=balance-amt;
}
}

*********************AccountJob.java******************
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){
this.account=account;
}

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

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 ");
try{
Thread.sleep(1000);
}catch(InterruptedException e){}

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

}
**********************TestAccount.java********************
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));

thread1.setName("Fred");
thread2.setName("Lucy");

thread1.start();
thread2.start();
}
}
***************************************************************

Problem:
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
code:
//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.
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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



Yes!

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 ]
 
Enjoy the full beauty of the english language. Embedded in this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic