• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

A question of Thread from Sun's Assessement test  RSS feed

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a problem with a question from Sun's Assessment test:



I choose e f, but this answer is wrong.
I am pretty sure about answer e, the reason is:
1. after a new thread "t" started, the main thread continue, print: pre
2. thread "t" call join, so main thread has to wait "t" finished, so print "in" first, then print "post"

so answer can be: pre in post

For another answer, I am not sure if i am right:
1, suppose thread "t" call t.join() failed, so "in" will be print out after "post"

as the result, I choose answer f: pre post in

but this is not correct, please tell me which is the another right answer? thanks a lot
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer would be in pre post OR pre in post

For detailed understanding check the below link:
http://www.coderanch.com/t/270074/java-programmer-SCJP/certification/output
 
Maggie Zhou
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot. I searched , but used wrong key word. Thanks again.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

From what I can see the output "pre in post" is very intuitive and undisputed, the problem is how is the output: "in pre post" possible?

I know that this must be infuriatingly simple to some, but I would like to make certain that I understand the whys behind this output. I have appreciated the time and effort that various members have put into clearing this up, but, unfortunately, the issue remains cloudy for me. I have come up with a very specific scenario under which the output could be created.

If it is correct please let me know. If it is incorrect, please respond in
kind, with a very specific description of the Threads entering and exiting states and which rules are controlling their behaviors.

Let me start out by establishing a fact:

K&B 1.5 pg 699 says that sleep() is guaranteed to cause the current Thread to stop executing for at least the specified sleep duration.



So, as I understand things, for the String "in" to be printed first the following must happen:

The JVM select the Thread t to be executed first. It then encounters the sleep(2000) call. According to the above guarantee, the Thread must stop executing for 2 seconds. (Now this is where the part that is hard to believe happens.) Under some implementation of the JVM, though there are only 2 Threads and one of them is asleep, as part of the no guarantees clause, we cannot say for certain that the main Thread will get a chance to execute at all during the 2000 millisecond period that Thread t spends in the waiting/blocking/sleeping state. The main thread just sits in the runnable pool for 2000+ milliseconds while Thread t sleeps. Thread t then awakens, joins the Runnable pool and is selected to enter the Running state. Thread t prints "in" and its run method completes and it enters the dead state. The main Thread (now the only Thread in the Runnable pool) finally gets to run. It prints "pre". It then attempts to join the now dead Thread t.

(Interpreted from K&B 1.5 pg 700 paragraph 1: You can join a dead (completed) Thread without getting an error. The current executing Thread just doesn't "back out" of executing.

Since t is dead, the main Thread just keeps executing and prints "post".

I think the reason that the previous chain of posts may have dragged on so long was that this scenario seems implausible. I will accept it, however, on faith in the "no guarantees" principle if someone authoritative will confirm the details. I respectfully request that you do not post a reply to this unless you have carefully read what I have written above.

Thanks in advance for your for your help.
 
Maggie Zhou
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now I am pretty sure the answer would be:
pre in post
or
in pre post

1, the post will always be the end, due to the join(); the reasons are:
--> if the join() is success: means t thread is still alive, and join let the main thead execute after the t thread finished
--> if the join() is failed: mean t thread is already finished, and join throwed a exception which is catched by program, them main thread print the post

2, pre and in can be different order; the reasons are:
--> that is possible, the main threa print pre, then wait t thread due to the join() has been called
--> that is also possible: the t threa is executed before main thread, so the in will be printed before pre.

I hope this can help some one understand this question.
I think doing the thread question, you should seperate which part is 100% assuring, which part is not that assuring
 
So I left, I came home, and I ate some pie. And then I read this tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!