aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Problem with excirse 9-2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Problem with excirse 9-2" Watch "Problem with excirse 9-2" New topic
Author

Problem with excirse 9-2

Eugenio Flores
Ranch Hand

Joined: Sep 13, 2007
Posts: 38
I resolve the excirse this way:

public class SyncExample extends Thread{

private StringBuffer sb;

public SyncExample (StringBuffer sb){
this.sb = sb;

}

public StringBuffer getSb() {
return sb;
}

public void run (){
synchronized (sb){
if (this.sb !=null){
for (int i = 1; i <= 100; i++){

System.out.println(Thread.currentThread().getName() + " i " + i + " "+ this.sb);
}
char ch0 = this.getSb().charAt(0);
ch0++;
this.getSb().setCharAt(0, ch0);
}

}

}

public static void main(String[] args) {

StringBuffer sb = new StringBuffer("A");
SyncExample t1 = new SyncExample(sb);
SyncExample t2 = new SyncExample(sb);
SyncExample t3 = new SyncExample(sb);
t1.setName("A");
t2.setName("B");
t3.setName("C");
t1.start();
t2.start();
t3.start();
}
}
And it works fine, but if I change the lock for the this instance (synchronized (this) instead of synchronized (sb)) I don�t get the expected result. The threads access the synchronized block interchangeably, and that is not correct. And if you saw the example named AccountDanger (page 706) you would see that the synchronized method makeWithdrawal, works fine, the object Account, which is referenced by acct, is accessed by one thread at a time. And as I understand it, the method makeWithdrawal locks on the this instance. Why does this last example present a different behavior than the exercise 9-2 when it locks on the this instance?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

This is a normal behavior. All the three threads share the same StringBuffer object but all of them are separate instances of SyncExample class. So each of them will have a separate this. This is the reason that if you synchronize of this, then access the StringBuffer interchangeably as they all hold locks to their own instance which is not common between them.
[ September 02, 2008: Message edited by: Ankit Garg ]

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Eugenio Flores
Ranch Hand

Joined: Sep 13, 2007
Posts: 38
Ankit, thank you for your answer. Now I think I understand why the example AccountDanger works fine. Because the instance that the threads share is the same, and the lock is on that instance (n). The this instance refers to the object whose reference variable is (n). Am I right?



AccountDanger n = new AccountDanger();
Thread one = new Thread(n);
Thread two = new Thread(r);
one.setName("Fred");
two.setName("Lucy");
one.start();
two.start();
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with excirse 9-2