File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please help clear my doubts.

 
Hrishikesh Yeshwant Alshi
Ranch Hand
Posts: 62
Eclipse IDE Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Hrishikesh Yeshwant Alshi
Ranch Hand
Posts: 62
Eclipse IDE Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question is posted a new topic as suggested.
 
Hrishikesh Yeshwant Alshi
Ranch Hand
Posts: 62
Eclipse IDE Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question is posted as a new topic as suggested.
 
Hrishikesh Yeshwant Alshi
Ranch Hand
Posts: 62
Eclipse IDE Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question is posted as a new topic as suggested.
 
Hrishikesh Yeshwant Alshi
Ranch Hand
Posts: 62
Eclipse IDE Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question is posted as a new topic as suggested.
 
Hrishikesh Yeshwant Alshi
Ranch Hand
Posts: 62
Eclipse IDE Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3159
33
Google App Engine Java Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseAMeaningfulSubjectLine. And one question in one Thread. This way we can answer your questions easily.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Hrishikesh Yeshwant Alshi
Ranch Hand
Posts: 62
Eclipse IDE Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Hrishikesh Yeshwant Alshi
Ranch Hand
Posts: 62
Eclipse IDE Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic