aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread's question 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 "Thread Watch "Thread New topic
Author

Thread's question

garima jain
Ranch Hand

Joined: Sep 22, 2008
Posts: 60
Source : http://www.examlab.tk/



Output is ABC. My question is whether it will always be ABC or not as it might happen that the run method does not its turn until main is not finished. In that case, XYZ might get printed. Please clarify.
Rekha Srinath
Ranch Hand

Joined: Sep 13, 2008
Posts: 178
Garima,
I think you are right. There is also a chance that XYZ might get printed, if run() does not execute even after main() is done.
Lets hear what others have to say too.
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3100
    
  40

Garima,

When specifying the source, please feel kind to include the question number, so we can track it quickly. Well, your question is the Q.no 72 of Practice Exam 1, in mu simulator.

Practically, there would be a chance for printing "XYZ". But the probability of printing "XYZ" is less than 1%. I'll show you.

Can you see the sleep statement inside the main method? When the main thread is about sleeping for 1 second, the other thread can complete the run() method, unless a system stuck or the system runs on a very very slow performance. Therefore, the correct answer is Prints "ABC" (always).

You may see this type of questions on the real exam too. So be careful.

Cheers


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
Maleen Abeydeera
Greenhorn

Joined: Nov 10, 2008
Posts: 20
Devaka,
But a probability of 1% is still a probability . In the exam, we are supposed to choose the best answer . So if there's an answer called "Result is unpredictable", i think the exam requires you to select that. Am I right?

And I am sure I have seen other questions in this forum, with that kind of logic, but the answer as "unpredictable".

Need others' opinion on this.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

Yes Devaka, the right answer is that anything can be displayed. It's not necessary that ABC wil be displayed all the time. The output can be ABC or XYZ...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 791

I think Maleen and Ankit are correct
My opinion is "unpredictable result" (XYZ or ABC).

Let see other's opinion

Regards
Ninad


SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3100
    
  40

Originally posted by Maleen Abeydeera:
Devaka,
But a probability of 1% is still a probability . In the exam, we are supposed to choose the best answer . So if there's an answer called "Result is unpredictable", i think the exam requires you to select that. Am I right?

And I am sure I have seen other questions in this forum, with that kind of logic, but the answer as "unpredictable".

Need others' opinion on this.


Sorry to say that I don't agree with this. Because when I was doing the SCJP exam, I got two questions like this; they used a sleep method to identify that thread will be executed after the other thread. In these two questions, I didn NOT choose the "unpredictable result" answer; I choosed the answer, as I mentioned above. However I got 100% for Threads section, so I'm sure that my answer is correct. I know that this is a confus-able question; and that is the reason why I make a question like this in my simulator.

You are correct, if there was a statement like sleep(1). But the statement used there is sleep(1000). 1000miliseconds = 1second is a very very large duration to a computer processor.

You may don't agree with me, but remember that there will BE this type questions in the real exam, as I got. Anyway either you are correct or I'm correct, Sun expects the answer as I given above.

If you are not sure about this, please consult a Sun exam question creator. Thanks everyone for discussing this question.

Hope more comments.

Cheers
Kenneth Lomvey
Ranch Hand

Joined: Nov 08, 2008
Posts: 94
I didn't take the SCJP exam yet, so I don't know about Sun exam questions. However I agree with Devaka on this.

Just consider the following simple example:


class A{
public static void main(String args[]){
new A();
}
}


Assume following answers were given

A. Prints "A"
B. Prints nothing
C. Runtime error
D. Compile time error.
E. Result is unpredictable

What do you choose for this? I know that you will definitely choose the Answer B, as the correct answer.

But wait, why don't you choose Answer E for this? Confused? Well think about this, if there were no such memory in the heap, the exception OutOfMemoryError might be thrown! Can you say NO? You can't say no, because there IS a probability for throwing this exception, even though the probability is less than 1%. So, what about you? Do you select Answer E on the exam? No, you never select it. Because you know that Sun expects you to choose "Answer B" as the correct answer; so you WILL choose "Answer B". In this case, will ignore the probability which is less than 1%; and you know it is correct to ignore it. Isn't it?

Now, think about that Thread question in Devaka's simulator. In this case, you must ignore the probability which is less than 1%. So the correct answer should be, only "ABC" can be printed. So I agree with Devaka.

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

Well Devaka I think you must have had a question that had specifically mentioned something like this

"Assume that the sleep method takes the same time as given as parameter and normal code takes negligible time to execute, what will be the output"

I have seen these kind of questions and in that case the answer will be ABC in every case. Otherwise the answer will be ABC or XYZ.

Any comments??
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 791

Hello Devaka Cooray
I completely agree with your previous statement


in previous program it prints "ABC" 99% of time

step 1 : main thread start to execute

step 2 : main started a thread which is in runnable state but we don't know whether it is in running or not running mode it depends on JVM

step 3 : main execute sleep

step 4 : then assign "XYZ" String to class variable s

but there is a chance to execute step 4 and then started thread runs and prints "XYZ" this is also possible 1% of time

Devaka you are 100% perfect but what you say about printing of "XYZ" it is also possible

Hello Henry Wong please clear our doubt regarding this if you are online
we all need your help


Regards
Ninad
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3100
    
  40

Originally posted by Ankit Garg:

"...Assume that the sleep method takes the same time as given as parameter and normal code takes negligible time to execute, what will be the output..."


Ankit,

On the Sun exam, they given an assumption statement like that, for some questions. But I didn't include that assumption statement in this question, because...

I thought like this; we know that the sleep(long) method takes a duration, which is always higher than to the specified duration to be completed. In this question, I used a statement sleep(1000); so the user can assume it may take 1000 ms, 1001ms, 2000ms or probably 100000ms to complete (where ms=milliseconds). But it is not a problem to this question. Even though that statement takes morethan 30 seconds to be completed, the answer will be "ABC". Because of this, I didn't include the above assumption-statement.

However, now I think it's better to include an assumption statement like that, so it's more easy select an answer. I'll mark this and will include it on the next version. Once the next version is released, you will be notified via JavaRanch. Thanks for helping me to improve this simulator.

Cheers
Maleen Abeydeera
Greenhorn

Joined: Nov 10, 2008
Posts: 20
Well, I think if you run this program 1000 times, 999 times (or even 1000) you'll get "ABC". But for the SCJP exam takers (like me), what is important is the exam creators' interpretation of the problem. In KS&BB book (whom I think is on the exam development team) it is biased to the "unpredictable" answer. So I am naturally assuming that this is the official view.
If we can get this confirmed or refuted by somebody from Sun, it will be beneficial to all of us.
Chris Hurst
Ranch Hand

Joined: Oct 26, 2003
Posts: 416
    
    2

I don't think the new failing argument holds water in that you'ed expect it to always succeed.

There is no reason why some couldn't write a JVM that always gave the wrong answer and thats the issue.

Consider Oracle java running on a Oracle database that that last time I looked was single threaded ie you could write code that creatde more threads but only one actually ran at any time in that scenario it might be entitled to make the first thread complete before executing the child, though it might try to run the child first and then let the parent continue.

Ok thats a silly but there could be other reasons why a JVM might want to play with how it schedules threads.

Exam sample questions have a habit of pretending spurious thread wake up never happens ... eg Sun certified Programmer for Java 5 and Sun made greater use of that behaviour between Java versions and caught some code out.


"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
Bob Ruth
Ranch Hand

Joined: Jun 04, 2007
Posts: 320
I really REALLY get confused here so I hope that someone can help me out!

In the run method, there is a block that synchronizes on "this".

In the main method, there is a block that synchronizes on "td".

In the run method synchronization ... my interpretation of "this" is an instance of an XMap object.

