aspose file tools*
The moose likes Threads and Synchronization and the fly likes synchronize Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "synchronize" Watch "synchronize" New topic
Author

synchronize

Tina Shupe
Greenhorn

Joined: Aug 05, 2002
Posts: 6
I am having issues with understanding synchronized blocks. I thought if a block was synchronized the thread that was accessing it has control until it is finished with that block of code.
Why does it print:
first0
second0
first1
second1
first2
second2
instead of:
first0
first1
first2
second0
second1
second2
*********************************************
public class Holt extends Thread {
private String sThreadName;
Holt(){}
Holt(String s){
sThreadName = s;
}
public String getThreadName(){
return sThreadName;
}
public void go(){
Holt first = new Holt("first");
first.start();
Holt second = new Holt("second");
second.start();
}
public static void main(String args[]){
Holt h = new Holt();
h.go();
}
public synchronized void run(){
for(int i=0; i < 3; i++){
System.out.println(getThreadName() + i);
}
}
}
**********************************************
Blake Minghelli
Ranch Hand

Joined: Sep 13, 2002
Posts: 331
The reason why it works like that is because when you create your 2 Holt threads, you are creating 2 separate instances of Holt. Synchronizing an instance method (non-static, like run()) only works if two or more threads are accessing the same method within the same instance. The way you have your program written, each Holt thread is executing its own "copy" of the run() method, therefore synchronizing does no good.
I think your confusion boils down to the fact that each instance of an object has it's own "copy" of its "instance" methods/fields. You might be confusing it with static ("class") method/fields that all objects of a class share.
Hope that hepls


Blake Minghelli<br />SCWCD<br /> <br />"I'd put a quote here but I'm a non-conformist"
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
It's synchronized with respect to its own instance variables.
But you have two instances, each with its own lock. They aren't synchronized with respect to each other.


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Blake Minghelli
Ranch Hand

Joined: Sep 13, 2002
Posts: 331
Check this out... I added a static method to your Holt class to illustrate. You can try adding/removing the "synchronized" keyword in doWork() to see how the results change.

In this code, you'll get the results you expected because the doWork() method is static and therefore shared by all instances of Holt, and therefore synchronizing works.
I also don't want to confuse you into thinking that synchronized methods must be static... more to come...

[ October 17, 2002: Message edited by: Blake Minghelli ]
Oops, my original post left "synchronized" in the run() method. I removed it
[ October 17, 2002: Message edited by: Blake Minghelli ]
Leslie Chaim
Ranch Hand

Joined: May 22, 2002
Posts: 336
Tena,
If you are posting code please use the code tags. It simply easier to read.
Anyway, first, here is the code which would do what you wish:

See how much nicer it is and its really easy
From your question it appears to me that you need better understanding of what is a Thread and what is a thread. A Thread is an Object while thread is a path of execution. Synchronization comes to play with threads.
BTW, for many reasons it not a good idea to synchronize run()
Check out Paul Hyde's book, he really takes a step-by-step spiral approach and its great.
[ October 17, 2002: Message edited by: Leslie Chaim ]

Normal is in the eye of the beholder
Blake Minghelli
Ranch Hand

Joined: Sep 13, 2002
Posts: 331
Not to beat a dead, one-eyed moose, but here is another example. In this example, there are two threads trying to execute the same method of a shared object...
Tina Shupe
Greenhorn

Joined: Aug 05, 2002
Posts: 6
this has helped me out A LOT. I have tested the code that everyone has provided and it helps. This has been great.
I still need to mess around with Threads and read up some more.
BTW - Next time I will put code brackets I just wasn't paying attention.
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

Another good helpful thing would be to print the object name itself.
print(this);
That way you get the Objects ID and you can tell that they are two different objects.
currently you can replace this

with this

and achieve the same results.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: synchronize