aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads Self Test (Kathy & Bert SCJP 6) 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 "Threads Self Test (Kathy & Bert SCJP 6)" Watch "Threads Self Test (Kathy & Bert SCJP 6)" New topic
Author

Threads Self Test (Kathy & Bert SCJP 6)

Eduardo Fierabras
Greenhorn

Joined: Sep 13, 2009
Posts: 8
Hi, I got a doubt about the next Self Test question: (page 777)

9. Given:

What is the result of this code?

A. It prints X and exits
B. It prints X and never exits
C. It prints XY and exits almost immediately
D. It prints XY with a 10 seconds delay between X and Y
E. It prints XY with a 10000 seconds delay between X and Y
F. The code does not compile
G. An exception is thrown at runtime

Answer:
G is correct. The code does not acquire a lock on t before calling t.wait(), so it throws an IllegalMonitorStateException. The method is synchronized, but it's not synchronized on t, so the exception will be thrown. If the wait() were placed inside a synchronized(t) block, then the answer would have been D.

Ok, I can understand the answer, but there is something in the code that I wonder about.
Thread t = new Thread(); creates a Thread instance, instead of create an object that extends Thread, to override the run() method.
The Thread class original run() methot does nothing if the thread object wasn't constructed using a separate Runnable run object (as said in the API Javadoc).
Then, when the scheduler select the t thread to pass it from runnable to running, how is that the code executed is the statements following t.start();
I thought that when a thread goes into running state, the code in the run() method is executed. In this case the run() method is empty, or not?

Any help will be greatly appreciated.
Thanks a lot, and sorry for my bad english!!!
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19068
    
  40

Eduardo Fierabras wrote:
Then, when the scheduler select the t thread to pass it from runnable to running, how is that the code executed is the statements following t.start();
I thought that when a thread goes into running state, the code in the run() method is executed. In this case the run() method is empty, or not?


The default run() method is not exactly empty, but yea, it won't do anything noticeable. Just quickly do something and done -- pretty much as if the run() method was empty.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Eduardo Fierabras
Greenhorn

Joined: Sep 13, 2009
Posts: 8
Hi Henry, thanks for your quickly answer.

I know that the original run() method isn't totally empty. What I mean is that it haven't any code written by me.
The API javadoc says that the run() method of the Thread class will call the run() method of the runnable object passed to the Thread constructor.
If the Thread object wasn't created passing a runnable object, the original run() method (not overriden) does nothing.
Perhaps the correct rule is the following: In the book example of code, the main thread creates a new thread (t), and t starts running the remaining code of the main() method.
And when t will terminate his job, the program will be finished, and the main thread that was executing the main() method is dead.
But what if the scheduler put thread t back to runnable before it has terminated his job? Then, the main thread can be put running again? And if so, at what point continues his job?

Thanks again.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19068
    
  40

Eduardo Fierabras wrote:
Perhaps the correct rule is the following: In the book example of code, the main thread creates a new thread (t), and t starts running the remaining code of the main() method.
And when t will terminate his job, the program will be finished, and the main thread that was executing the main() method is dead.
But what if the scheduler put thread t back to runnable before it has terminated his job? Then, the main thread can be put running again? And if so, at what point continues his job?


Sorry, read what you wrote three times. Don't have a clue on the point you are making.

How is an empty run() method that different from a run() method does something insignificant, that the threading engine has to do something different?

Henry
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19068
    
  40

Eduardo Fierabras wrote:
Perhaps the correct rule is the following: In the book example of code, the main thread creates a new thread (t), and t starts running the remaining code of the main() method.


When the main thread creates and starts the new thread, then there will be two threads. Thread t will do the initialization code, then run() method, then teardown code for its thread. And the main thread will continue running "the remaining code of the main() method". After which, it will do the teardown code for the main thread.

Eduardo Fierabras wrote:
And when t will terminate his job, the program will be finished, and the main thread that was executing the main() method is dead.


When thread t, or the main thread finishes, *and* it is the last thread, then the program is finished. As long as there is an user thread running, the program is not finished.

Henry
Eduardo Fierabras
Greenhorn

Joined: Sep 13, 2009
Posts: 8
Thanks Henry, sorry to be so stupid.

The t thread can't do anything because his run() method does nothing. The main thread continues his job, the main() method, at line 5. At this point, t is dead.

Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Henry : Does using "synchronized" on main() have any effect on the
user application? I would guess not, but what does the JVM do with it?

Jim... ...


BEE MBA PMP SCJP-6
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

Synchronization on static methods make effect if they are called multiple times within the same JVM by multiple threads. The main thread is called by the JVM when the program starts. After that if you call it yourself using multiple threads, then it will make an effect (but your program can easily go into an infinite recursion if you do something like that)...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Thanks Ankit. Recursive calls to main() does indeed sound
risky. If static main() is synchronized, I suppose the class
object lock would be used.

Jim ... ...
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Henry Wong wrote:
Eduardo Fierabras wrote:
Then, when the scheduler select the t thread to pass it from runnable to running, how is that the code executed is the statements following t.start();
I thought that when a thread goes into running state, the code in the run() method is executed. In this case the run() method is empty, or not?


The default run() method is not exactly empty, but yea, it won't do anything noticeable. Just quickly do something and done -- pretty much as if the run() method was empty.

Henry


Have a look on this


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads Self Test (Kathy & Bert SCJP 6)