File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Can someone explain this question to me? please Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Can someone explain this question to me? please" Watch "Can someone explain this question to me? please" New topic
Author

Can someone explain this question to me? please

Andrew Symantec
Greenhorn

Joined: Apr 24, 2003
Posts: 15

The above question is excerpted from mock green's SCJP exam. The resut of this question is "ABC".
I don't understand it, since the instance of class A is already created by using new A(sa),
and the array is alredy passed in, the constructor of A should initialize its own string array array. How could the value be changed?
Thank you very much in advance.
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
Well, you only have 1 sa object. Both objects A and B have their own reference to that one object but, there can be only one.
Now, the real point of this is to show how using the synchronized keyword affects the behavior in multithreaded applications. Since B has synchronized on sa, the Thread A you start can not perform operations on the sa object until B releases it (ie the synchronized block ends).


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Andrew Symantec
Greenhorn

Joined: Apr 24, 2003
Posts: 15
Right, I understand it now.
Thanks Carl
BTW, you are not from Austria, are you?
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1158

Their are probably several points tied into what you said.

1. Passing an array reference seems different than passing a regular reference. The assignment being done is to what the reference referrs, and not to the reference. So the reference keeps its value, but what the reference points to is changed. Sort of like a pointer to a pointer if you are from a pointer coding background.
2. Though the thread is started it can not proceed until the original thread releases the lock that it needs. This often burns people when they start using SwingUtilities InvokeLater but fail to grasp that point.
3. Ugh, I can't remember the 3rd point
 
 
subject: Can someone explain this question to me? please
 
Threads others viewed
Thread Question
Question on Threads from Dan's topic exam
Question on Locks
Threads - synchronized methods
Threads again
developer file tools