| Author |
synchronization: Clarify!!
|
Ashok Paulraj
Ranch Hand
Joined: Jul 07, 2003
Posts: 78
|
|
Clarify synchronization: I guess I didn't quite get hold off which object will be synchronized in this context. Does both the run & setPrice methods are synchronized on the same object, confusing ?... any help is appreciated... Thanks, Shalini public class Master{ boolean bContinue=false; public static void main(String argv[]){ Master m = new Master(); m.go(); } public void go(){ Slave s = new Slave(this); Thread t1 = new Thread(s); t1.start(); while(bContinue==false){ } s.setPrice(200); } } class Slave implements Runnable{ int iPrice =100; Master master; Slave(Master m){ master=m; } synchronized public void setPrice(int iM){ iPrice=iM; } synchronized public void run(){ master.bContinue=true; while(true){ System.out.println(iPrice); } } }
|
 |
David Hadiprijanto
Ranch Hand
Joined: Sep 14, 2003
Posts: 52
|
|
Hello Taurean, when you apply the keyword synchronized to a method, the whole method is synchronized using the corresponding object. So, perhaps we can go through your code above? after t1.start(), the main thread will be in the loop of line 1 above until bContinue is false. Meanwhile, thread t1 is executing the run() method of s, synchronized using s. Since the run() method is synchronized, thread t1 will first acquire the lock of object s - apparently nobody else hold the lock of object s at this point. Then, at line 3, in thread t1, the bContinue of master is set to be true. Now, since supposedly, both thread t1 and the main thread are running, after line 3, the loop in the main thread (line 1) will finish. The main thread, then, is trying to execute s.setPrice(200) - line 2. But, since setPrice is also synchronized, it requires the lock of object s, which is already acquired by t1. So, the main thread has to wait until the lock on object s is release before it enter method setPrice(200) of object s. Since, line 4-6 are infinite loop, the main thread is going to wait forever for the lock of s. So, if my analysis is correct, you will get print of lots of 100's (the initial value of iPrice), and never get the print of 200. Hope this helps.
|
 |
Ashok Paulraj
Ranch Hand
Joined: Jul 07, 2003
Posts: 78
|
|
thanks, u got it David !! ~ Shalini
|
 |
Sriram Chintapalli
Ranch Hand
Joined: Dec 16, 2003
Posts: 59
|
|
hi shalini, after the statement t1.start(); is executed the run method in Slave class gets executed by the thread t1 whereas your main thread is looping continuously here: while(bContinue==false) { } because both run() and setPrice() are synchronized methods of the same class if any thread enters one of the methods automatically all synchronized methods of that object ("s object" here) are locked by that thread which means no other thread(main thread here) can enter the synchronized methods using the same "object s". now the thread t1 sets the value of master.bContinue to true(master.bContinue=true so the main thread comes out of the above while loop and tries to execute s.setPrice(200); but remember out little s objects' synchronized method setPrice() is locked by the thread t1 in the run method and looping infinitely here: while(true){ System.out.println(iPrice); } So the thread t1 will be in the running state forever and never gives up the s objects' lock while the main thread will be in runnable state for eternity.
|
 |
 |
|
|
subject: synchronization: Clarify!!
|
|
|