aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Please help clear my doubts. 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 "Please help clear my doubts." Watch "Please help clear my doubts." New topic
Author

Please help clear my doubts.

Hrishikesh Yeshwant Alshi
Ranch Hand

Joined: Dec 06, 2010
Posts: 62

I am using Enthuware 1.4 (I know its pretty old but that what I have with me right now.). Below I are the questions from the same...

Question I:


It says that This program will print "Main Returns". I believe that it is NOT GUARANTEED. Yeah 99 times out of 100 it might but as per my knowledge there is no guarantee. Am I right?

Write your code as if the person who maintains it is a homicidal maniac who knows where you live.
OCPJP6/SCJP6 86%
Hrishikesh Yeshwant Alshi
Ranch Hand

Joined: Dec 06, 2010
Posts: 62

This question is posted a new topic as suggested.
Hrishikesh Yeshwant Alshi
Ranch Hand

Joined: Dec 06, 2010
Posts: 62

This question is posted as a new topic as suggested.
Hrishikesh Yeshwant Alshi
Ranch Hand

Joined: Dec 06, 2010
Posts: 62

This question is posted as a new topic as suggested.
Hrishikesh Yeshwant Alshi
Ranch Hand

Joined: Dec 06, 2010
Posts: 62

This question is posted as a new topic as suggested.
Hrishikesh Yeshwant Alshi
Ranch Hand

Joined: Dec 06, 2010
Posts: 62

I am really sorry for flooding so many questions. Please help clear my doubts.
Many thanks in advance. Looking for the help.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

Please UseAMeaningfulSubjectLine. And one question in one Thread. This way we can answer your questions easily.


Mohamed Sanaulla | My Blog
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Hrishikesh Yeshwant Alshi wrote:
It says that This program will print "Main Returns". I believe that it is NOT GUARANTEED. Yeah 99 times out of 100 it might but as per my knowledge there is no guarantee. Am I right?


There are two threads, did you execute the code? then main thread will execute those println() statements. The code won't complete normally because of the endless loop in a thread.


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Hrishikesh Yeshwant Alshi
Ranch Hand

Joined: Dec 06, 2010
Posts: 62

Abimaran Kugathasan wrote:

There are two threads, did you execute the code? then main thread will execute those println() statements. The code won't complete normally because of the endless loop in a thread.


Yeah I know, because the other thread is a non-daemon thread JVM will never shut-down. That's fine. But my question is IS IT GUARANTEED that the main method will print "main returns"? I mean on some specific implementations of JVM the other thread might not give chance to the main thread EVER. So it is NOT GUARANTEED that the program will print "main returns". Is my understanding correct?

There this gives birth to the other question that how to deal with such questions in real exam?
Ravi Khadgi
Greenhorn

Joined: Aug 06, 2010
Posts: 20

There are two threads running. One is the "Main" thread and the other is "TestClass" thread.
Main method is running inside the "Main" thread whereas the infinite for loop is running
inside the "TestClass" thread. The JVM switches between the threads using some algorithm.
When "Main" thread gets chance to run, it will execute line 11(prints out "Main returns").
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18715
    
  40

Hrishikesh Yeshwant Alshi wrote:But my question is IS IT GUARANTEED that the main method will print "main returns"? I mean on some specific implementations of JVM the other thread might not give chance to the main thread EVER. So it is NOT GUARANTEED that the program will print "main returns". Is my understanding correct?

There this gives birth to the other question that how to deal with such questions in real exam?


I think that the best way to deal with the real exam is to be reasonable. Is it possible that the JVM will starve one thread -- when two equal priority threads are running? Sure, anything is possible. Is it probable that the JVM will do it? Probably not.

Maybe if you go back 15 years, to get a green threads implementation of the JVM. And if you time it right, you can starve it. But even then, I wouldn't hold my breath on it.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Hrishikesh Yeshwant Alshi
Ranch Hand

Joined: Dec 06, 2010
Posts: 62

Wow. In mock exams I missed many questions just because of being "unreasonable". I always thought what IS guaranteed and what ISN'T. In K&B book also it is mentioned that, in Thread examples, exam WILL test your knowledge about what IS guaranteed and what ISN'T. So I never got into the paradigm of being "reasonable".

It is great to know that I can be "reasonable". Thanks much Henry. This surely helped.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Please help clear my doubts.