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


hi frnds,
i felt the answer to be 3 but when i executed i found it is printing world 5 times..why is it so ??

here Testclass also has the Start method inherited .so iam unable to understand the O/P??can anyone please explain??

srikanth
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

i felt the answer to be 3 but when i executed i found it is printing world 5 times..why is it so ??



Actually the answer for this is "Total 5 words will be printed." its not "WORLD" printed 5 times.Because the thread t2 and t1 starts executing simultaneously and as 'i' is static the exact value of i in both the loops interdepends.So the assured output is total five words will be printed.When you run the program you could get five "Hello" or five "World" printed that depends on the JVM to JVM.
Change for loops as for(; i<10; i++) now the output on my machine is

E:\java\dev\Miss>java TestClass
World
World
World
World
World
World
World
World
Hello
Hello
Hello

So the assured output irrespective of JVM is a total of 10 words will be printed.
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The output I think is mainly dependent on the static variable i.

Try to execute the following, u will get the answer.
 
Ranch Hand
Posts: 481
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all

This has to be explained more detailed

here when i executed the output i Got is 5 "world".

After I interchanged the start method as

public static void main(String args [])
{
Thread t1 = new A();
Thread t2 = new TestClass();
t1.start();
t2.start();
}

I got the output as fine "Hello"

So As i per knowledge this shows that as "i" is static which ever the first theard is start only that theards run method value is printed.

Please check and help me out clearly
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unfortunately, I think choice 4 is also wrong. The reason is "i" is not declare volatile. This means that it is possible for the two threads to have a cache copy and print more than 5 words.

Now this is very unlikely, because the threads are so short lived. In fact, the first thread would probably finish before the second one is started... but techically, choice 4 is also wrong.

Henry
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First the thread t2 is started so it executes its run() version and the static variable i is used from 0 t0 4 and only then control is passed to the next thread ie t1.start().if t1's run() version has iteration say from i=0 to i<10 after excecution of run() of t2 for 5 times(i= 0 to 4),i's current value is i=5. hence now with t1.run() call the loop starts from i=5 and iterates till i<10 and prints hello,ie for i= 0 to 4 t2 prints world 5 times
and for i=5 to 9 t1 prints hello 5 times
thus the output will be world
world
world
world
world
hello
hello
hello
hello
hello
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic