• 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 doubt

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

In java if main thread dies still child threds exit??

Thanks in advance
siva
 
Marshal
Posts: 79508
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try setting up the Event Despatch Thread with a Swing Container (eg a plain JFrame).
Throw an Exception from the main thread (eg System.out.println(1 / 0);) . . .
 
siva mail
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Campbell

I didn't understand this .can you please give me more explanation with simple example.

Thanks in advance
Siva
 
Sheriff
Posts: 22791
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell meant the following:

That call to EventQueue.invokeLater starts the Event Dispatcher Thread.
 
Campbell Ritchie
Marshal
Posts: 79508
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, Rob, that is exactly the sort of thing I meant. Thank you.
 
siva mail
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,

What i understand from the code is even if main thread exit . still child threds alive. am i correct?.

if that is the case i have one more doubt. if child threads are waiting for some resources and main thread dies.Now how can i notify the waiting child thread. I am bit confusing about child threds and main thread relation. can you please clarify me

Thanks
Siva
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There isn't really a relationship between the main thread and any other thread that it starts*. If you want to make a relationship between threads then you have to build it yourself using some shared information. See The Concurrency Tutorial to learn more about how independent threads can communicate.

* The exception to this is Daemon threads.
 
siva mail
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if there is no relation then why we are calling as child threads which are startd in main method??. which threads we can call as child threads??.you mean we can make child and parent(main) thread relation through API only??. In a program Main therad is the last thread always execute . here my doubt is if main thread exit, the thread which is started in main method still waiting for some resource.how can i notify that thread to do for some other actions.

Please clarify and correct me

thanks
Siva
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Once you start a new thread, it is an independent, parallel stream of execution.

'Why do we call 'child Threads child threads?'

I usually don't, but just like in life, where a Child human is a person born from a Parent human, a Child Thread is one that is started from the Parent Thread. That is all the relationship they have, one started as a course of exectution of the other.

Why don't we take the Parent-Child metaphor further - consider real-world parent-child relationships. If my mother (parent) dies, do you expect me (the child) to die as well? If my father (parent) has some information, how do I (child) get that info? Not automatically. If my father needs to get some information to me he has to come up with a system to tell me (like calling me, or sending me a message). If he dies before I can get the information than I hope he left it someplace I can get to by myself. Otherwise it is lost.

You should read that tutorial link I provided earlier it will help explain in more detail.
 
siva mail
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks steve...
very good explanation
 
reply
    Bookmark Topic Watch Topic
  • New Topic