Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

static synchronized method

 
tanu dua
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Output: 6 put followed by 6 get

Output: 2 put 2 get 4 put 4 get.

The only differnce in the above 2 progs is that first version has non-static synchronized put method
however second version has static synchronized method.

Problem: Why the output differs?

Thanx
Waiting someone to reply.

[ edited to preserve formatting using the [code] and [/code] UBB tags -ds ]
[ July 13, 2004: Message edited by: Dirk Schreckmann ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A synchronized method needs an object to synchronize on. When a synchronized method (or block) is entered, it gets the "monitor" on this object, which it releases when the method is exited. Only one thread can have a monitor on an object at a time.

A non-static method uses "this" as the object to synchronize on. For static methods, there is no "this", they use the class object instead (Q.class in your example).

That's why a non-static and a static synchronized method won't ever block each other.

Hope this helps...
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When posting code, please be sure to surround the code with the [code] and [/code] UBB Tags. This will help to preserve the formatting of the code, thus making it easier to read and understand.

Moving this to the Threads and Synchronization forum...
 
tanu dua
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx for ur replies !!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!