File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question about thread output from examlab practice test 1 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "question about thread output from examlab practice test 1 " Watch "question about thread output from examlab practice test 1 " New topic
Author

question about thread output from examlab practice test 1

Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 446
    
    1

I am not able to understand how the program flow is
means
I don't know how the method td.start() is in the syschrinized context of the thread td
also how does the thread goes into the synchronized state even if we are not starting the thread
we are starting the thread in the synchronised context
then
how does the td thread runs the synchronised context before even starting it ?
please help me
here is the code


and the output is
ABC


SCJP 6 [86%], OCPWCD [84%], OCEJPAD [83%]
If you find any post useful, click the "plus one" sign on the right
Larry Olson
Ranch Hand

Joined: Feb 03, 2009
Posts: 142
Hi,

OK, when



is executed, all it does is to create a (thread) object (it is not yet a thread of execution). This thread object could be used like any other object for synchronization purposes. So all synchronized(td) means is that the main thread now enters a synchronized block and it has to acquire the lock on the 'td' object before it can proceed further. This has nothing to do with the 'td' thread itself. Here we are looking at 'td' as a thread object and not a thread of execution.

But once the main thread enters the synchronized block, then it starts the td thread and td becomes a thread of execution. From this point onwards the two threads could execute in parallel with each other, with no predictable sequence (since we can't predict how the thread scheduler will execute the main thread and the td thread). The td thread itself has a synchroinzed(this) in its code which means it is synchronized on the XMap() instance on which it is executing.

It just happens that the main thread has a Thread.sleep(1000), in which case it just happens we get the output ABC. But XYZ is also a possible result, even though you might not see that happen frequently. Try commenting out the sleep code and you could see that XYZ is a possible output.
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 446
    
    1

ok
when the statement
synchronised(object){

}
encounters, I was thinking that
"the code is synchronized on the object and nothing can enter this code till the execution of object gets completed "

possibly I was wrong here itself
according to your explanation, i came to know that code acquires the lock of the object and then executes the following code
if I am wrong, please correct me
I came to know following points
main thread takes the lock of the object td and then starts executing next code
in that
td.start() is there hence run method also gets called
am I right now?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question about thread output from examlab practice test 1