• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

thread

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<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().
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anna, great from U....Thanx in big way!!! for the vanila stuff.
 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After

it is missing the .start() call.
 
Paulo Silveira
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
b. Thread myT has the NORM_PRIORITY priority.
How about the priority of this thread? Is the b statement true or false?
Clement
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic