• 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

Printing sequence - Threads

 
Ranch Hand
Posts: 81
IntelliJ IDE Oracle C++
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the following code,

the output got is 'In doIt method' followed by ' Thread is running.'

I was expecting the output in reverse- ' Thread is running.' followed by ' In doIt method'. Could someone please clarify this behavior?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Thread.start() method returns pretty much immediately, but it takes some time for a thread to get going. The thread that called start() (i.e., your main thread) keeps running while the newly-created thread gets ready to go. By the time that new thread starts actually running Java code, the original thread will have executed many instructions.

So the main thread here calls start(), then immediately calls doIt(), which prints a message. All the while, the new thread is being initialized. Finally, some time after doit() is called, the new thread starts to execute your code.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic