Originally posted by rajashree ghatak:
hi everyOne,
pls look at the below code and comment on it:
public void run()
{
for (int i=0;i<6;i++)<br /> System.out.println("Hello"+i);<br /> }<br /> public static void main(String[] r)<br /> {<br /> Thread t=new Thread(new ttest09());//ttest09 is a Runnable object<br /> t.start();<br /> for(int i=15;i>0;i--)
System.out.println("World"+i);
t.start();
System.out.println("main() Ending");
}
Output is
15 times "World" and 6 times "Hello" is appearing and at the end "main() Ending" appears.
[b] My query is why isn't "Hello" again getting printed 6 times since start() is called twice on Thread t?
My 2nd query:if i=10 in 2nd for loop,output varies ne being 10 times "World" and 6 times "Hello" appearing alternatively and no Exception being thrown.
the second output being "World" and "Hello" being printed at a random order and IllegalThreadStateException being thrown.
i know why the above exception gets generated.what i want to know is if asked in the EXAM what should be our answer to the 2nd query and are such kind of Qs whose output is not guaranteed being asked?
rajashree.
[/B]
Dave
Originally posted by rajashree ghatak:
hi all
thanx for ur responses to my query.i have replaced the 2nd start()method in my code with run()method.
But the output is still varies with sometimes
1) 15 times "World" and 12 times "Hello"
2) 15 times "World" and only 6 times "Hello"
kindly tell me why is "Hello" not getting printed 12 times always, now that run() method is called directly.
rajashree.
....start() method throws IllegalThreadStateException if the thread was already started.....
.. running thread is allowed to execute for a fixed length of time , after which it moves to ready-to-run state to await its turn to run again....
Gagan (/^_^\) SCJP2 SCWCD IBM486 <br />Die-hard JavaMonk -- little Java a day, keeps you going.<br /><a href="http://www.objectfirst.com/blog" target="_blank" rel="nofollow">My Blog</a>
Gagan (/^_^\) SCJP2 SCWCD IBM486 <br />Die-hard JavaMonk -- little Java a day, keeps you going.<br /><a href="http://www.objectfirst.com/blog" target="_blank" rel="nofollow">My Blog</a>
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |