• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

synchronize

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}
}
}
**********************************************
 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Blake Minghelli
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Blake Minghelli
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic