File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Reg. Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Reg. Threads " Watch "Reg. Threads " New topic
Author

Reg. Threads

Angela Narain
Ranch Hand

Joined: Apr 14, 2001
Posts: 327
"When one thread calls a synchronized method , it is guaranteed
that the method will finish, before another can execute any
synchronized method on the same object "
I am not getting clear about the phrase "on the same object"
I understant , synchronized method will be executed by only one thread at a time,no other thread will allowed to execute that method if some
thread is already executing it.
Can someone give me an example related to this ?
thanks
Rashmi Tambe
Ranch Hand

Joined: Aug 07, 2001
Posts: 418
Hi Angela ,
Consider following Ex.
class BankAccount{
private int balance;
public synchronizec int ReadBalance(){
return balance;
}
public synchronized void UpdateBalance(int amount){
balance += amount;
}
}
class ReaderThread extends Thread{
BankAccount a;
ReaderThread (BankAccount a){
this.a = a;
}
public void run(){
while (true){
System.out.println("Balance=" + a.ReadBalance());
}//while
}
}//readerThread

class WriterThread extends Thread{
BankAccount a;
WriterThread (BankAccount a){
this.a = a;
}
public void run(){
while (true){
a.UpdateBalance(100);
System.out.println("Updated the Balance by Amount " + 100+ "$");
}//while
}
}//writerThread

public class Bank{
public static void main(String args[]){
BankAccount a= new BankAccount();
ReaderThread t1= new ReaderThread (a); //1
WriterThread t2 = WriterThread (a); //2
t1.start();
t2.start();
}//main
}//bank

Now here both Reader and writer thread are working on the same object of the bank account ie.a. Both threads are in while loop, continuously reading or writing the account resp.
In this case, 'ReadBalance()' and 'UpdateBalance()' being synch., unless one thread finishes executing a method on the same bank account object'a', second can't.
I hope this helps.
Rashmi

[This message has been edited by Rashmi Gunjotikar (edited September 03, 2001).]
Angela Narain
Ranch Hand

Joined: Apr 14, 2001
Posts: 327
Does it mean that for the thread t2, the method updateBalance()
will be always invoked after the thread t1's method readBalance()
has finished.
Also if i change the main method line // 2 code as :
public class Bank{
public static void main(String args[]){
BankAccount a= new BankAccount();
ReaderThread t1= new ReaderThread (a); //1
WriterThread t2 = new WriterThread (new BankAccount()); //2
t1.start();
t2.start();
}//main
}//bank
It will work fine. But here the BankAccount() new instance
has been created , then how it is the same object ?
Rashmi Tambe
Ranch Hand

Joined: Aug 07, 2001
Posts: 418
No, It does not mean that for thread t2, the method updateBalance() will be always invoked after the thread t1's method readBalance()has finished.

These two threads are working independently,but on the same object. They have different purposes. However, they are running continuously because of while loop. So it may happen that while writer thread is updating the balance, it is interrupted inbetween and reader is scheduled. However writer holds the lock on bank account object'a' , so reader has to wait till it aquires it. So reader goes in lock waiting queue of object 'a'. When some time after writer is again scheduled and completes it's work, lock is released. If u want to notify this event to reader thread, u can use wait() and notify(). (i haven't used it.) In this scenario, we can never say that, updatebalance() is always invoked after readbalance() is executed. It depends on thread scheduling.
About ur secong Q-
Yes, U r creating new instance of bank account(annonymous instance) and passing it for t2. So these 2 r diff objects. t1 is reading the balance of 'a' and t2 is updating the balance of some other annonymous object.
Why would u want to do that? In this caes there is no need of sync. as both the threads are working on diff. object. synch. comes into picture only when u want to protect some data of object from being corrept inbetween because of uncontrolled thread scheduling.
Hope this helps.
Rashmi
vipin bhatia
Ranch Hand

Joined: Jul 14, 2001
Posts: 31
import io.*;
class BankAccount
{
private int balance;
boolean flag;
public synchronized int ReadBalance()
{
try{
if(flag)
{
notify();
wait();
}
flag = true;
return balance;
}catch(Exception e)
{
P.l(e);
}
return 0;
}
public synchronized void UpdateBalance(int amount)
{
try
{
if(!flag)
{
notify();
wait();
}
flag = false;
balance += amount;
}
catch(Exception e)
{
P.l(e);
}
}
}
class ReaderThread extends Thread
{
BankAccount a;
ReaderThread (BankAccount a)
{
this.a = a;
}
public void run()
{
while (true)
{
System.out.println("Balance=" + a.ReadBalance());
}//while
}
}//readerThread
class WriterThread extends Thread
{
BankAccount a;
WriterThread (BankAccount a)
{
this.a = a;
}
public void run()
{
while (true)
{
a.UpdateBalance(100);
System.out.println("Updated the Balance by Amount " + 100+ "$");

}//while
}
}//writerThread

public class Bank
{
public static void main(String args[])
{
BankAccount a= new BankAccount();
ReaderThread t1= new ReaderThread (a); //1
WriterThread t2 = new WriterThread (a); //2
t1.start();
t2.start();
}//main
}//bank
this program takes care of ur requirement angila where in the balance will be displayed only after it has been incremented by the other piece of code which the ReaderThread is notifying.
:-)
------------------
Sun Certified Programmer for Java2 Platform scored 100%.


Sun Certified Programmer for Java2 Platform scored 100%.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reg. Threads