aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes thread problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "thread problem" Watch "thread problem" New topic
Author

thread problem

dx wu
Ranch Hand

Joined: Apr 28, 2002
Posts: 60
I have two question,thanks to someone to help me.
1.Which two CANNOT directly cause a thread to stop executing? (Choose Two)
A.Existing from a synchronized block
B.Calling the wait method on an object
C.Calling notify method on an object
D.Calling read method on an InputStream object
E.Calling the SetPriority method on a Thread object

2.Which two CANNOT directly cause a thread to stop executing? (Choose Two)

A.Calling the yield method
B.Calling the wait method on an object
C.Calling the notify method on an object
D.Calling the NotifyAll method on an object
E.Calling the start method on another Thread object


good good study , day day up!
kay lin
Ranch Hand

Joined: May 20, 2004
Posts: 132
Originally posted by dx wu:
I have two question,thanks to someone to help me.
1.Which two CANNOT directly cause a thread to stop executing? (Choose Two)
A.Existing from a synchronized block
B.Calling the wait method on an object
C.Calling notify method on an object
D.Calling read method on an InputStream object
E.Calling the SetPriority method on a Thread object

I think the answer is A and E


2.Which two CANNOT directly cause a thread to stop executing? (Choose Two)

A.Calling the yield method
B.Calling the wait method on an object
C.Calling the notify method on an object
D.Calling the NotifyAll method on an object
E.Calling the start method on another Thread object


I think the answer is A and B

anybody else?
Sunyaluk Boonmas
Greenhorn

Joined: Apr 18, 2005
Posts: 24
I am not sure.

1. ans A,C
A.Existing from a synchronized block
//no reason for stop thread
B.Calling the wait method on an object
//thread will goto waiting state
C.Calling notify method on an object
//..
D.Calling read method on an InputStream object
//may be block if it waiting for io
E.Calling the SetPriority method on a Thread object
//back to runnable state if has anyhigher priority

2. ans C,D or D
A.Calling the yield method
//Back to runnable
B.Calling the wait method on an object
//Goto waiting state
C.Calling the notify method on an object
//??
D.Calling the NotifyAll method on an object
//??
E.Calling the start method on another Thread object
//??

I don't think notify has the effect to stop thread

I am not sure about the answer.And I will waiting for your answer here...



SCJP Passed(93%)<br />SCWCD Preparing<br />SCEA future.<br /> <br />Here is the great community!!
dx wu
Ranch Hand

Joined: Apr 28, 2002
Posts: 60
any more answers?
Sunyaluk Boonmas
Greenhorn

Joined: Apr 18, 2005
Posts: 24
I want the more exacly answer and explanation.

Anynone else?

Thank you.
S.L.Narayanan
Ranch Hand

Joined: Apr 01, 2005
Posts: 431
I have two question,thanks to someone to help me.
1.Which two CANNOT directly cause a thread to stop executing? (Choose Two)
A.Existing from a synchronized block
B.Calling the wait method on an object
C.Calling notify method on an object
D.Calling read method on an InputStream object
E.Calling the SetPriority method on a Thread object


The exact answer is A & C.

Because (A).Exiting from a synchronized block will make the thread to release the locks only. Because of that thread will not stop executing.
(C). If some code is there inside synchronized block to be executed by the current thread which called notify, the thread will run till it finishes its work.

In all other cases the thread will stop.



2.Which two CANNOT directly cause a thread to stop executing? (Choose Two)

A.Calling the yield method
B.Calling the wait method on an object
C.Calling the notify method on an object
D.Calling the NotifyAll method on an object
E.Calling the start method on another Thread object I have two question,thanks to someone to help me.


Here also the answers are C & D.
The same explanation for Question 1 applies here also.

E cannot be an answer because if you start a new thread with an higher priority, there is a chance for the current one to stop.


SCJP 1.4, SCWCD 1.4, SCBCD 1.3, NEXT--->IBM XML
dx wu
Ranch Hand

Joined: Apr 28, 2002
Posts: 60
thanks to S.L.Narayanan
kay lin
Ranch Hand

Joined: May 20, 2004
Posts: 132
Originally posted by S.L.Narayanan:
[QB]I have two question,thanks to someone to help me.
1.Which two CANNOT directly cause a thread to stop executing? (Choose Two)
A.Existing from a synchronized block
B.Calling the wait method on an object
C.Calling notify method on an object
D.Calling read method on an InputStream object
E.Calling the SetPriority method on a Thread object


I think the answer should be B and E, for B, because even if the wait() on a thread is called, the thread will finish executing the synchronized block of code and then go out the waiting/blocking state.

for E, if you read page 513 of the Sierra and Bert's book on yield(), I think the answer should be clear. Suppose we have Thread A that has a priority of 6, and Thread B has a priority of 3, now suppose Thread A is executing, and you call setPriority to bump up Thread B's priority to 6, there is a good chance that the JVM will still pick Thread A to run.

2.Which two CANNOT directly cause a thread to stop executing? (Choose Two)

A.Calling the yield method
B.Calling the wait method on an object
C.Calling the notify method on an object
D.Calling the NotifyAll method on an object
E.Calling the start method on another Thread object I have two question,thanks to someone to help me.


I think the answer is A and B,
for A, it is obvious if you consult page 513 of the book
and For B, it is just the same as the first question


Here are my explanations to these questions, after i consulted the book and gave the question some thought..
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
I think the correct answer for 1. is A,E
Becasue both of the method Cannot directly cause a thread to stop executing.

see in all other option they may stop the thread but in A,E they may not stop..


and correct answer for 2 . is A,E
becasue yeild method stop currently executing thread only if its priority is lower then the thread waiting to execute.
and calling start method on other method may not begin immediately..

The point to be noted in above question is that it is asking for "cannot stop directly"
other option will stop the thread sooner or later..
and more immediately then correct answer option

well ...do give comments


Thanks and Regards, Amit Taneja
Sunyaluk Boonmas
Greenhorn

Joined: Apr 18, 2005
Posts: 24
It has many of answer.

Uhm everybody has own reason.

If it possible please tell me where is this question come from?

I want to know what is the answer from the author.
I am confuse in answer.

Thank you so much.
kay lin
Ranch Hand

Joined: May 20, 2004
Posts: 132
yes, please let us know where the question comes from...
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19060
    
  40

Originally posted by dx wu:
I have two question,thanks to someone to help me.
1.Which two CANNOT directly cause a thread to stop executing? (Choose Two)
A.Existing from a synchronized block
B.Calling the wait method on an object
C.Calling notify method on an object
D.Calling read method on an InputStream object
E.Calling the SetPriority method on a Thread object


I know that we are supposed to choose 2, but for this question, I can only choose one...

First, B and D are obviously wrong because both of those operations can block, and hence, allow another thread to run.

Second, E is wrong because it is possible to raise the priority level of another thread that it will cause a prempt to that thread.

Now, I am pretty sure that A was meant to be one of the two answers, but there is a case where it will cause the current thread to stop executing... specifically...

Let say you have two threads, a high priority and a low priority thread. The high priority thread is running until it does some I/O. The low priority thread runs and enters the synchronized block. The high priority thread prempts the low priority thread when it returns from I/O. The high priority thread then tries to grabs the same synchronized lock -- but blocks because it is owned by the low priority one.

Now... the low priority thread runs, but as soon as it exits the synchronized block, it will be prempted, in favor of the high priority thread...

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: thread problem