Two Laptop Bag*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Chicks - Synchronization question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Chicks - Synchronization question" Watch "Chicks - Synchronization question" New topic
Author

Chicks - Synchronization question

O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

I did a search on this question and found quite a few answers but none are specific to the query i have. All examples below are from K&B.

In the following example question:




Which are true? (Choose all that apply.)
A. Compilation fails
B. The output could be 4 4 2 3
C. The output could be 4 4 2 2
D. The output could be 4 4 4 2
E. The output could be 2 2 4 4
F. An exception is thrown at runtime
Answer:
® 3 F is correct. When run() is invoked, it is with a new instance of ChicksYack and c has
not been assigned to an object. If c were static, then because yack is synchronized, answers
C and E would have been correct.
®˚ A, B, C, D, and E are incorrect based on the above. (Objective 4.3)


The book states that there will be a NPE because c has not been initialised. It then goes on to say that if c were static, then because yack is synchronised, answers C and E would have been correct.
I don't understand how C and E would have been correct even if c was declared as static. I feel that C and E would have been correct if the 'yack' method was declared as static not the instance variable c.

As an example, the following code snippet output 1 and 1. Not 1 and 2 which would have been the case if the two TestClass instances were refering to the same x variable.



Please clarify this.

Thanks
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3573
    
  14

You say you don't understand how the answer could be C and E, but you don't tell us why. What's wrong, what other possibly outcomes could there be, or why wouldn't it work?
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

I think the reason i dont understand is possibly because i might not fully understand the difference between a static primitive variable and a static instance variable. I think that C and E will only be correct if the method yack was static - not the instance variable c.
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

Sorry. Actually i meant the difference between a static instance variable and a static method.

In the above example, a different instance of ChicksYack is used in each of the threads. This means that each thread will have its own copy of 'c'. My question is what would have happened if 'c' was declared static and also would this have been different if 'c' was not static but the method 'yack' was static?

If 'c' was declared as static and the method 'yack' was synchronised (but not static), which locks would the two individual threads use?

Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3573
    
  14

Small nitpick, there's no such thing as a static instance variable. It's an instance variable (field), or a static variable.

Let's start with the hypothetical situation that the book presents. If c was declared static, it means all three instances of ChicksYack have a reference to the same Chicks object: the one created in the go() method.

When the two threads call the yack() method, one of them has to wait because the other entered the synchronized block. After the method has finished running, the other thread enters the block. So the program could print either C or E.

The same thing would happen if the yack method() was static, instead of the c field. No error would occur, because the method is static, and you can call static methods on null, as long as the type is appropriate.
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

Stephan van Hulst wrote:Small nitpick, there's no such thing as a static instance variable. It's an instance variable (field), or a static variable.

Let's start with the hypothetical situation that the book presents. If c was declared static, it means all three instances of ChicksYack have a reference to the same Chicks object: the one created in the go() method.

When the two threads call the yack() method, one of them has to wait because the other entered the synchronized block. After the method has finished running, the other thread enters the block. So the program could print either C or E.

The same thing would happen if the yack method() was static, instead of the c field. No error would occur, because the method is static, and you can call static methods on null, as long as the type is appropriate.


Stephan, if c was declared static, does it mean all its members are also static even if they are not declarared as static? An example of this is



The members of class A are not static. Does the fact that a in class B is declared as static mean that all of a's members are also static?
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3573
    
  14

No you can't do this. The static keyword simply means that a particular member, be it a field or a method, is part of the class, and not part of the instance. The particular Chicks object you assign to c does not suddenly have all its members declared static. All that happens is that the c *variable* that holds the reference can be accessed through the ChicksYack class, and not through an instance of ChicksYack.

When c isn't declared static, the program will throw an exception because the two new ChicksYack objects hold a null reference in their own c variable. When c is declared static, they don't have a c variable to begin with. The c variable is part of the class. All ChicksYack instances have access to it. So if c is initialized in one part of your program, the same c is available everywhere else. So in this version, the two different ChicksYack objects are calling the yack() method on the *same* Chicks object. This is why synchronization works.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Chicks - Synchronization question
 
Similar Threads
Thread synchonization
Using non-static object as lock in Threads PRoblem
SCJP 1.6 ...Threads. Needs clarification of a Self Test ans from K&S Book
Threads question.
Multithreading questions