aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread" Watch "Thread" New topic
Author

Thread

cyril vidal
Ranch Hand

Joined: Jul 02, 2003
Posts: 247
Dear all,
Question n�19 from Dan's (third part) devoted to threads:

Answer for possible outputs:

Prints: [T1,A][T2,B].
c. Prints: [T1,B][T2,B].
d. Prints: [T2,B][T1,A].
e. Prints: [T2,A][T1,A].
Since method m1 is not synchronized

As far as I understand, here the modifier static for object b in class A is very important: it means that the same object B is shared between the two threads a and b.
But if we delete this keyword in the code, now each thread has its own object b, and there is no more reason that we obtain a different result than
[T1,A][T2,B]

I've tested it, and on my computer, that's indeed the case.
But may we generalize?
Thanks in advance for your answer,
Cyril.


SCJP 1.4, SCWCD, SCBCD, IBM XML, IBM Websphere 285, IBM Websphere 287
Miki Muzsi
Ranch Hand

Joined: Jun 23, 2003
Posts: 120
If you remove the static keyword, indeed there are two diffeerent instances of B created. This garantees that you can not get output like:
[T1,B]
or [T2,A]
However you will not have garantee that the output is always: [T1,A][T2,B]. It can be [T2,B] [T1,A] as well, since no control (garantee) how thread scheduler will do things.
Miki


Miki<br /> <br />SCJP 1.4, SCBCD 1.3
cyril vidal
Ranch Hand

Joined: Jul 02, 2003
Posts: 247
That's right!
Thanks Miki for your answer.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread