• 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

Threads

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From JavaCaps.com

How is the output 10 11 sh 11 12 zz

 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mohammad shaid wrote:
How is the output 10 11 sh 11 12 zz



good.[do you get answare to your question? ]

there are three threads .
10 [Main]
11 sh [Thread1]
11 [Main]
12 zz[Thread2]

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mohammad,

here it deals with three different threads:
1) the main thread (commented with // #1 and // #2 ),
2) the thread named "thread" and
3) the thread named "thread2", both using an implementer of the Runnable interface.
(both implementer override the run() method, incrementing the static member variable x)

Due to the fact that nearly nothing is predictable concerning thread execution, we can't say which of the above-mentioned thread is executed first.
(or better to say: the main thread is always executed first, but we can't determine when exactly another thread pushes the main thread from the running to runnable state).

Even thread2 - System.out.print(x + " zz "); - can be executed first - it's up to the thread scheduler how to handle the execution orders (besides, I've checked the outputs with eclipse).

So there are several ouput possibilities:
10 (#1) 10 (#2) 11 zz 12 sh
11 sh 11 (#1) 11 (#2) 12 zz
10 (#1) 11 sh 11 (#2) 12 zz
etc.


Please take into account that lines #1 and #2 belong to the main thread, so x is not incremented.
For sake of readability, I've put "#1" and "#2" into the output, too.

Hope this helps!



 
mohammad shaid
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ulrich.. All in all the output is unpredictable.. i was thinking that the Runnable Anonymous subclass will be executed first since it is in main method.. i was wrong.. there's 3 diff threads.. Thanks Ulrick///
 
Yeah. What he said. Totally. Wait. What? Sorry, I was looking at this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic