aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Whizlabs Thread Question 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 "Whizlabs Thread Question" Watch "Whizlabs Thread Question" New topic
Author

Whizlabs Thread Question

catherine powell
Greenhorn

Joined: Oct 07, 2006
Posts: 26
The following is a question from Whizlabs SCJP 1.4 mock test:

What will happen when you attempt to compile and run the following code:

class MyThread extends Thread{
public void run(){System.out.println("MyThread: run()");}
public void start(){System.out.println("MyThread: start()");}
}
class MyRunnable implements Runnable{
public void run(){System.out.println("MyRunnable: run()");}
public void start(){System.out.println("MyRunnable: start()");}
}
public class MyTest{
public static void main(String[] args){
MyThread myThread = new MyThread();
MyRunnable myRunnable = new MyRunnable();
Thread thread = new Thread(myRunnable);
myThread.start();
thread.start();
}
}

Whizlabs answer:
Prints: "MyThread: start() followed by MyRunnable:run()"

I thought the answer should be "None of the above".

My thinking is that theoretically the thread referenced by myThread could remain in a Runnable state after starting with the main thread continuing to run. The main thread would call start() on the thread referenced by thread. At that point, myThread and thread would both be in the pool of Runnable threads and you cannot predict which one of them the thread scheduler would choose to run first. I thought that would mean the output is not guaranteed to be: "MyThread: start() followed by MyRunnable:run()" but could possibly be "MyRunnable: run() followed by MyThread: start()".

I don't know if I am wrong and if there something that I'm not understanding. I just want to make sure I know what the correct logic is should there be a similar question on the actual test. Could someone please help me with this? Thanks very much in advance!
Matt Russell
Ranch Hand

Joined: Aug 15, 2006
Posts: 165
Originally posted by catherine powell:
My thinking is that theoretically the thread referenced by myThread could remain in a Runnable state after starting with the main thread continuing to run. The main thread would call start() on the thread referenced by thread. At that point, myThread and thread would both be in the pool of Runnable threads and you cannot predict which one of them the thread scheduler would choose to run first. I thought that would mean the output is not guaranteed to be: "MyThread: start() followed by MyRunnable:run()" but could possibly be "MyRunnable: run() followed by MyThread: start()".


If I understand it correctly (and it's quite possible that I don't!), myThread will not enter the "Runnable" state. After construction, a thread is in the "New" state until it is started. However, a MyThread can not be started because start() is overriden, and Thread.start() never gets called (and there's nothing magical about calling a method that happens to be called "start").

As an aside, if the lines

were the other way round:

then I think the output would be indeterminate.


Matt
Inquisition: open-source mock exam simulator for SCJP and SCWCD
Ali Gohar
Ranch Hand

Joined: Mar 18, 2004
Posts: 572
myThread.start() will not start the thread, infact it calls the start method defined your class. So there is one thread main running only when myThread.start() was called. Whereas the other thread started in next line. So Whizlab answer is correct
catherine powell
Greenhorn

Joined: Oct 07, 2006
Posts: 26
I got it now...thank you guys very much!
Vivek Vikash
Greenhorn

Joined: Oct 12, 2006
Posts: 8
Whereas the other thread started in next line.


I thought that second thread will also fail to start but it is not so.
Can you please explain me why is so?
Matt Russell
Ranch Hand

Joined: Aug 15, 2006
Posts: 165
vivek: What made you think the second thread would fail to start?
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi cowgals &boys,

we have here two different things,
first:


MyThread is subclassing Thread but it does not only override run() but also start().

The original start() method of class Thread should invoke run().
But in this case, start() is overriden and just prints "MyThread: start()". The run() method of MyThread is never invoked.

second

Here we have a Runnable with its run() method (wouldn't compile without) plus an additional start() method.
The Runnable is passed to the Thread constructor.
When a Runnable is passed into the constructor of a Thread, the run() Method of the Runnable (and not of Thread) will be invoked. And this prints "MyRunnable: run()" in this example.
Here the start() method of the Runnable is never invoked.

Yours,
Bu.


all events occur in real time
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Whizlabs Thread Question