• 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

Main(Parent) and Child threads runs parallely?

 
Ranch Hand
Posts: 45
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does Main thread runs parallely with the thread being created in the same thread?


Sometimes the output come as 4950 and sometime it comes as 0.

I heard that thread behavior cannot be judged.But this looks more weird - Both threads runs simultaneously and main thread gets completed before child thread?

I guess they are considered as 2 separate threads and not parent and child here.Is that so?

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

Senthil Kumar Sekar wrote:I heard that thread behavior cannot be judged.But this looks more weird - Both threads runs simultaneously and main thread gets completed before child thread?


This is not weird. This is expected behaviour.

Senthil Kumar Sekar wrote:I guess they are considered as 2 separate threads and not parent and child here.Is that so?


What do you mean by child and parent? Why do you think that child/parent relationship in Java is supported in any way?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Senthil Kumar Sekar wrote:Does Main thread runs parallely with the thread being created in the same thread?



Yes. That's the whole point of multithreading.

However, that doesn't mean that both threads will be executing simultaneously. Depending on the hardware, the OS, what else is running on the system, and the threads' mix of CPU and I/O, they may run simultaneously, or they may take turns, or one may run to completion before the other one even starts. We can't predict that and we can't control it.

In your case, the main thread could look at the "b" variable before the other thread starts, after it's done, or anywhere in between. Also, since b is not declared volatile and you don't use syncing, any value the the second thread writes to b might not be visible to the main thread right away--if it reads b, it may still see the previous value.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Senthil Kumar Sekar wrote:
I guess they are considered as 2 separate threads and not parent and child here.Is that so?



Yes, they are two completely independent threads. The fact that one is the "parent" of the other has no effect on their scheduling.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic