Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread Question

 
Daniel Liu
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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???
 
Ankur Gupta
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Daniel Liu
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ankur Gupta
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic