File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads 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 " Watch "Threads " New topic
Author

Threads

ramesh maredu
Ranch Hand

Joined: Mar 15, 2008
Posts: 210

Following code is taken from whizlabs, can someone explain why output is

Thread1.run()
Thread1.run()




SCJP 1.5 94%.
The greatest glory in living lies not in never falling, but in rising every time we fall.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

I don't know the exact reason, but judging by the output, it is because the Thread1 class overrides the run method of Thread class. What the run method of Thread class do is check if this thread was created using a runnable by checking if a private instance of Runnable in Thread class is null or not. If it is not null, then it would call the run method of that object otherwise it would do nothing.



But in this case, the run method of the overriding class Thread1 class is invoked, so it doesn't do this check and directly displays Thread1.run()

This is what I feel but I may be wrong as this is just a prediction...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Paul Somnath
Ranch Hand

Joined: May 19, 2008
Posts: 177
Here in the code, you are actually never invoking the run method inside Thread2.
When you implement the Runnable interface, the Thread class should be passed the Runnable target for the run() method to execute as a new thread.
So if you want to execute the run() method of Thread2,
you will have to code like this:



In the original code, you never pass the Runnable target in the Thread object..
Any other suggestions are welcome.
[ September 04, 2008: Message edited by: Somnath Paul ]

Preparing for SCJP 6.0
Paul Somnath
Ranch Hand

Joined: May 19, 2008
Posts: 177
You can find a detailed surgery of the case here.

vidhya suvarna
Ranch Hand

Joined: Aug 28, 2008
Posts: 148
As you know you can create a thread either by extends the THREAD class or by implementing the RUNNABLE interface.

So in the code:
new Thread1().start --> is invoking the call to the RUN() method of THREAD1 which is a common way to create a new thread. Simpler way is:
Thread1 t1=new Thread1();
t1.start();

Now,
new Thread1(new Thread2()).start();--> First calls the 2nd constructor of THREAD1 i.e
Thread1(Runnable runnable) {
super(runnable);}--> which inturns call the constructor of THREAD with RUNNABLE agrument.
Simpler way is:
Thread2 t2=new Thread2();
Thread1 t1=new Thread1(t2);
t1.start(); -->which also invokes the THREAD1's start as its THREAD1's instance

i hope i have cleared your doubt.


SCJP 1.4 - 88%<br />SCWCD 1.5 - Preparing
 
 
subject: Threads
 
Similar Threads
join() method usage
Need Explanation of unpredicted output even if methods are synchronized
Threads
locking??
Thread synchronization