Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

6/13/13

Bill Brodgen's Hardest Question no. 15 (SCJP forum at JavaRanch)

File APIs for Java Developers Manipulate DOC, XLS, PPT, PDF and many others from your application. http://aspose.com/file-tools

Big Moose Saloon


A friendly place for programming greenhorns!
Search

Java FAQ

Recent Topics

Register / Login

A special promo: Enter your blog post or vote on a blogger to be featured in an upcoming Journal

JavaRanch Java Forums Certification Programmer Certification (SCJP/OCPJP)

Author

Bill Brodgen's Hardest Question no. 15


posted 5/16/2001 12:50 AM

Prasanna Wamanacharya Ranch Hand Joined: Apr 24, 2001 Posts: 143

Hi, the following question is from Bill Brodgen's Hardest Questions. You are working on an applet which does animation. There is a single object animC, derived from java.awt.Canvas, which holds all the animation data and a memory image. There is a single Thread, animT which uses this data to create a new image for the next animation frame and then calls the following method.
view plain c opy to c lipboard print ?

N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped

0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 .

s y n c h r o n i z e dv o i dw a i t F o r P a i n t ( ){ p a i n t e d=f a l s e ; r e p a i n t ( ) ; w h i l e ( ! p a i n t e d ){ t r y{ w a i t ( ) ; }c a t c h ( I n t e r r u p t e d E x c e p t i o nx ){ } }
1/3

www.coderanch.com/t/199379/java-programmer-SCJP/certification/Bill-Brodgen-Hardest

6/13/13

Bill Brodgen's Hardest Question no. 15 (SCJP forum at JavaRanch)

1 0 .

The paint method in animC executes the foll. code after the new animation frame has been shown.
view plain c opy to c lipboard print ?

N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped

0 1 .

s y n c h r o n i z e d ( t h i s ){ p a i n t e d=t r u e ;n o t i f y ( ) ; }

After animT has entered the wait() method in waitForPaint and before the paint method has been executed, which of the foll. statements is true? a. Other Threads cannot modify data in the animC object. b. Other Threads can only modify data in the animC object using synchronized methods. c. Other Threads can only modify data in the animC object using synchronized or unsynchronized methods. d. If the animT Thread is interrupted, it will exit the waitForPaint method. Answer given: c I think that even d should be correct. Explanation for option d is given as

Answer d is wrong because when a blocked Thread is interrupted, an InterruptedException is thrown. Since this exception is caught inside the while loop, animT will re-enter the wait method.

My question is, what is the point of interrupting a Thread, if it is going to reenter the wait method and susequently into the waiting state? If we want the method to exit, should we just declare the exception in the throws clause of the method, and not handle it in the method, but handle it in the calling method? Thank you, Prasanna.
js yang Ranch Hand Joined: May 09, 2001 Posts: 40

posted 5/16/2001 3:41 AM

Note Brogden's explanation: Answer d is wrong because when a blocked Thread is interrupted, an InterruptedException is thrown. Since this exception is caught INSIDE THE WHILE LOOP, animT will re-enter the wait method. Normally, when an InterruptedException is thrown, the thread leaves the wait status. However, in Brogden's problem, the InterruptedException is in a while loop--when the thread leaves the wait status, it is thrown back into waiting because of the while loop.

Prasanna Wamanacharya Ranch Hand

posted 5/16/2001 10:53 AM

www.coderanch.com/t/199379/java-programmer-SCJP/certification/Bill-Brodgen-Hardest

2/3

6/13/13

Bill Brodgen's Hardest Question no. 15 (SCJP forum at JavaRanch)

Joined: Apr 24, 2001 Posts: 143

Thank you js yang, I have understood it now. Prasanna.

Mikael Jonasson Ranch Hand Joined: May 16, 2001 Posts: 158

posted 5/16/2001 3:07 PM

Just to point out the obvious: It doesn't have to re-enter the loop. First i re-checks the value of painted. If that is true now, it leaves the loop.

I agree. Here's the link: http://aspose.com/file-tools

subject: Bill Brodgen's Hardest Question no. 15

Similar Threads Threads Thread question: synchronization on differerent obj Threads: Synchronized method threads thread related
All times above are in your local time zone & format.T he current ranch time (not your local time) is Jun 13, 2013 03:07:31 .

Contact Us | Powered by JForum |

C opyright 1998-2013 Paul W he aton

www.coderanch.com/t/199379/java-programmer-SCJP/certification/Bill-Brodgen-Hardest

3/3

You might also like