aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread's Beta Version (Dan) 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 Beta Version (Dan)

Shishio San
Ranch Hand

Joined: Aug 29, 2002
Posts: 223
Hi Dan,
I think that the following comment about question 14 needs to emphasize the fact that the yield method gives only threads of the same priority a chance to run. If there are no equal priority threads that are runnable, then the yield is ignored.

The thread.yield method is intended to cause the currently executing thread to move from the running state to the ready state and offer the thread scheduler an opportunity to allow a different thread to execute based on the discretion of the thread scheduler. The thread scheduler may select the same thread to run immediately or it may allow a different thread to run. The thread.yield method is a native method so the behavior is not guaranteed to be the same on every platform.


Whatever doesn't kill us ...<br />Is probably circling back for another try.<br />SCJP 1.4
Shishio San
Ranch Hand

Joined: Aug 29, 2002
Posts: 223
In question 26 the following code does produce a runtime error: java.lang.IllegalThreadStateException but still prints A. May be you should change the answer to this question to b and e then.
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Shishio,
Thank you for pointing that out. The question is intended to point out that the start method can not be invoke twice on the same thread without generating an exception. I did not consider the possibility that the thread might run to completion before the second attempt is made to invoke the start method.
I just uploaded the following change.

The answer is "e. Run time error".
The new remark associated with the answer is as follows.

The second call to a1.start will always cause an IllegalThreadStateException to be thrown. It is also possible that thread a1 might start running immediately causing the letter A to be printed before the second attempt is made to start thread a1. Although thread a1 might sometimes run to completion before the exception is thrown this result will not always occur.

How do you feel about this new version?


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Originally posted by Shishio San:
Hi Dan,
I think that the following comment about question 14 needs to emphasize the fact that the yield method gives only threads of the same priority a chance to run. If there are no equal priority threads that are runnable, then the yield is ignored.

I actually did emphasize that point when I developed the first version of that question. Later, someone pointed out that that behavior is not guaranteed across all virtual machines. At that time I looked around for some form of guarantee but I did not find one. For that reason, I removed the answer related to threads of lower priority.
Could you please post a link to the source of information that mentions the limitation concerning lower priority threads?
Shishio San
Ranch Hand

Joined: Aug 29, 2002
Posts: 223
Hi Dan

How do you feel about this new version?

I think this version is much better

Could you please post a link to the source of information that mentions the limitation concerning lower priority threads?

You can check that out using the following link. It's mentionned rigth before the Summary
http://java.sun.com/docs/books/tutorial/essential/threads/priority.html
if you decide still to keep the comment the way it is then in question 18, the following statement should not be true
h. The Thread.yield method will not yield to a thread of lower priority.
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Shishio,
Thank you for posting that link. I knew that I had read that some place, but I didn't remember where. It is nice to see that it is published on the Sun web site. However, that behavior is not described in the API Specification for the yield method. Instead, the API Specification only says that the yield allows other threads an opportunity to run. For that reason, I would like to keep the statements in the exam less absolute.
Question 14 is now as follows.

Question 14
Which of the following is true?
a. The Thread.yield method might cause the thread to move to the blocking state.
b. The Thread.yield method might cause the thread to move to the ready state.
c. The same thread might continue to run after calling the Thread.yield method.
d. The Thread.yield method is a static method.
e. The behavior of the Thread.yield method is consistent from one platform to the next.
f. The Thread.sleep method causes the thread to move to the blocking state.
g. The Thread.sleep method causes the thread to move to the ready state.
h. None of the above.

The answer is b, c, d, f.
The remark is as follows.

The thread.yield method is intended to cause the currently executing thread to move from the running state to the ready state and offer the thread scheduler an opportunity to allow a different thread to execute based on the discretion of the thread scheduler. The thread scheduler may select the same thread to run immediately or it may allow a different thread to run. The thread.yield method is a native method so the behavior is not guaranteed to be the same on every platform. However, at least some implementations of the yield method will not yield to a thread that has a lower priority.
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Question 18 has been updated as follows.

Question 18
Which of the following are true?
a. The Thread.run method is used to start a new thread running.
b. The Thread.start method is used to start a new thread running.
c. The Runnable interface declares the start method.
d. The Runnable interface declares the run method.
e. The Thread class implements the Runnable interface.
f. If an Object.notify method call appears in a synchronized block, then it must be the last method call in the block.
g. No restriction is placed on the number of threads that can enter a synchronized method.
h. Some implementations of the Thread.yield method will not yield to a thread of lower priority.
i. None of the above.

The answer is b, d, e, h.
The new remark associated with the answer is as follows.
The Object.notify method can only be called from within a synchronized block by the thread that holds the monitor for that object. Suppose that thread T1 is holding the monitor for an object, A, within a synchronized block. If thread T1 calls the notify method immediately after entering the synchronized block, then no other thread can grab the monitor for object A until T1 leaves the synchronized block. For that reason, the transfer of control from thread T1 to any waiting thread can not be accelerated by moving the notify method to an earlier point in the synchronized block. The behavior of Thread.yield is platform specific. However, at least some implementations of the yield method will not yield to a thread that has a lower priority. Invoking the Thread.yield method is like offering a suggestion to the JVM to allow another thread to run. The response to the suggestion is platform specific.

How do you like the new version of questions 14 and 18?
Shishio San
Ranch Hand

Joined: Aug 29, 2002
Posts: 223
Hi Dan,
I think it's much better. I was wondering though about the real exam. How should we react to such a question if nothing is mentionned about the platform's specific behavior of yield !?
Thx for your effort Dan
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Originally posted by Shishio San:
Hi Dan,
I think it's much better. I was wondering though about the real exam. How should we react to such a question if nothing is mentionned about the platform's specific behavior of yield !?
Thx for your effort Dan

The behavior described in the API Specification is the only thing that you can count on.

Causes the currently executing thread object to temporarily pause and allow other threads to execute.

The API is very vague because the implementation is platform dependent. The lesson that should be learned is that the behavior of the Thread.yield method is not guaranteed. Although it can be used to enhance the cooperation of threads, it can not be used to guarantee the cooperation of threads. That's why I included the following comment in the remark associated with the yield question.

Invoking the Thread.yield method is like offering a suggestion to the JVM to allow another thread to run. The response to the suggestion is platform specific.

In summary, the Thread.yield method should be used to enhance the operation of your program but should not be used to guarantee the operation of your program.
[ October 07, 2002: Message edited by: Dan Chisholm ]
Shishio San
Ranch Hand

Joined: Aug 29, 2002
Posts: 223
Hi Dan
Okidokie sounds right.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread's Beta Version (Dan)