In the main method synchronization ... the block appears to synchronize on an instance of a thread object. When the Thread object in main is instantiated it is true that we pass a reference to a new XMap object. But td itself points to a Thread object instance.

To ME it looks like these two are synchronizing on DIFFERNT objects which would make me think that there would be no synchronization at all so the print of "ABC" would occur every time.

I am going to go prototype this problem and print the value of "this" and td and if they are NOT the same I think this would back up my suspicion above. In the mean=time can someone shoot holes in my theory and if it is wrong, tell me where it is wrong. What am I not seeing correctly.

Thanks...
Bob


------------------------
Bob
SCJP - 86% - June 11, 2009
Kenneth Lomvey
Ranch Hand

Joined: Nov 08, 2008
Posts: 94
Yes, I think Bert Bates should post a comment here.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18842
    
  40

The reason schedulling (how to) is not specified, is because there are operating systems that doesn't do a very good at it. However, I don't know of any modern day operating systems that cannot schedule a time slice to a thread within a second -- especially when there are no other runnable threads in the application.

The reason a "spurious interruption" is allowed is because there are certain operating systems that don't do a very good job at delivering them. However, this is a very small program, that doesn't use interrupts. I can't imagine a spurious interrupt being sent.


So... IMHO, I highly doubt that you will ever see "XYZ", but I guess, on some OS, with a somewhat broken scheduller, that is under stress, it could happen.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18842
    
  40

To ME it looks like these two are synchronizing on DIFFERNT objects which would make me think that there would be no synchronization at all so ...


Yes, the synchronization are on two different objects -- so the two threads are not blocking on each other.

Henry
Bob Ruth
Ranch Hand

Joined: Jun 04, 2007
Posts: 320
Okay, I'm back from prototyping. I see Henry's response but, in the spirit of discovery, would like to post my findings. I have modified the original program in function but not in the basic architecture. All I modified was the code inside the block a little to demonstrate whether or not they were blocking one another.

Here is the code:



What happens here is that the two methods synchronize just as they did in the original code: the run method on "this" and the main method on "td". I have each print it's object just to see the indication.

Main creates a new thread object and then synchronizes on it.... the run method just synchronizes on this BUT it will not do that until started so the thread is started. So, initially, main() wins the lock. I'm not sure why I did a timed wait() but I did so it times out and returns with the lock. It prints out it's object, starts the thread, and enters a "forever loop" where it prints out it's identity and sleeps. Sleep does not release the lock so main() holds the lock for as long as it runs.

Once started, the run method is entered which prints out it's identity and then goes into it's "forever loop".

Once compiled this prints the main identity, pauses 3 seconds, prints the second threads object identity, then goes into a loop where both messages come out on a one second period.

That means, they are not synchronizing on the same object for certain or you would never get messages from the second thread.

I guess this should have been clear from the beginning though since the original code synchronizes....then sleeps (which does not release the lock) and then changes the string before exiting it's own synchronized block. If they were locking the same lock you would never even see "ABC".

Thanks, Henry. I know you answered this but I just wanted to show the results of the prototyping that I did in hopes it might help somebody out there.
Divya Bhatt
Greenhorn

Joined: Nov 14, 2008
Posts: 1
public class XMap2 implements Runnable{
static String s="ABC";
public void run(){
synchronized(this){
System.out.println("run thread");
try{
Thread.sleep(1000);//1
System.out.print(s);
}catch(InterruptedException e){}
}
}

public static void main(String args[])throws Exception{
Thread td=new Thread(new XMap2());
synchronized(td){
td.start();
System.out.println("main thread");
Thread.sleep(1000);
s="XYZ";
}
}

}

We certainly can have the XYZ answer if we add line..//1
[ November 14, 2008: Message edited by: Divya Bhatt ]
Kenneth Lomvey
Ranch Hand

Joined: Nov 08, 2008
Posts: 94
Thanks Henry,

According to your post, I think that the given answer (always prints "ABC") is correct.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread's question