my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads-synchronized access 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 "Threads-synchronized access" Watch "Threads-synchronized access" New topic
Author

Threads-synchronized access

Rekha Srinath
Ranch Hand

Joined: Sep 13, 2008
Posts: 178
Source: http://nikojava.wordpress.com/2008/09/08/scjp-mock-exam-for-threads/



Question: Just before the main method exits, in the place marked '//here' , the account�s number field is guaranteed to have value 2000. Is this true or false ?

The answer given is false.

I thought the answer is true, because at //here, the two threads have completed. And in each thread's run(), the loop has to get completed, and one of the threads will finally change the 'number' value to 2000. What am I missing here?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

Originally posted by Rekha Srinath:
at //here, the two threads have completed.


you are wrong here. when //here is reached, it is not mandatory that the two threads will be stopped. If you modify the code as the below, then it is guaranteed that the account�s number field is guaranteed to have value 2000.



SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Rekha Srinath
Ranch Hand

Joined: Sep 13, 2008
Posts: 178
Yes I got it Ankit...Thanks..

That was a quick reply...
Rekha Srinath
Ranch Hand

Joined: Sep 13, 2008
Posts: 178
Ankit,

I had another query seeing the following code of yours:



Can i say that the above code ENSURES that c1 WILL complete first and then followed by c2?
[ November 03, 2008: Message edited by: Rekha Srinath ]
Graeme Jenkinson
Greenhorn

Joined: Sep 08, 2008
Posts: 27
Originally posted by Rekha Srinath:
Ankit,

I had another query seeing the following code of yours:



Can i say that the above code ENSURES that c1 WILL complete first and then followed by c2?

[ November 03, 2008: Message edited by: Rekha Srinath ]


I've only skimmed the thread so I may be wrong, but no you can't say anyhting about whether c1 or c2 completes first or last; this is solely in the hands of the scheduler.
raju ansari
Greenhorn

Joined: Oct 28, 2008
Posts: 7
--------------
c1.join();
c2.join();
--------------
I don't think it will guarantee that c1 will finish before c2, because c1.join() ensures that the current thread(main thread) will start once c1 finishes. Although we can have c2 finished before c1. It does not related c1 and c2 thread however it ensures main thread with c1 and c2 thread.

Please comment.


Regards,<br />Raju Ansari
Rekha Srinath
Ranch Hand

Joined: Sep 13, 2008
Posts: 178
All,

c1.join() - This means "Hey main thread !! Pause till c1 is complete"... So, after this call, main() is suspended, which means that it cannot proceed to the next statement which is c2.join().... So, does it not indirectly indicate that c1 has to complete first? In other words, the order is as follows:

(1) main() is running
(2) c1.join() called--main() suspended, till c1 completes, so that main() can join c1
(3) c1 completes--main() joins c1, i.e. main() comes out of suspended state, and starts running again
(4) c2.join() called--main() again suspended, till c2 completes

So, c1 has to complete before c2... Correct me if I am wrong.
raju ansari
Greenhorn

Joined: Oct 28, 2008
Posts: 7
We can not guarantee the order of c1 and c2 thread completion.
------------------------------------------------------------------
(1) main() is running
(2) c1.join() called--main() suspended, till c1 completes, so that main() can join c1
(3) c1 completes--main() joins c1, i.e. main() comes out of suspended state, and starts running again
(4) c2.join() called--main() again suspended, till c2 completes
-------------------------------------------------------------------------
1,2 is absolutely right, but 2 does not say anything about the relation between c1 and c2 thread. Both c1 and c2 run independently(switching between runnable and running state by JVM scheduler).

It is possible that c2 thread finishes before c1 thread, in this scenario main thread still wait for c1 thread. once c1 finishes it execute c2.join() which will not suspend main thread because c2 has already finished.

If c1 finishes before c2, then suspended main thread resume and execute c2.join() which causes main to suspend again to wait for c2 thread completion.
Rekha Srinath
Ranch Hand

Joined: Sep 13, 2008
Posts: 178
Yes, I got it Raju..Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads-synchronized access