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


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread Question" Watch "Thread Question" New topic
Author

Thread Question

Daniel Liu
Greenhorn

Joined: Jul 05, 2000
Posts: 19
What is the output when compile and excute the following code?
class Test implements Runnable {
public static void main(String args[]) {
Test rt = new Test();
Thread t = new Thread(rt);
t.start();
System.out.println("after t.star()");
}
public void run() {
System.out.println("in run()");
}
}
The output on my machine (NT) was:
after t.star()
in run()
Why???

Daniel
Ankur Gupta
Ranch Hand

Joined: Jun 13, 2000
Posts: 66
Why? as in
1)why this code is working
OR why as in,
2)why was "after t.star()" printed before "in run()" ?
Daniel Liu
Greenhorn

Joined: Jul 05, 2000
Posts: 19
Originally posted by Daniel Liu:
What is the output when compile and excute the following code?
class Test implements Runnable {
public static void main(String args[]) {
Test rt = new Test();
Thread t = new Thread(rt);
t.start();
System.out.println("after t.star()");
}
public void run() {
System.out.println("in run()");
}
}
The output on my machine (NT) was:
after t.star()
in run()
Why???

Sorry, it was not clear in my original post. My question should be read why "after ... " was printed before "in run()" ?
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
The reason after start() printed first is because it's in the currently executing Thread. Even though you create a new Thread to for execution of your .run() method, you haven't released the Thread of execution. So main() is goin to hole the thread of execution until it's finished then release it to the next waiting Thread(ie, .run())..
Ankur Gupta
Ranch Hand

Joined: Jun 13, 2000
Posts: 66
This is because the main thread has not finished executing, even though Thread "t" is in ready state. The priority of t will be 5 which would be the same as that of "main" thread. When the thread main relinquishes control then Thread t gets chance to execute.
Does this clarify your doubt?
Ankur
sasank manohar
Ranch Hand

Joined: Feb 14, 2008
Posts: 186

I tried to use yield() in the main method, when I called yield() from the currently running thread that is main() then the o/p is
in.run()
after t.start()
May be you can make out the answer why it was reverse before.
Question: I have a doubt, how can you say the thread priority is 5.
class Test3 implements Runnable {
public static void main(String args[]) {
Test3 rt = new Test3();
Thread t = new Thread(rt);

t.start();
Thread.yield();

System.out.println("after t.start()");
}
public void run() {

System.out.println("in run()");
}

}
thanks
sdev


"SCJP5 | SCWCD5| DEVELOPER"
Daniel Liu
Greenhorn

Joined: Jul 05, 2000
Posts: 19
Originally posted by sdev:

I tried to use yield() in the main method, when I called yield() from the currently running thread that is main() then the o/p is
in.run()
after t.start()
May be you can make out the answer why it was reverse before.
Thank you, sdev and Ankur.
Thread class has static final int member
MIN_PRIORITY =1;
NORM_PRIORITY = 5;
MAX_PRIORITY =10;
The default priority is NORM (I guess) if you
do not set priority property for a thread.
Question: I have a doubt, how can you say the thread priority is 5.
class Test3 implements Runnable {
public static void main(String args[]) {
Test3 rt = new Test3();
Thread t = new Thread(rt);

t.start();
Thread.yield();

System.out.println("after t.start()");
}
public void run() {

System.out.println("in run()");
}

}
thanks
sdev

Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Actually the order in which the two statements are printed is unpredictable. Since thread scheduling is vendor dependent, you may never know whether the thread 't' gets picked up by the scheduler immediately after start, OR if the main continues to run. If you run this program on different platforms with different schedulers, it behaves differently.
Ankur, your reasoning about the priorities is right. However, remember that some thread schedulers(time-sliced for example, ) might not honor the priority.
Ajith


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Ankur Gupta
Ranch Hand

Joined: Jun 13, 2000
Posts: 66
A newly created Thread will always inherit the priority of its parent thread unless u set it, by setPriority(). "5" is the normal priority assigned to a thread by default.
Also, it is not necessary that main thread will complete before the Thread t starts executing. For this example this seems to be the case. But you cannot guarantee that main will finish before Thread "t" gets a chance to execute and this is because the priorities for both the threads in question are same. Try appending around 30 print statements, or maybe more, after "t.start()" in the main method and then try running the program. U will see that in some the cases "in run()" does get printed in the middle of "after t.star()".
Ankur
[This message has been edited by Ankur (edited July 14, 2000).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread Question