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 thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "thread" Watch "thread" New topic
Author

thread

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
<pre>
Thread myT = new Thread();
myT.start();
</pre>
True/false ?
a. Thread myT is now in a runnable state //True
b. Thread myT has the NORM_PRIORITY priority.
c. Thread will die without accomplishing anything. //True
d. The run method in the class where the statement occurs will be executed.......//why not True ? since start() will execute run().
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Here you are creating an object of class Thread. It has a public void run() { } method ,which does nothing when started. Your code does exactly the same. The Thread class has another constructor Thread(Runnable r) . If you create a thread using this constructor by sending a Runnable object (an object of a class which implements Runnable / an object of a class which extends Thread) , then the run() method this object which is used in the arg to create the thread will be invoked when it is started.
Note that 3rd and 4th ans. mean same thing. Since you accepted the 3rd ans as true, why do you get the doubt for ans 4)?
4th ans is false.
regds
maha anna.
[This message has been edited by maha anna (edited March 11, 2000).]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I am still confused with choice (d).
I am sure that in option (c), start() calls public void run{} of Thread and hence does nothing.
Why not in option (d) follow, start()....public void run()...does nothing... ?
Here my point is whether Thread reaches to run() or not.
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Umesh,
The myT thread does reach the run() method. But it is the run method of the Thread class.
not run() method of the class in which this code is written
. See the code foll.Cut and paste this code and try it.
regds
maha anna
<pre>
class TestThread extends Thread {
public void run() {
System.out.println("Umesh");
}
public static void main(String[] args) {
//Thread myT = new Thread(new TestThread());
Thread myT = new Thread();

//See above stmt carefully. It is a vanilla Thread object
//which is not connected to this TestThread class at all.

myT.start();
/** When this stmt is executed, it
actually calls the vanilla Thread's empty
run() method. The run method which we defined in this class TestThread
WILL NOT be invoked, because myT was created as new Thread().
In order to invoke our run() method which prints "Umesh", we have to create
the thread like this.
Thread myT = new Thread(new TestThread());
*/
}
}
</pre>

[This message has been edited by maha anna (edited March 11, 2000).]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Anna, great from U....Thanx in big way!!! for the vanila stuff.
Thiru Thangavelu
Ranch Hand

Joined: Aug 29, 2001
Posts: 219
Maha, the following code doesn't print anything for me. I expected the answer Umesh
<pre>
class TestThread extends Thread {
public void run() {
System.out.println("Umesh");
}
public static void main(String[] args) {
Thread myT = new Thread(new TestThread());
}
}
</pre>


Thanks,<br />Thiru<br />[SCJP,SCWCD,SCBCD]
Paulo Silveira
Ranch Hand

Joined: May 21, 2002
Posts: 32
After

it is missing the .start() call.
Paulo Silveira
Ranch Hand

Joined: May 21, 2002
Posts: 32
Originally posted by Umesh:

a. Thread myT is now in a runnable state //True


It is not true...
Isnt it in the READY state?
Or it is just yet another name for it?
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Paulo Silveira:

Isnt it in the READY state?

That's correct. When you invoke the start method of a Thread, a new thread is created and goes into the "ready to run" state. From there, it can transfer to the running state whenever the corresponding context switch takes place.
I hope that helps,
Corey


SCJP Tipline, etc.
Steven Wong
Ranch Hand

Joined: Mar 07, 2002
Posts: 295
Hi,
b. Thread myT has the NORM_PRIORITY priority.
How about the priority of this thread? Is the b statement true or false?
Clement


best regards,<br />Steven<br />SCJP, SCEA
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
I believe that, when you start a thread, it obtains the same priority as the thread that started it. The main thread, by default, has the normal priority. Therefore, unless you modify the priority of one of your threads along the way, any thread you create will have the normal priority.
So, from the above snippet, it would be impossible to tell if the new thread had the normal priority or not. Therefore, I would say that B is a false statement.
I hope that helps,
Corey
Khurram Fakhar
Ranch Hand

Joined: May 29, 2000
Posts: 65
Hi corey,
Dont assume something in exam for such questions. As You have to reply with in the domain of the question. Just look at the question , there is nothing like change in the priority of the main thread or anyother parent thread. So it is true.
Kindly dont confuse urself in the exam while assuming such things . If we start thinking like this then we will start guessing and judging in most of the questions of the exam
Thanks
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
I have a feeling that this code snippet is taken from a larger snippet for a question. If you look at answer C:

c. Thread will die without accomplishing anything. //True

How could we know that this statement is a true statement without seeing more code than what it posted here. As it is, there is nothing that would allow us to make this conclusion (or disprove it).
Corey
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Originally posted by Corey McGlone:

How could we know that this statement is a true statement without seeing more code than what it posted here. As it is, there is nothing that would allow us to make this conclusion (or disprove it).
Corey


Well, there is the JavaDocs for Thread.run()...

If this thread was constructed using a separate Runnable run object, then that Runnable object's run method is called; otherwise, this method does nothing and returns.




Rob
SCJP 1.4
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: thread
 
Similar Threads
ExamCram - Threads
Thread Question
to maha - new state of a Thread ?
thread
exam cram